account

package
v8.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2020 License: ISC Imports: 24 Imported by: 0

Documentation

Overview

Package account contains code for managing account keypairs, generating a HD wallet as well as saving them securely to disk.

HD wallet functionality per AEX-10 (https://github.com/aeternity/AEXs/blob/master/AEXS/aex-10.md) is implemented in this fashion:

        +----------------------------+
        |   BIP39 Mnemonic           |
        +-------------+--------------+
                      |
        +-------------+--------------+
        |   Binary Seed              |
        +-------------+--------------+
                      |
        +-------------+--------------+
        |   BIP32 Master Key         |
        +----X-----------------------+
            XXX
            XXX
    +-----X---+      +---------+     +----------+
    |         |      |         |     |          |
    |         |      |         |     |          |
    |         |      |         |     |          |
    +---------XX     +---------+     +----------+
                XXX
+---------+     +--XX-----+
|         |     |         |
|         |     |         |
|         |     |         |
+---------+     +-------XX+
                        X                           +---------------------+
                    +----XX+----+                   |                     |
                    |           |                   |                     |
                    | Child Key +------------------->  aeternity Account  |
                    |           |                   |                     |
                    +-----------+                   |                     |
                                                    +---------------------+
Example
mnemonic := "ring defense obey exhaust boat popular surround supreme front lemon monster number"
seed, err := ParseMnemonic(mnemonic)
if err != nil {
	fmt.Println(err)
}

// Derive the subaccount m/44'/457'/3'/0'/1'
key, err := DerivePathFromSeed(seed, 3, 1)
if err != nil {
	fmt.Println(err)
}

// Deriving the aeternity Account from a BIP32 Key is a destructive process
alice, err := BIP32KeyToAeKey(key)
if err != nil {
	fmt.Println(err)
}
fmt.Println(alice.Address)
Output:

Index

Examples

Constants

View Source
const FirstHardenedChild = uint32(0x80000000)

FirstHardenedChild (2^31) denotes when hardened child derivation starts.

Variables

View Source
var (
	// ErrPathNoMaster HD wallet path does not start with m
	ErrPathNoMaster = errors.New("Path must start with m")
	// ErrPathChildMaster HD wallet path contains m in a child node
	ErrPathChildMaster = errors.New("Path contains m as a child node")
	// ErrPathNodeNotNumber HD wallet path node is not a valid uint32 number
	ErrPathNodeNotNumber = errors.New("Path node is not a valid uint32 number")
	// ErrPathNodeNumberTooLarge HD wallet path node is >= 2^31
	ErrPathNodeNumberTooLarge = errors.New("Path node must be less than 2^31")
)
View Source
var ErrHardenedChildPublicKey = errors.New("Can't create hardened child from public key")

ErrHardenedChildPublicKey is returned when trying to create a hardened child from a public key.

View Source
var ErrHardenedOnly = errors.New("ed25519 only works with hardened children")

ErrHardenedOnly is returned when a node in the path is not hardened.

Functions

func CheckWalletExists

func CheckWalletExists(path string) (walletPath string, err error)

CheckWalletExists checks if a file exists at the specified path.

func KeystoreSeal

func KeystoreSeal(account *Account, password string) (j []byte, e error)

KeystoreSeal create an encrypted json keystore with the private key of the account

func ParseMnemonic

func ParseMnemonic(mnemonic string) (masterSeed []byte, err error)

ParseMnemonic uses BIP39 to parse a mnemonic. BIP39 uses a password along with the mnemonic to arrive at the final seed bytearray value, but aeternity only ever uses a blank string for the password, so the point of ParseMnemonic is to reduce ambiguity/confusion.

func StoreToKeyStoreFile

func StoreToKeyStoreFile(account *Account, password, walletName string) (filePath string, err error)

StoreToKeyStoreFile saves an encrypted Account to a JSON file

func Verify

func Verify(address string, message, signature []byte) (valid bool, err error)

Verify a message with the signing/private key

Types

type Account

type Account struct {
	SigningKey ed25519.PrivateKey
	Address    string
}

Account holds the signing/private key and the aeternity account address

func BIP32KeyToAeKey

func BIP32KeyToAeKey(key *Key) (acc *Account, err error)

BIP32KeyToAeKey translates a BIP32 Key into an aeternity Account.

func FromHexString

func FromHexString(hexPrivateKey string) (account *Account, err error)

FromHexString creates an Account from a hexstring

func KeystoreOpen

func KeystoreOpen(data []byte, password string) (account *Account, err error)

KeystoreOpen open and decrypt a keystore

func LoadFromKeyStoreFile

func LoadFromKeyStoreFile(keyFile, password string) (account *Account, err error)

LoadFromKeyStoreFile loads an encrypted Account from a JSON file

func New

func New() (account *Account, err error)

New generates a new Account

func (*Account) Sign

func (account *Account) Sign(message []byte) (signature []byte)

Sign a message with the signing/private key

func (*Account) SigningKeyToHexString

func (account *Account) SigningKeyToHexString() (signingKeyHex string)

SigningKeyToHexString returns the SigningKey as an hex string

type Key

type Key struct {
	Key         []byte // 33 bytes
	ChildNumber uint32 // 4 bytes
	ChainCode   []byte // 32 bytes NEEDED FOR CHILDKEY DERIVATION
	Depth       uint32 // 1 bytes
	IsPrivate   bool   // unserialized
}

Key represents a bip32 extended key, used for deriving subaccounts for HD wallets.

func DerivePathFromSeed

func DerivePathFromSeed(masterSeed []byte, accountIndex, addressIndex uint32) (key *Key, err error)

DerivePathFromSeed derives a BIP32 Key given a seed (usually derived from a mnemonic) and an account index and address index, as standardized by AEX-10.

func NewMasterKey

func NewMasterKey(seed []byte) (*Key, error)

NewMasterKey creates a new master extended key from a seed

func (*Key) NewChildKey

func (key *Key) NewChildKey(childIdx uint32) (*Key, error)

NewChildKey derives a child key from a given parent as outlined by bip32

type Path

type Path struct {
	Elements []PathNode
}

Path represents a parsed HD wallet path

func ParsePath

func ParsePath(p string) (*Path, error)

ParsePath parses a bip32 HD wallet path, e.g. "m/44'/457'/0'/0'/0'"

type PathNode

type PathNode struct {
	Master      bool
	ChildNumber uint32
}

PathNode is an element of an HD wallet path

func (PathNode) Hardened

func (p PathNode) Hardened() bool

Hardened returns true if this path node is hardened

Jump to

Keyboard shortcuts

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