@ledgerhq/hw-app-eth
- Version 6.42.1
- Published
- 2.35 MB
- 14 dependencies
- Apache-2.0 license
Install
npm i @ledgerhq/hw-app-eth
yarn add @ledgerhq/hw-app-eth
pnpm add @ledgerhq/hw-app-eth
Overview
Ledger Hardware Wallet Ethereum Application API
Index
Variables
Functions
Classes
Eth
- clearSignTransaction()
- eth2GetPublicKey()
- eth2SetWithdrawalIndex()
- getAddress()
- getAppConfiguration()
- getChallenge()
- getEIP1024PublicEncryptionKey()
- getEIP1024SharedSecret()
- loadConfig
- provideDomainName()
- provideERC20TokenInformation()
- provideNFTInformation()
- setExternalPlugin()
- setLoadConfig()
- setPlugin()
- signEIP712HashedMessage()
- signEIP712Message()
- signPersonalMessage()
- signTransaction()
- starkGetPublicKey()
- starkProvideQuantum()
- starkProvideQuantum_v2()
- starkSignOrder()
- starkSignOrder_v2()
- starkSignTransfer()
- starkSignTransfer_v2()
- starkUnsafeSign()
- transport
Type Aliases
Variables
variable ledgerService
const ledgerService: LedgerEthTransactionService;
variable nftSelectors
const nftSelectors: any[];
variable tokenSelectors
const tokenSelectors: ERC20_CLEAR_SIGNED_SELECTORS[];
Functions
function getChainIdAsUint32
getChainIdAsUint32: (chainId: BigNumber | number) => number;
for the README
Helper to convert a chainId from a BigNumber to a 4 bytes integer. ChainIds are uint256, but the device limits them to 4 bytes
Parameter chainId
Returns
{Number}
function getParity
getParity: ( vFromDevice: number, chainId: BigNumber, transactionType: Transaction) => 0 | 1;
for the README
Ledger devices are returning v with potentially EIP-155 already applied when using legacy transactions. Because that v value is only represented as a single byte, we need to replicate what would be the overflow happening on the device while applying EIP-155 and recover the original parity.
Parameter vFromDevice
Parameter chainIdUint32
Returns
function getV
getV: ( vFromDevice: number, chainId: BigNumber, transactionType: Transaction) => string;
for the README
Depending on the transaction type you're trying to sign with the device, the v value will be different. For legacy transactions, the v value is used to store the chainId, and that chainId can be a uint256, and some math operation should be applied to it in order to comply with EIP-155 for replay attacks.
In order to prevent breaking changes at the time, the
v
value has been kept as a single byte which forces us to replicate an overflow happening on the device to get the correctv
valueParameter vFromDevice
Parameter chainId
Parameter transactionType
Returns
{string} hexa string of the v value
function hexBuffer
hexBuffer: (str: string) => Buffer;
function intAsHexBytes
intAsHexBytes: (int: number, bytes: number) => string;
for the README
Helper to convert an integer as a hexadecimal string with the right amount of digits to respect the number of bytes given as parameter
Parameter int
Integer
Parameter bytes
Number of bytes it should be represented as (1 byte = 2 caraters)
Returns
The given integer as an hexa string padded with the right number of 0
function maybeHexBuffer
maybeHexBuffer: (str: string | null | undefined) => Buffer | null | undefined;
function mergeResolutions
mergeResolutions: ( resolutionsArray: Partial<LedgerEthTransactionResolution>[]) => LedgerEthTransactionResolution;
function padHexString
padHexString: (str: string) => string;
function safeChunkTransaction
safeChunkTransaction: ( transactionRlp: Buffer, derivationPath: Buffer, transactionType: Transaction) => Buffer[];
for the README
In order to prevent the device from considering a transaction RLP as complete before it actually is we need to split the RLP into chunks which could not be mistaken for a complete transaction. This is true for legacy transaction, where the
v
value is used to store the chainIdParameter transactionRlp
Parameter derivationPath
Parameter transactionType
Returns
{Buffer[]}
function splitPath
splitPath: (path: string) => number[];
Classes
class Eth
class Eth {}
Ethereum API
Example 1
import Eth from "@ledgerhq/hw-app-eth"; const eth = new Eth(transport)
constructor
constructor(transport: Transport, scrambleKey?: string, loadConfig?: LoadConfig);
property loadConfig
loadConfig: LoadConfig;
property transport
transport: Transport;
method clearSignTransaction
clearSignTransaction: ( path: string, rawTxHex: string, resolutionConfig: ResolutionConfig, throwOnError?: boolean) => Promise<{ r: string; s: string; v: string }>;
Helper to get resolution and signature of a transaction in a single method
Parameter path
: the BIP32 path to sign the transaction on
Parameter rawTxHex
: the raw ethereum transaction in hexadecimal to sign
Parameter resolutionConfig
: configuration about what should be clear signed in the transaction
Parameter throwOnError
: optional parameter to determine if a failing resolution of the transaction should throw an error or not
Example 1
const tx = "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080"; // raw tx to sign const result = eth.clearSignTransaction("44'/60'/0'/0/0", tx, { erc20: true, externalPlugins: true, nft: true}); console.log(result);
method eth2GetPublicKey
eth2GetPublicKey: ( path: string, boolDisplay?: boolean) => Promise<{ publicKey: string }>;
get an Ethereum 2 BLS-12 381 public key for a given BIP 32 path.
Parameter path
a path in BIP 32 format boolDisplay optionally enable or not the display an object with a publicKey
Example 1
eth.eth2GetPublicKey("12381/3600/0/0").then(o => o.publicKey)
method eth2SetWithdrawalIndex
eth2SetWithdrawalIndex: (withdrawalIndex: number) => Promise<boolean>;
Set the index of a Withdrawal key used as withdrawal credentials in an ETH 2 deposit contract call signature
It shall be run before the ETH 2 deposit transaction is signed. If not called, the index is set to 0
Parameter withdrawalIndex
index path in the EIP 2334 path m/12381/3600/withdrawalIndex/0 True if the method was executed successfully
method getAddress
getAddress: ( path: string, boolDisplay?: boolean, boolChaincode?: boolean, chainId?: string) => Promise<{ publicKey: string; address: string; chainCode?: string }>;
get Ethereum address for a given BIP 32 path.
Parameter path
a path in BIP 32 format boolDisplay optionally enable or not the display boolChaincode optionally enable or not the chaincode request chainId optionally display the network clearly on a Stax device an object with a publicKey, address and (optionally) chainCode
Example 1
eth.getAddress("44'/60'/0'/0/0").then(o => o.address)
method getAppConfiguration
getAppConfiguration: () => Promise<{ arbitraryDataEnabled: number; erc20ProvisioningNecessary: number; starkEnabled: number; starkv2Supported: number; version: string;}>;
method getChallenge
getChallenge: () => Promise<string>;
Method returning a 4 bytes TLV challenge as an hexa string
Returns
{Promise}
method getEIP1024PublicEncryptionKey
getEIP1024PublicEncryptionKey: ( path: string, boolDisplay?: boolean) => Promise<{ publicKey: string }>;
get a public encryption key on Curve25519 according to EIP 1024
Parameter path
a path in BIP 32 format boolDisplay optionally enable or not the display an object with a publicKey
Example 1
eth.getEIP1024PublicEncryptionKey("44'/60'/0'/0/0").then(o => o.publicKey)
method getEIP1024SharedSecret
getEIP1024SharedSecret: ( path: string, remotePublicKeyHex: string, boolDisplay?: boolean) => Promise<{ sharedSecret: string }>;
get a shared secret on Curve25519 according to EIP 1024
Parameter path
a path in BIP 32 format
Parameter remotePublicKeyHex
remote Curve25519 public key boolDisplay optionally enable or not the display an object with a shared secret
Example 1
eth.getEIP1024SharedSecret("44'/60'/0'/0/0", "87020e80af6e07a6e4697f091eacadb9e7e6629cb7e5a8a371689a3ed53b3d64").then(o => o.sharedSecret)
method provideDomainName
provideDomainName: (data: string) => Promise<boolean>;
provides a domain name (like ENS) to be displayed during transactions in place of the address it is associated to. It shall be run just before a transaction involving the associated address that would be displayed on the device.
Parameter data
an stringied buffer of some TLV encoded data to represent the domain
Returns
a boolean
method provideERC20TokenInformation
provideERC20TokenInformation: (data: string) => Promise<boolean>;
provides a trusted description of an ERC 20 token to associate a contract address with a ticker and number of decimals.
Parameter data
stringified buffer of ERC20 signature
Returns
a boolean
method provideNFTInformation
provideNFTInformation: (data: string) => Promise<boolean>;
provides a trusted description of an NFT to associate a contract address with a collectionName.
Parameter data
stringified buffer of the NFT description
Returns
a boolean
method setExternalPlugin
setExternalPlugin: (payload: string, signature?: string) => Promise<boolean>;
provides the name of a trusted binding of a plugin with a contract address and a supported method selector. This plugin will be called to interpret contract data in the following transaction signing command.
Parameter payload
external plugin data signature optionally signature for the plugin
Returns
a boolean
method setLoadConfig
setLoadConfig: (loadConfig: LoadConfig) => void;
method setPlugin
setPlugin: (data: string) => Promise<boolean>;
provides the name of a trusted binding of a plugin with a contract address and a supported method selector. This plugin will be called to interpret contract data in the following transaction signing command.
Parameter data
stringified buffer of plugin signature
Returns
a boolean
method signEIP712HashedMessage
signEIP712HashedMessage: ( path: string, domainSeparatorHex: string, hashStructMessageHex: string) => Promise<{ v: number; s: string; r: string }>;
Sign a prepared message following web3.eth.signTypedData specification. The host computes the domain separator and hashStruct(message)
Example 1
eth.signEIP712HashedMessage("44'/60'/0'/0/0", Buffer.from("0101010101010101010101010101010101010101010101010101010101010101").toString("hex"), Buffer.from("0202020202020202020202020202020202020202020202020202020202020202").toString("hex")).then(result => { var v = result['v'] - 27; v = v.toString(16); if (v.length < 2) { v = "0" + v; } console.log("Signature 0x" + result['r'] + result['s'] + v); })
method signEIP712Message
signEIP712Message: ( path: string, jsonMessage: EIP712Message, fullImplem?: boolean) => Promise<{ v: number; s: string; r: string }>;
Sign an EIP-721 formatted message following the specification here: https://github.com/LedgerHQ/app-ethereum/blob/develop/doc/ethapp.asc#sign-eth-eip-712 ⚠️ This method is not compatible with nano S (LNS). Make sure to use a try/catch to fallback on the signEIP712HashedMessage method ⚠️
Parameter path
derivationPath
Parameter jsonMessage
message to sign
Parameter fullImplem
use the legacy implementation
Returns
{Promise}
Example 1
eth.signEIP721Message("44'/60'/0'/0/0", { domain: { chainId: 69, name: "Da Domain", verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", version: "1" }, types: { "EIP712Domain": [ { name: "name", type: "string" }, { name: "version", type: "string" }, { name: "chainId", type: "uint256" }, { name: "verifyingContract", type: "address" } ], "Test": [ { name: "contents", type: "string" } ] }, primaryType: "Test", message: {contents: "Hello, Bob!"}, })
method signPersonalMessage
signPersonalMessage: ( path: string, messageHex: string) => Promise<{ v: number; s: string; r: string }>;
You can sign a message according to eth_sign RPC call and retrieve v, r, s given the message and the BIP 32 path of the account to sign.
Example 1
eth.signPersonalMessage("44'/60'/0'/0/0", Buffer.from("test").toString("hex")).then(result => { var v = result['v'] - 27; v = v.toString(16); if (v.length < 2) { v = "0" + v; } console.log("Signature 0x" + result['r'] + result['s'] + v); })
method signTransaction
signTransaction: ( path: string, rawTxHex: string, resolution?: LedgerEthTransactionResolution | null) => Promise<{ s: string; v: string; r: string }>;
You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign.
Parameter path
: the BIP32 path to sign the transaction on
Parameter rawTxHex
: the raw ethereum transaction in hexadecimal to sign
Parameter resolution
: resolution is an object with all "resolved" metadata necessary to allow the device to clear sign information. This includes: ERC20 token information, plugins, contracts, NFT signatures,... You must explicitly provide something to avoid having a warning. By default, you can use Ledger's service or your own resolution service. See services/types.js for the contract. Setting the value to "null" will fallback everything to blind signing but will still allow the device to sign the transaction.
Example 1
import { ledgerService } from "@ledgerhq/hw-app-eth" const tx = "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080"; // raw tx to sign const resolution = await ledgerService.resolveTransaction(tx); const result = eth.signTransaction("44'/60'/0'/0/0", tx, resolution); console.log(result);
method starkGetPublicKey
starkGetPublicKey: (path: string, boolDisplay?: boolean) => Promise<Buffer>;
get Stark public key for a given BIP 32 path.
Parameter path
a path in BIP 32 format boolDisplay optionally enable or not the display the Stark public key
method starkProvideQuantum
starkProvideQuantum: ( operationContract: string | undefined, operationQuantization: BigNumber) => Promise<boolean>;
provide quantization information before singing a deposit or withdrawal Stark powered contract call
It shall be run following a provideERC20TokenInformation call for the given contract
Parameter operationContract
contract address of the token to be transferred (not present for ETH)
Parameter operationQuantization
quantization used for the token to be transferred
method starkProvideQuantum_v2
starkProvideQuantum_v2: ( operationContract: string | undefined, operationQuantizationType: StarkQuantizationType, operationQuantization?: BigNumber, operationMintableBlobOrTokenId?: BigNumber) => Promise<boolean>;
provide quantization information before singing a deposit or withdrawal Stark powered contract call using the Starkex V2 protocol
It shall be run following a provideERC20TokenInformation call for the given contract
Parameter operationContract
contract address of the token to be transferred (not present for ETH)
Parameter operationQuantizationType
quantization type of the token to be transferred operationQuantization quantization used for the token to be transferred (not present for erc 721 or mintable erc 721) operationMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) of the token to be transferred
method starkSignOrder
starkSignOrder: ( path: string, sourceTokenAddress: string | undefined, sourceQuantization: BigNumber, destinationTokenAddress: string | undefined, destinationQuantization: BigNumber, sourceVault: number, destinationVault: number, amountSell: BigNumber, amountBuy: BigNumber, nonce: number, timestamp: number) => Promise<Buffer | { r: string; s: string }>;
sign a Stark order
Parameter path
a path in BIP 32 format sourceTokenAddress contract address of the source token (not present for ETH)
Parameter sourceQuantization
quantization used for the source token destinationTokenAddress contract address of the destination token (not present for ETH)
Parameter destinationQuantization
quantization used for the destination token
Parameter sourceVault
ID of the source vault
Parameter destinationVault
ID of the destination vault
Parameter amountSell
amount to sell
Parameter amountBuy
amount to buy
Parameter nonce
transaction nonce
Parameter timestamp
transaction validity timestamp the signature
method starkSignOrder_v2
starkSignOrder_v2: ( path: string, sourceTokenAddress: string | undefined, sourceQuantizationType: StarkQuantizationType, sourceQuantization: BigNumber | undefined, sourceMintableBlobOrTokenId: BigNumber | undefined, destinationTokenAddress: string | undefined, destinationQuantizationType: StarkQuantizationType, destinationQuantization: BigNumber | undefined, destinationMintableBlobOrTokenId: BigNumber | undefined, sourceVault: number, destinationVault: number, amountSell: BigNumber, amountBuy: BigNumber, nonce: number, timestamp: number) => Promise<Buffer | { r: string; s: string }>;
sign a Stark order using the Starkex V2 protocol
Parameter path
a path in BIP 32 format sourceTokenAddress contract address of the source token (not present for ETH)
Parameter sourceQuantizationType
quantization type used for the source token sourceQuantization quantization used for the source token (not present for erc 721 or mintable erc 721) sourceMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) associated to the source token destinationTokenAddress contract address of the destination token (not present for ETH)
Parameter destinationQuantizationType
quantization type used for the destination token destinationQuantization quantization used for the destination token (not present for erc 721 or mintable erc 721) destinationMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) associated to the destination token
Parameter sourceVault
ID of the source vault
Parameter destinationVault
ID of the destination vault
Parameter amountSell
amount to sell
Parameter amountBuy
amount to buy
Parameter nonce
transaction nonce
Parameter timestamp
transaction validity timestamp the signature
method starkSignTransfer
starkSignTransfer: ( path: string, transferTokenAddress: string | undefined, transferQuantization: BigNumber, targetPublicKey: string, sourceVault: number, destinationVault: number, amountTransfer: BigNumber, nonce: number, timestamp: number) => Promise<Buffer | { r: string; s: string }>;
sign a Stark transfer
Parameter path
a path in BIP 32 format transferTokenAddress contract address of the token to be transferred (not present for ETH)
Parameter transferQuantization
quantization used for the token to be transferred
Parameter targetPublicKey
target Stark public key
Parameter sourceVault
ID of the source vault
Parameter destinationVault
ID of the destination vault
Parameter amountTransfer
amount to transfer
Parameter nonce
transaction nonce
Parameter timestamp
transaction validity timestamp the signature
method starkSignTransfer_v2
starkSignTransfer_v2: ( path: string, transferTokenAddress: string | undefined, transferQuantizationType: StarkQuantizationType, transferQuantization: BigNumber | undefined, transferMintableBlobOrTokenId: BigNumber | undefined, targetPublicKey: string, sourceVault: number, destinationVault: number, amountTransfer: BigNumber, nonce: number, timestamp: number, conditionalTransferAddress?: string, conditionalTransferFact?: BigNumber) => Promise<Buffer | { r: string; s: string }>;
sign a Stark transfer or conditional transfer using the Starkex V2 protocol
Parameter path
a path in BIP 32 format transferTokenAddress contract address of the token to be transferred (not present for ETH)
Parameter transferQuantizationType
quantization type used for the token to be transferred transferQuantization quantization used for the token to be transferred (not present for erc 721 or mintable erc 721) transferMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) associated to the token to be transferred
Parameter targetPublicKey
target Stark public key
Parameter sourceVault
ID of the source vault
Parameter destinationVault
ID of the destination vault
Parameter amountTransfer
amount to transfer
Parameter nonce
transaction nonce
Parameter timestamp
transaction validity timestamp conditionalTransferAddress onchain address of the condition for a conditional transfer conditionalTransferFact fact associated to the condition for a conditional transfer the signature
method starkUnsafeSign
starkUnsafeSign: ( path: string, hash: string) => Promise<Buffer | { r: string; s: string }>;
sign the given hash over the Stark curve It is intended for speed of execution in case an unknown Stark model is pushed and should be avoided as much as possible.
Parameter path
a path in BIP 32 format
Parameter hash
hexadecimal hash to sign the signature
Type Aliases
type StarkQuantizationType
type StarkQuantizationType = | 'eth' | 'erc20' | 'erc721' | 'erc20mintable' | 'erc721mintable';
Package Files (3)
Dependencies (14)
Dev Dependencies (12)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@ledgerhq/hw-app-eth
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@ledgerhq/hw-app-eth)
- HTML<a href="https://www.jsdocs.io/package/@ledgerhq/hw-app-eth"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 5380 ms. - Missing or incorrect documentation? Open an issue for this package.