CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ledgerhq--hw-app-eth

Ledger Hardware Wallet Ethereum Application API that enables developers to integrate Ledger wallet support into Ethereum applications for address generation, transaction signing, message signing, and various Ethereum operations.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

services-utilities.mddocs/

Services and Utilities

Service layer for transaction resolution and utility functions for data processing. The library includes comprehensive services for resolving ERC20 tokens, NFTs, contract plugins, and various utility functions for transaction and data manipulation.

Capabilities

Ledger Service

Main transaction resolution service that automatically resolves transaction metadata for clear signing.

/**
 * Main ledger service for transaction resolution
 */
const ledgerService: LedgerEthTransactionService;

interface LedgerEthTransactionService {
  /**
   * Resolve transaction metadata for clear signing display
   * @param rawTxHex - Raw transaction data as hex string  
   * @param loadConfig - Service configuration for external APIs
   * @param resolutionConfig - Configuration for resolution features
   * @returns Resolved transaction metadata
   */
  resolveTransaction(
    rawTxHex: string,
    loadConfig: LoadConfig,
    resolutionConfig: ResolutionConfig
  ): Promise<LedgerEthTransactionResolution>;
}

Usage Example:

import { ledgerService } from "@ledgerhq/hw-app-eth";

// Resolve transaction with all features enabled
const resolution = await ledgerService.resolveTransaction(
  rawTxHex,
  {
    nftExplorerBaseURL: "https://nft-api.com",
    pluginBaseURL: "https://plugin-api.com",
    cryptoassetsBaseURL: "https://crypto-api.com"
  },
  {
    erc20: true,
    nft: true,
    externalPlugins: true,
    uniswapV3: true
  }
);

console.log(resolution.erc20Tokens); // Resolved ERC20 addresses
console.log(resolution.nfts); // Resolved NFT contract addresses
console.log(resolution.externalPlugin); // Plugin configurations

ERC20 Services

Services for resolving ERC20 token information and metadata.

/**
 * Find ERC20 signature information for a given chain
 * @param loadConfig - Service configuration
 * @param chainId - Blockchain chain ID
 * @returns ERC20 signatures blob or null
 */
function findERC20SignaturesInfo(
  loadConfig: LoadConfig,
  chainId: number
): Promise<string | null | undefined>;

/**
 * Get token information by contract address and chain ID
 * @param contractAddress - ERC20 contract address
 * @param chainId - Blockchain chain ID  
 * @param erc20SignaturesBlob - ERC20 signatures data blob
 * @returns Token information or null
 */
function byContractAddressAndChainId(
  contractAddress: string,
  chainId: number,
  erc20SignaturesBlob: string | null | undefined
): TokenInfo | null | undefined;

interface TokenInfo {
  /** Token contract address */
  contractAddress: string;
  /** Token decimal places */
  decimals: number;
  /** Token symbol (e.g., "USDC") */
  symbol: string;
  /** Token full name */
  name: string;
  /** Chain ID where token exists */
  chainId: number;
}

Usage Examples:

// Get ERC20 signatures for Ethereum mainnet
const signatures = await findERC20SignaturesInfo(loadConfig, 1);

// Look up USDC token information
const usdcInfo = byContractAddressAndChainId(
  "0xA0b86a33E6776a9fEddac4F0c0bfB3F4a9d7d3e7",
  1, // Ethereum mainnet
  signatures
);

if (usdcInfo) {
  console.log(usdcInfo.symbol); // "USDC"
  console.log(usdcInfo.decimals); // 6
  console.log(usdcInfo.name); // "USD Coin"
}

NFT Services

Services for resolving NFT collection information and loading NFT-specific plugins.

/**
 * Get NFT collection information
 * @param contractAddress - NFT contract address
 * @param chainId - Blockchain chain ID
 * @param loadConfig - Service configuration
 * @returns NFT collection metadata
 */
function getNFTInfo(
  contractAddress: string,
  chainId: number,
  loadConfig: LoadConfig
): Promise<any>;

/**
 * Load NFT-specific plugin data
 * @param contractAddress - NFT contract address
 * @param selector - Method selector (4-byte function signature)
 * @param chainId - Blockchain chain ID
 * @param loadConfig - Service configuration
 * @returns Plugin data or null
 */
