Documentation
¶
Overview ¶
Package XHDWalletAPI implements BIP32-Ed25519 Hierarchical Deterministic Keys over a Non-linear Keyspace for Algorand's ARC-52.
Reference: https://github.com/algorandfoundation/xHD-Wallet-API-kt
Index ¶
- Constants
- Variables
- func BIP39Seed(mnemonic, passphrase string) []byte
- func Harden(index uint32) uint32
- func IsHardened(index uint32) bool
- func VerifyWithPublicKey(signature, message, publicKey []byte) bool
- type AlgoPath
- type DerivationType
- type ExtendedKey
- type KeyContext
- type Wallet
- func (w *Wallet) AlgorandAddress(account, change, keyIndex uint32) ([]byte, error)
- func (w *Wallet) DeriveChildPublicFromExtended(xpk ExtendedKey, index uint32) ([]byte, error)
- func (w *Wallet) DeriveKeyExtended(path []uint32, isPrivate bool) (ExtendedKey, error)
- func (w *Wallet) ECDH(ctx KeyContext, account, change, keyIndex uint32, otherPub []byte, ...) ([]byte, error)
- func (w *Wallet) Export() ExtendedKey
- func (w *Wallet) KeyGen(ctx KeyContext, account, change, keyIndex uint32) ([]byte, error)
- func (w *Wallet) Path(account, change, keyIndex uint32) AlgoPath
- func (w *Wallet) Path00(keyIndex uint32) AlgoPath
- func (w *Wallet) RootKey() ExtendedKey
- func (w *Wallet) SignAlgoTransaction(ctx KeyContext, account, change, keyIndex uint32, txBytes []byte) ([]byte, error)
- func (w *Wallet) SignBytes(ctx KeyContext, account, change, keyIndex uint32, data []byte) ([]byte, error)
- func (w *Wallet) SignData(ctx KeyContext, account, change, keyIndex uint32, data []byte) ([]byte, error)
- func (w *Wallet) Verify(ctx KeyContext, account, change, keyIndex uint32, signature, message []byte) (bool, error)
- func (w *Wallet) Wipe()
Constants ¶
const ( BIP44Purpose = 44 HardenedOffset = 0x80000000 )
Variables ¶
var ( ErrInvalidSeed = errors.New("XHDWalletAPI: seed produces invalid root key") ErrHardenedPublic = errors.New("XHDWalletAPI: cannot derive hardened child from public key") ErrInvalidKeyLen = errors.New("XHDWalletAPI: invalid key length") ErrTransactionTag = errors.New("XHDWalletAPI: data contains Algorand transaction tag prefix") ErrInvalidPublicKey = errors.New("XHDWalletAPI: invalid public key") ErrOverflow = errors.New("XHDWalletAPI: child key overflows 2^255") ErrInvalidSignature = errors.New("XHDWalletAPI: ed25519 library returned an invalid signature") )
Functions ¶
func BIP39Seed ¶
BIP39Seed derives a 64-byte BIP-39 seed from a mnemonic and optional passphrase. Uses PBKDF2-HMAC-SHA512(mnemonic, "mnemonic"+passphrase, 2048, 64).
func IsHardened ¶
IsHardened returns true if the index is hardened.
func VerifyWithPublicKey ¶
VerifyWithPublicKey verifies an Ed25519 signature.
Types ¶
type AlgoPath ¶
type AlgoPath struct {
// contains filtered or unexported fields
}
Algorand Go SDK compatibile wrapper with simplified path interface
func (AlgoPath) AlgorandAddress ¶
AlgorandAddress derives and returns the Algorand address for this path.
func (AlgoPath) SignBytes ¶
SignBytes signs raw bytes with the key at this path, with no domain separator or prefix applied.
func (AlgoPath) SignData ¶
SignData signs arbitrary data with the key at this path making sure that there is no known Algorand TX type prefix.
func (AlgoPath) SignTransaction ¶
SignTransaction returns the bytes of a signed transaction ready to be broadcasted to the network If the derived address is different than the txn sender's, the derived corresponding address will be assigned as AuthAddr
type DerivationType ¶
type DerivationType int
DerivationType selects the child key derivation algorithm variant.
const ( // Peikert zeros top 9 bits of zL. More secure, max 8 safe derivation levels. Peikert DerivationType = iota // Khovratovich zeros top 32 bits of zL. Standard BIP32-Ed25519 paper. Khovratovich )
type ExtendedKey ¶
type ExtendedKey []byte
ExtendedKey holds an extended private key [kL(32) || kR(32) || c(32)] = 96 bytes, or an extended public key [A(32) || c(32)] = 64 bytes.
func DeriveChildNodePublic ¶
func DeriveChildNodePublic(parent ExtendedKey, index uint32, dt DerivationType) (ExtendedKey, error)
DeriveChildNodePublic derives a child extended public key (soft derivation only).
func DeriveKey ¶
func DeriveKey(rootKey ExtendedKey, path []uint32, isPrivate bool, dt DerivationType) (ExtendedKey, error)
DeriveKey derives an extended key along a BIP-44 path. If isPrivate, returns 96-byte extended private key; else 64-byte extended public key.
func FromSeed ¶
func FromSeed(seed []byte) (ExtendedKey, error)
FromSeed derives the root extended private key from a BIP-39 seed (typically 64 bytes).
Algorithm (BIP32-Ed25519 §V.A):
- k = SHA-512(seed); split into kL, kR
- While bit 253 of kL is set, retry: k = HMAC-SHA512(key=kL, data=kR)
- Clamp kL: clear bits 0-2; clear bit 255; set bit 254
- Chain code c = SHA-256(0x01 || seed)
- Return kL || kR || c (96 bytes)
type KeyContext ¶
type KeyContext uint32
KeyContext represents the BIP-44 coin type for key derivation.
const ( AlgoCoinType KeyContext = 283 // Algorand address coin type (283') Identity KeyContext = 0 // Identity/W3C coin type (0') )
type Wallet ¶
type Wallet struct {
// contains filtered or unexported fields
}
Wallet provides the high-level HD wallet API for Algorand (ARC-0052).
func ImportRootKey ¶
func ImportRootKey(rootKey ExtendedKey, dt DerivationType) (*Wallet, error)
ImportRootKey creates a Wallet from a 96-byte root key.
func NewWalletFromMnemonic ¶
NewWalletFromMnemonic creates an HD wallet from a BIP-39 mnemonic and optional passphrase.
func NewWalletWithDerivation ¶
func NewWalletWithDerivation(seed []byte, dt DerivationType) (*Wallet, error)
NewWalletWithDerivation creates an HD wallet with a specified derivation type.
func (*Wallet) AlgorandAddress ¶
AlgorandAddress returns the Algorand address for the given derivation params.
func (*Wallet) DeriveChildPublicFromExtended ¶
func (w *Wallet) DeriveChildPublicFromExtended(xpk ExtendedKey, index uint32) ([]byte, error)
DeriveChildPublicFromExtended derives the Nth child public key from an extended public key.
func (*Wallet) DeriveKeyExtended ¶
func (w *Wallet) DeriveKeyExtended(path []uint32, isPrivate bool) (ExtendedKey, error)
DeriveKeyExtended derives an extended key along a custom path.
func (*Wallet) ECDH ¶
func (w *Wallet) ECDH(ctx KeyContext, account, change, keyIndex uint32, otherPub []byte, meFirst bool) ([]byte, error)
ECDH computes a shared secret via Elliptic Curve Diffie-Hellman. Ed25519 keys are converted to X25519 internally. Result = SHA-512(sharedPoint || pk1 || pk2) where order depends on meFirst.
func (*Wallet) Export ¶
func (w *Wallet) Export() ExtendedKey
Export returns a copy of the root key.
func (*Wallet) KeyGen ¶
func (w *Wallet) KeyGen(ctx KeyContext, account, change, keyIndex uint32) ([]byte, error)
KeyGen derives the 32-byte Ed25519 public key at m'/44'/coinType'/account'/change/keyIndex.
func (*Wallet) Path ¶
Path returns an AlgoPath for the given account, change, and key index, using the Algorand coin type (283) in the BIP-44 derivation path m/44'/283'/account'/change/keyIndex.
func (*Wallet) Path00 ¶
Path00 returns an AlgoPath for account 0, change 0 at the given key index. This is a convenience method for the most common derivation path m/44'/283'/0'/0/keyIndex.
func (*Wallet) RootKey ¶
func (w *Wallet) RootKey() ExtendedKey
RootKey returns a copy of the root extended private key.
func (*Wallet) SignAlgoTransaction ¶
func (w *Wallet) SignAlgoTransaction(ctx KeyContext, account, change, keyIndex uint32, txBytes []byte) ([]byte, error)
SignAlgoTransaction signs an Algorand transaction's bytes-to-sign (including "TX" prefix).
func (*Wallet) SignBytes ¶
func (w *Wallet) SignBytes(ctx KeyContext, account, change, keyIndex uint32, data []byte) ([]byte, error)
SignBytes signs raw bytes without tag checks.
func (*Wallet) SignData ¶
func (w *Wallet) SignData(ctx KeyContext, account, change, keyIndex uint32, data []byte) ([]byte, error)
SignData signs arbitrary data, rejecting Algorand transaction tag prefixes.