ledger

package module
v0.0.0-...-5e77ee3 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

README

go-ledger-sdk

A golang SDK for using Ledger wallets from golang Spacemesh wallets such as smrepl

Note: this repository is no longer maintained. Please see spacemesh-sdk instead.

API

Enumerate Ledger devices.

/**
 * @param {int} productID USB Product ID filter, 0 - all.
 * @return {[]*HidDevice} Discovered Ledger devices.
 *
 * @example
 * devices := ledger.GetDevices(0)
 * if devices != nil && len(devices) > 0 {
 * 	device := devices[0]
 * 	if err := device.Open(); err == nil {
 * 		...
 * 		device.Close()
 * 	} else {
 * 		fmt.Printf("Open device ERROR: %v\n", err)
 * 	}
 * }
 */
func GetDevices(productID int) []*HidDevice

Open Ledger device for communication.

/**
 * @return {error} Error value.
 *
 * @example
 * devices := ledger.GetDevices(0)
 * if devices != nil && len(devices) > 0 {
 * 	device := devices[0]
 * 	if err := device.Open(); err == nil {
 * 		...
 * 		device.Close()
 * 	} else {
 * 		fmt.Printf("Open device ERROR: %v\n", err)
 * 	}
 * }
 */
func (device *HidDevice) Open() error

Close communication with Ledger device.

/**
 * @example
 * devices := ledger.GetDevices(0)
 * if devices != nil && len(devices) > 0 {
 * 	device := devices[0]
 * 	if err := device.Open(); err == nil {
 * 		...
 * 		device.Close()
 * 	} else {
 * 		fmt.Printf("Open device ERROR: %v\n", err)
 * 	}
 * }
 */
func (device *HidDevice) Close()

Get the ledger app version.

/**
 * @returns {Version} Result object containing the application version number.
 * @return {error} Error value.
 *
 * @example
 * version, err := device.GetVersion()
 * if err != nil {
 * 	fmt.Printf("get version ERROR: %v\n", err)
 * } else {
 * 	fmt.Printf("version: %+v\n", version)
 * }
 */
func (device *HidDevice) GetVersion() (*Version, error)

Get a public key from the specified BIP 32 path.

/**
 * @param {BipPath} path The BIP 32 path indexes. Path must begin with `44'/540'/n'`, and shuld be 5 indexes long.
 * @return {ExtendedPublicKey} The public key with chaincode for the given path.
 * @return {error} Error value.
 *
 * @example
 * publicKey, err := device.GetExtendedPublicKey(ledger.StringToPath("44'/540'/0'/0/0'"))
 * if err != nil {
 * 	fmt.Printf("get public key ERROR: %v\n", err)
 * } else {
 * 	fmt.Printf("public key: %+v\n", publicKey)
 * }
 */
func (device *HidDevice) GetExtendedPublicKey(path BipPath) (*ExtendedPublicKey, error)

Gets an address from the specified BIP 32 path.

/**
 * @param {BipPath} path The BIP 32 path indexes. Path must begin with `44'/540'/0'/0/i`
 * @return {[]byte} The address for the given path.
 * @return {error} Error value.
 *
 * @example
 * address, err := device.GetAddress(ledger.StringToPath("44'/540'/0'/0/0'"))
 * if err != nil {
 * 	fmt.Printf("get address ERROR: %v\n", err)
 * } else {
 * 	fmt.Printf("address: %+v\n", address)
 * }
 */
func (device *HidDevice) GetAddress(path BipPath) ([]byte, error)

Show an address from the specified BIP 32 path for verify.

/**
 * @param {BipPath} indexes The path indexes. Path must begin with `44'/540'/0'/0/i`
 * @return {error} Error value.
 *
 * @example
 * err := device.ShowAddress(ledger.StringToPath("44'/540'/0'/0/1'"))
 * if err != nil {
 * 	fmt.Printf("show address ERROR: %v\n", err)
 * } else {
 * 	fmt.Printf("show address: OK\n")
 * }
 */
func (device *HidDevice) ShowAddress(path BipPath) error

Sign a transaction by the specified BIP 32 path account address.