function loadNftPlugin(
  contractAddress: string,
  selector: string,
  chainId: number,
  loadConfig: LoadConfig
): Promise<string | null>;

Usage Examples:

// Get NFT collection information
const nftInfo = await getNFTInfo(
  "0x1234567890123456789012345678901234567890", // CryptoPunks
  1, // Ethereum mainnet
  loadConfig
);
console.log(nftInfo.name); // Collection name
console.log(nftInfo.symbol); // Collection symbol

// Load NFT plugin for specific method
const pluginData = await loadNftPlugin(
  "0x1234567890123456789012345678901234567890",
  "0xa22cb465", // setApprovalForAll selector
  1,
  loadConfig
);

Contract Services

Services for loading contract method information and external plugins.

/**
 * Load contract method information for clear signing
 * @param contractAddress - Contract address
 * @param selector - Method selector (4-byte function signature)
 * @param chainId - Blockchain chain ID
 * @param loadConfig - Service configuration
 * @returns Contract method metadata
 */
function loadInfosForContractMethod(
  contractAddress: string,
  selector: string,
  chainId: number,
  loadConfig: LoadConfig
): Promise<any>;

Usage Example:

// Load contract method information
const methodInfo = await loadInfosForContractMethod(
  "0xContractAddress123...",
  "0x095ea7b3", // approve(address,uint256) selector
  1, // Ethereum mainnet
  loadConfig
);
console.log(methodInfo); // Method signature and parameter info

Load Configuration Service

Service for merging and managing load configurations.

/**
 * Get merged load configuration with defaults
 * @param userLoadConfig - User-provided configuration (optional)
 * @returns Complete load configuration with defaults
 */
function getLoadConfig(userLoadConfig?: LoadConfig): LoadConfig;

Usage Example:

// Get default configuration
const defaultConfig = getLoadConfig();

// Merge with user configuration
const customConfig = getLoadConfig({
  nftExplorerBaseURL: "https://custom-nft-api.com",
  pluginBaseURL: null // Disable plugin loading
});

Utility Functions

String and Buffer Utilities

Utilities for hex string processing and buffer conversion.

/**
 * Pad hex string to even length
 * @param str - Hex string to pad
 * @returns Padded hex string
 */
function padHexString(str: string): string;

/**
 * Convert hex string to buffer, handling 0x prefix
 * @param str - Hex string (with or without 0x prefix)
 * @returns Buffer representation
 */
function hexBuffer(str: string): Buffer;

/**
 * Safe hex buffer conversion with null handling
 * @param str - Hex string, null, or undefined
 * @returns Buffer, null, or undefined
 */
function maybeHexBuffer(str: string | null | undefined): Buffer | null | undefined;

/**
 * Convert integer to hex string with specified byte length
 * @param int - Integer value
 * @param bytes - Number of bytes for output
 * @returns Hex string with specified length
 */
function intAsHexBytes(int: number, bytes: number): string;

Usage Examples:

// Pad hex string
const padded = padHexString("abc"); // "0abc"

// Convert hex to buffer
const buffer = hexBuffer("0x1234abcd"); // Buffer with hex data
const buffer2 = hexBuffer("1234abcd"); // Works without 0x prefix

// Safe conversion with null handling
const result = maybeHexBuffer(null); // null
const result2 = maybeHexBuffer("0x1234"); // Buffer

// Integer to hex bytes
const hex = intAsHexBytes(255, 2); // "00ff"
const hex2 = intAsHexBytes(65535, 4); // "0000ffff"

Path Utilities

Utilities for BIP32 derivation path processing.

/**
 * Parse BIP32 derivation path into numeric array
 * @param path - BIP32 path string (e.g., "44'/60'/0'/0/0")
 * @returns Array of path components as numbers
 */
function splitPath(path: string): number[];

Usage Example:

// Parse BIP32 path
const pathComponents = splitPath("44'/60'/0'/0/0");
console.log(pathComponents); // [2147483692, 2147483708, 2147483648, 0, 0]

// Parse complex path
const components = splitPath("44'/60'/1'/5/10");
console.log(components); // [2147483692, 2147483708, 2147483649, 5, 10]

Transaction Utilities

Utilities for transaction signature calculation and processing.

