ledger

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: LGPL-3.0 Imports: 10 Imported by: 11

Documentation

Overview

Package ledger provides hardware wallet integration for TRON signing.

Index

Constants

This section is empty.

Variables

View Source
var DEBUG bool

DEBUG enables verbose HID frame logging when true.

Functions

func GetAddress

func GetAddress() string

GetAddress returns the TRON address from the connected Ledger device. Deprecated: Use OpenDevice() and call GetAddress() on the Device instead.

func SignTx

func SignTx(tx []byte) ([]byte, error)

SignTx signs the given transaction bytes using a newly opened Ledger device. Deprecated: Use OpenDevice() and call SignTransaction() on the Device instead.

Types

type APDU

type APDU struct {
	CLA     byte
	INS     byte
	P1, P2  byte
	Payload []byte
}

APDU represents an Application Protocol Data Unit for Ledger communication.

type Device added in v0.26.0

type Device interface {
	// GetAddress returns the TRON address from the device.
	GetAddress() (string, error)
	// SignTransaction signs a raw transaction and returns the signature.
	SignTransaction(ctx context.Context, tx []byte) ([]byte, error)
	// Close releases the device connection.
	Close() error
}

Device abstracts a hardware wallet for testability and multi-device support.

func OpenDevice added in v0.26.0

func OpenDevice() (Device, error)

OpenDevice opens a Ledger Nano S and returns it as a Device. This is the recommended entry point for callers.

type ErrCode

type ErrCode uint16

ErrCode represents a Ledger APDU error status code.

func (ErrCode) Error

func (c ErrCode) Error() string

type MockDevice added in v0.26.0

type MockDevice struct {
	// GetAddressFn is called by GetAddress. When nil, the stored Address
	// and Err fields are returned instead.
	GetAddressFn func() (string, error)
	// SignTransactionFn is called by SignTransaction. When nil, the stored
	// Signature and Err fields are returned instead.
	SignTransactionFn func(ctx context.Context, tx []byte) ([]byte, error)
	// CloseFn is called by Close. When nil, nil is returned.
	CloseFn func() error

	// Address is returned by GetAddress when GetAddressFn is nil.
	Address string
	// Signature is returned by SignTransaction when SignTransactionFn is nil.
	Signature []byte
	// Err is returned by both GetAddress and SignTransaction when their
	// respective function fields are nil.
	Err error
}

MockDevice is a test double that implements the Device interface. Callers can set the function fields to control return values; unset fields return zero values.

func (*MockDevice) Close added in v0.26.0

func (m *MockDevice) Close() error

Close calls CloseFn if set, otherwise returns nil.

func (*MockDevice) GetAddress added in v0.26.0

func (m *MockDevice) GetAddress() (string, error)

GetAddress returns the configured address or calls GetAddressFn.

func (*MockDevice) SignTransaction added in v0.26.0

func (m *MockDevice) SignTransaction(ctx context.Context, tx []byte) ([]byte, error)

SignTransaction returns the configured signature or calls SignTransactionFn.

type NanoS

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

NanoS represents a connection to a Ledger Nano S hardware wallet.

func OpenNanoS

func OpenNanoS() (*NanoS, error)

OpenNanoS detects and opens a connection to a Ledger Nano S device.

func (*NanoS) Close added in v0.26.0

func (n *NanoS) Close() error

Close releases the underlying HID device connection.

func (*NanoS) Exchange

func (n *NanoS) Exchange(cmd byte, p1, p2 byte, data []byte) (resp []byte, err error)

Exchange sends an APDU command to the Ledger and returns the response.

func (*NanoS) GetAddress

func (n *NanoS) GetAddress() (addr string, err error)

GetAddress returns the TRON address from the Ledger device.

func (*NanoS) GetVersion

func (n *NanoS) GetVersion() (version string, err error)

GetVersion returns the TRON app version running on the Ledger device.

func (*NanoS) SignTransaction added in v0.26.0

func (n *NanoS) SignTransaction(_ context.Context, tx []byte) ([]byte, error)

SignTransaction signs the given raw transaction bytes using the Ledger device. It returns the 65-byte signature after verifying via ecrecover.

func (*NanoS) SignTxn

func (n *NanoS) SignTxn(txn []byte) (sig [signatureSize]byte, err error)

SignTxn signs a raw transaction using the Ledger device and returns the 65-byte signature.

Jump to

Keyboard shortcuts

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