/**
 * @param {BipPath} path The BIP 32 path indexes. Path must begin with `44'/540'/0'/0/i`
 * @param {[]byte} tx The XDR encoded transaction data, include transaction type
 * @return {[]byte} Signed transaction.
 * @return {error} Error value
 *
 * @example
 * tx := make([]byte, 0)
 * var bin []byte
 * bin, _ = hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000") // network id
 * tx = append(tx, bin...)
 * tx = append(tx, 0) // coin transaction with ed
 * tx = append(tx, uint64_to_buf(1)...) // nonce
 * bin, _ = hex.DecodeString("0000000000000000000000000000000000000000") // recepient
 * tx = append(tx, bin...)
 * tx = append(tx, uint64_to_buf(1000000)...) // gas limit
 * tx = append(tx, uint64_to_buf(1000)...) // gas price
 * tx = append(tx, uint64_to_buf(1000000000000)...) // amount
 * tx = append(tx, publicKey.PublicKey...)
 * 
 * response, err := device.SignTx(ledger.StringToPath("44'/540'/0'/0/0'"), tx)
 * if err != nil {
 * 	fmt.Printf("Verify coin tx ERROR: %v\n", err)
 * } else {
 * 	hash := sha512.Sum512(tx)
 * 	fmt.Printf("Verify coin tx: %v\n", ed25519.Verify(publicKey.PublicKey, hash[:], response[1:65]))
 * }
 */
func (device *HidDevice) SignTx(path BipPath, tx []byte) ([]byte, error)

Documentation

Index

Constants