/**
 * Calculate transaction signature V value
 * @param vFromDevice - V value from hardware wallet
 * @param chainId - Blockchain chain ID
 * @param transactionType - Transaction type (legacy, EIP-1559, etc.)
 * @returns Calculated V value as string
 */
function getV(
  vFromDevice: number,
  chainId: BigNumber,
  transactionType: Transaction["type"]
): string;

/**
 * Extract signature parity from V value
 * @param vFromDevice - V value from hardware wallet
 * @param chainId - Blockchain chain ID
 * @param transactionType - Transaction type
 * @returns Signature parity (0 or 1)
 */
function getParity(
  vFromDevice: number,
  chainId: BigNumber,
  transactionType: Transaction["type"]
): 0 | 1;

/**
 * Convert chain ID to 4-byte integer
 * @param chainId - Chain ID as BigNumber or number
 * @returns Chain ID as 4-byte integer
 */
function getChainIdAsUint32(chainId: BigNumber | number): number;

/**
 * Split transaction for device transmission
 * @param transactionRlp - RLP-encoded transaction
 * @param derivationPath - BIP32 path as buffer
 * @param transactionType - Transaction type
 * @returns Array of transaction chunks
 */
function safeChunkTransaction(
  transactionRlp: Buffer,
  derivationPath: Buffer,
  transactionType: Transaction["type"]
): Buffer[];

Usage Examples:

import { BigNumber } from "bignumber.js";

// Calculate V value for Ethereum mainnet
const vValue = getV(27, new BigNumber(1), "legacy");
console.log(vValue); // "0x1c"

// Get signature parity
const parity = getParity(28, new BigNumber(1), "legacy");
console.log(parity); // 1

// Convert chain ID to uint32
const chainIdUint32 = getChainIdAsUint32(new BigNumber(1));
console.log(chainIdUint32); // 1

// Split large transaction for device
const chunks = safeChunkTransaction(
  transactionRlpBuffer,
  pathBuffer,
  "eip1559"
);
console.log(chunks.length); // Number of chunks needed

Resolution Utilities

Utilities for merging transaction resolution metadata.

/**
 * Merge multiple transaction resolutions into one
 * @param resolutionsArray - Array of partial resolution objects
 * @returns Merged complete resolution object
 */
function mergeResolutions(
  resolutionsArray: Partial<LedgerEthTransactionResolution>[]
): LedgerEthTransactionResolution;

Usage Example:

// Merge multiple resolution sources
const erc20Resolution = { erc20Tokens: ["0x123..."], nfts: [], externalPlugin: [], plugin: [], domains: [] };
const nftResolution = { erc20Tokens: [], nfts: ["0x456..."], externalPlugin: [], plugin: [], domains: [] };

const mergedResolution = mergeResolutions([erc20Resolution, nftResolution]);
console.log(mergedResolution.erc20Tokens); // ["0x123..."]
console.log(mergedResolution.nfts); // ["0x456..."]

Method Selector Constants

Pre-defined method selectors for common operations.

/** ERC20 clear-signed method selectors */
const tokenSelectors: string[];
const ERC20_CLEAR_SIGNED_SELECTORS: readonly string[];

/** ERC721 clear-signed method selectors */
const ERC721_CLEAR_SIGNED_SELECTORS: readonly string[];

/** ERC1155 clear-signed method selectors */  
const ERC1155_CLEAR_SIGNED_SELECTORS: readonly string[];

/** NFT method selectors (ERC721 + ERC1155) */
const nftSelectors: string[];

/** DApp-specific method selectors */
const DAPP_SELECTORS: readonly string[];

Usage Examples:

// Check if method is ERC20 clear-signed
const isERC20Method = ERC20_CLEAR_SIGNED_SELECTORS.includes("0x095ea7b3"); // approve

// Check if method is NFT-related
const isNFTMethod = nftSelectors.includes("0xa22cb465"); // setApprovalForAll

// Get all supported clear-signed selectors
const allSelectors = [
  ...ERC20_CLEAR_SIGNED_SELECTORS,
  ...ERC721_CLEAR_SIGNED_SELECTORS,
  ...ERC1155_CLEAR_SIGNED_SELECTORS,
  ...DAPP_SELECTORS
];

docs

advanced-features.md

core-operations.md

index.md

message-signing.md

services-utilities.md

starkex.md

transaction-signing.md

tile.json