XHDWalletAPI

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

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

View Source
const (
	BIP44Purpose   = 44
	HardenedOffset = 0x80000000
)

Variables

View Source
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

func BIP39Seed(mnemonic, passphrase string) []byte

BIP39Seed derives a 64-byte BIP-39 seed from a mnemonic and optional passphrase. Uses PBKDF2-HMAC-SHA512(mnemonic, "mnemonic"+passphrase, 2048, 64).

func Harden

func Harden(index uint32) uint32

Harden returns the hardened version of an index.

func IsHardened

func IsHardened(index uint32) bool

IsHardened returns true if the index is hardened.

func VerifyWithPublicKey

func VerifyWithPublicKey(signature, message, publicKey []byte) bool

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

func (ap AlgoPath) AlgorandAddress() (types.Address, error)

AlgorandAddress derives and returns the Algorand address for this path.

func (AlgoPath) SignBytes

func (ap AlgoPath) SignBytes(data []byte) ([]byte, error)

SignBytes signs raw bytes with the key at this path, with no domain separator or prefix applied.

func (AlgoPath) SignData

func (ap AlgoPath) SignData(data []byte) ([]byte, error)

SignData signs arbitrary data with the key at this path making sure that there is no known Algorand TX type prefix.

func (AlgoPath) SignTransaction

func (ap AlgoPath) SignTransaction(tx types.Transaction) (txid string, stxBytes []byte, err error)

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):

  1. k = SHA-512(seed); split into kL, kR
  2. While bit 253 of kL is set, retry: k = HMAC-SHA512(key=kL, data=kR)
  3. Clamp kL: clear bits 0-2; clear bit 255; set bit 254
  4. Chain code c = SHA-256(0x01 || seed)
  5. 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 NewWallet

func NewWallet(seed []byte) (*Wallet, error)

NewWallet creates an HD wallet from BIP-39 seed bytes (Peikert default).

func NewWalletFromMnemonic

func NewWalletFromMnemonic(mnemonic, passphrase string) (*Wallet, error)

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

func (w *Wallet) AlgorandAddress(account, change, keyIndex uint32) ([]byte, error)

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

func (w *Wallet) Path(account, change, keyIndex uint32) AlgoPath

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

func (w *Wallet) Path00(keyIndex uint32) AlgoPath

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.

func (*Wallet) Verify

func (w *Wallet) Verify(ctx KeyContext, account, change, keyIndex uint32, signature, message []byte) (bool, error)

Verify verifies a signature using this wallet's derived key.

func (*Wallet) Wipe

func (w *Wallet) Wipe()

Wipe zeros the root key material.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL