Files

97 lines
2.7 KiB
Go

// Copyright (C) 2019-2022, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package key implements key manager and helper functions.
package key
import (
"bytes"
"errors"
"sort"
"github.com/luxfi/ids"
"github.com/luxfi/node/v2/v2/vms/components/lux"
"github.com/luxfi/node/v2/v2/vms/platformvm/txs"
"github.com/luxfi/node/v2/v2/vms/secp256k1fx"
)
var (
ErrInvalidType = errors.New("invalid type")
ErrCantSpend = errors.New("can't spend")
)
// Key defines methods for key manager interface.
type Key interface {
// P returns all formatted P-Chain addresses.
P(string) (string, error)
// C returns the C-Chain address in Ethereum format
C() string
// Addresses returns the all raw ids.ShortID address.
Addresses() []ids.ShortID
// Match attempts to match a list of addresses up to the provided threshold.
Match(owners *secp256k1fx.OutputOwners, time uint64) ([]uint32, []ids.ShortID, bool)
// Spends attempts to spend all specified UTXOs (outputs)
// and returns the new UTXO inputs.
//
// If target amount is specified, it only uses the
// outputs until the total spending is below the target
// amount.
Spends(outputs []*lux.UTXO, opts ...OpOption) (
totalBalanceToSpend uint64,
inputs []*lux.TransferableInput,
signers [][]ids.ShortID,
)
// Sign generates [numSigs] signatures and attaches them to [pTx].
Sign(pTx *txs.Tx, signers [][]ids.ShortID) error
}
type Op struct {
time uint64
targetAmount uint64
feeDeduct uint64
}
type OpOption func(*Op)
func (op *Op) applyOpts(opts []OpOption) {
for _, opt := range opts {
opt(op)
}
}
type innerSortTransferableInputsWithSigners struct {
ins []*lux.TransferableInput
signers [][]ids.ShortID
}
func (ins *innerSortTransferableInputsWithSigners) Less(i, j int) bool {
iID, iIndex := ins.ins[i].InputSource()
jID, jIndex := ins.ins[j].InputSource()
switch bytes.Compare(iID[:], jID[:]) {
case -1:
return true
case 0:
return iIndex < jIndex
default:
return false
}
}
func (ins *innerSortTransferableInputsWithSigners) Len() int {
return len(ins.ins)
}
func (ins *innerSortTransferableInputsWithSigners) Swap(i, j int) {
ins.ins[j], ins.ins[i] = ins.ins[i], ins.ins[j]
ins.signers[j], ins.signers[i] = ins.signers[i], ins.signers[j]
}
// SortTransferableInputsWithSigners sorts the inputs and signers based on the
// input's utxo ID.
//
// This is based off of (generics?): https://github.com/luxfi/node/v2/blob/224c9fd23d41839201dd0275ac864a845de6e93e/vms/components/lux/transferables.go#L202
func SortTransferableInputsWithSigners(ins []*lux.TransferableInput, signers [][]ids.ShortID) {
sort.Sort(&innerSortTransferableInputsWithSigners{ins: ins, signers: signers})
}