View Source
const (
	// LedgerUSBVendorID allows identifying USB devices made by Ledger.
	LedgerUSBVendorID = 0x2c97
	// ReadBuffMaxSize is the maximal number of bytes in the read buffer.
	ReadBuffMaxSize = 2048
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BipPath

type BipPath []uint32

BipPath type

func StringToPath

func StringToPath(pathStr string) BipPath

StringToPath Parse string to BIP32 path

type ExtendedPublicKey

type ExtendedPublicKey struct {
	PublicKey []byte
	ChainCode []byte
}

ExtendedPublicKey struct

type HidDevice

type HidDevice struct {
	Info HidDeviceInfo
	// contains filtered or unexported fields
}

HidDevice Wrapper for HIDAPI library

func (*HidDevice) Close

func (device *HidDevice) Close()

Close Close communication with Ledger device.

example devices := ledger.GetDevices(0)

if devices != nil && len(devices) > 0 {
	device := devices[0]
	if err := device.Open(); err == nil {
		...
		device.Close()
	} else {
		fmt.Printf("Open device ERROR: %v\n", err)
	}
}

func (*HidDevice) Exchange

func (device *HidDevice) Exchange(apdu []byte) ([]byte, error)

Exchange Exchange with the device using APDU protocol. param apdu return {[]byte} apdu response return {error} Error value.

func (*HidDevice) GetInfo

func (device *HidDevice) GetInfo() *HidDeviceInfo

GetInfo Get HID device info

func (*HidDevice) Open

func (device *HidDevice) Open() error

Open Open Ledger device for communication.

return {error} Error value.

example devices := ledger.GetDevices(0)

if devices != nil && len(devices) > 0 {
	device := devices[0]
	if err := device.Open(); err == nil {
		...
		device.Close()
	} else {
		fmt.Printf("Open device ERROR: %v\n", err)
	}
}

type HidDeviceInfo

type HidDeviceInfo struct {
	// Platform-specific device path
	Path string
	// Device Vendor
	VendorID uint16
	// Device Product ID
	ProductID uint16
	// Device Release Number in binary-coded decimal,
	// also known as Device Version Number
	ReleaseNumber uint16
	// Usage Page for this Device/Interface
	// (Windows/Mac/hidraw only)
	UsagePage uint16
	// Usage for this Device/Interface
	// (Windows/Mac/hidraw only)
	Usage uint16
	// The USB interface which this logical device
	//  represents.
	//  * Valid on both Linux implementations in all cases.
	//  * Valid on the Windows implementation only if the device
	//    contains more than one interface.
	//  * Valid on the Mac implementation if and only if the device
	//    is a USB HID device.
	InterfaceNumber int
}

HidDeviceInfo hidapi info structure

type IHidDevice

type IHidDevice interface {
	Open() error
	Close()
	Exchange(apdu []byte) ([]byte, error)
	GetInfo() *HidDeviceInfo
}

IHidDevice HID Lenger device interface

type Ledger

type Ledger struct {
	// contains filtered or unexported fields
}

Ledger struct

func GetDevices

func GetDevices(productID int) []*Ledger

GetDevices Enumerate Ledger devices.

param {int} productId USB Product ID filter, 0 - all. return {[]*HidDevice} Discovered Ledger devices.

example devices := ledger.GetDevices(0)

if devices != nil && len(devices) > 0 {
	device := devices[0]
	if err := device.Open(); err == nil {
		...
		device.Close()
	} else {
		fmt.Printf("Open device ERROR: %v\n", err)
	}
}

func NewLedger

func NewLedger(hid IHidDevice) *Ledger

NewLedger Create new Ledger

func (*Ledger) Close

func (device *Ledger) Close()

Close Ledger device

func (*Ledger) GetAddress

func (device *Ledger) GetAddress(path BipPath) ([]byte, error)

GetAddress Gets an address from the specified BIP 32 path.

param {BipPath} path The BIP 32 path indexes. Path must begin with `44'/540'/0'/0/i` return {[]byte} The address for the given path. return {error} Error value.

example address, err := device.GetAddress(ledger.StringToPath("44'/540'/0'/0/0'"))

if err != nil {
	fmt.Printf("get address ERROR: %v\n", err)
} else {

	fmt.Printf("address: %+v\n", address)
}

func (*Ledger) GetExtendedPublicKey

func (device *Ledger) GetExtendedPublicKey(path BipPath) (*ExtendedPublicKey, error)

GetExtendedPublicKey Get a public key from the specified BIP 32 path.

param {BipPath} path The BIP 32 path indexes. Path must begin with `44'/540'/n'`, and shuld be 5 indexes long. return {ExtendedPublicKey} The public key with chaincode for the given path. return {error} Error value.

example publicKey, err := device.GetExtendedPublicKey(ledger.StringToPath("44'/540'/0'/0/0'"))

if err != nil {
	fmt.Printf("get public key ERROR: %v\n", err)
} else {

	fmt.Printf("public key: %+v\n", publicKey)
}

func (*Ledger) GetHidInfo

func (device *Ledger) GetHidInfo() *HidDeviceInfo

GetHidInfo Get HID device info

func (*Ledger) GetVersion

func (device *Ledger) GetVersion() (*Version, error)

GetVersion Returns an object containing the app version.

returns {Version} Result object containing the application version number. return {error} Error value.

example version, err := device.GetVersion()

if err != nil {
	fmt.Printf("get version ERROR: %v\n", err)
} else {

	fmt.Printf("version: %+v\n", version)
}

func (*Ledger) Open

func (device *Ledger) Open() error

Open Ledger device

func (*Ledger) ShowAddress

func (device *Ledger) ShowAddress(path BipPath) error

ShowAddress Show an address from the specified BIP 32 path for verify.

param {BipPath} indexes The path indexes. Path must begin with `44'/540'/0'/0/i` return {error} Error value.

example err := device.ShowAddress(ledger.StringToPath("44'/540'/0'/0/1'"))

if err != nil {
	fmt.Printf("show address ERROR: %v\n", err)
} else {

	fmt.Printf("show address: OK\n")
}

func (*Ledger) SignTx

func (device *Ledger) SignTx(path BipPath, tx []byte) ([]byte, error)

SignTx Sign a transaction by the specified BIP 32 path account address.

param {BipPath} path The BIP 32 path indexes. Path must begin with `44'/540'/0'/0/i` param {[]byte} tx The XDR encoded transaction data, include transaction type return {[]byte} Signed transaction. return {error} Error value

example tx := make([]byte, 0) var bin []byte bin, _ = hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000") // network id tx = append(tx, bin...) tx = append(tx, 0) // coin transaction with ed tx = append(tx, uint64_to_buf(1)...) // nonce bin, _ = hex.DecodeString("0000000000000000000000000000000000000000") // recepient tx = append(tx, bin...) tx = append(tx, uint64_to_buf(1000000)...) // gas limit tx = append(tx, uint64_to_buf(1000)...) // gas price tx = append(tx, uint64_to_buf(1000000000000)...) // amount tx = append(tx, publicKey.PublicKey...)

response, err := device.SignTx(ledger.StringToPath("44'/540'/0'/0/0'"), tx)

if err != nil {
	fmt.Printf("Verify coin tx ERROR: %v\n", err)
} else {

	hash := sha512.Sum512(tx)
	fmt.Printf("Verify coin tx: %v\n", ed25519.Verify(publicKey.PublicKey, hash[:], response[1:65]))
}

type Version

type Version struct {
	Major byte
	Minor byte
	Patch byte
	Flags byte
}

Version struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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