UNPKG

18.6 kBTypeScriptView Raw
1/// <reference types="node" />
2import { BigNumber } from "bignumber.js";
3import type Transport from "@ledgerhq/hw-transport";
4import { EIP712Message } from "@ledgerhq/types-live";
5import { LedgerEthTransactionResolution, LoadConfig, ResolutionConfig } from "./services/types";
6import ledgerService from "./services/ledger";
7export { ledgerService };
8export * from "./utils";
9export type StarkQuantizationType = "eth" | "erc20" | "erc721" | "erc20mintable" | "erc721mintable";
10/**
11 * Ethereum API
12 *
13 * @example
14 * import Eth from "@ledgerhq/hw-app-eth";
15 * const eth = new Eth(transport)
16 */
17export default class Eth {
18 transport: Transport;
19 loadConfig: LoadConfig;
20 setLoadConfig(loadConfig: LoadConfig): void;
21 constructor(transport: Transport, scrambleKey?: string, loadConfig?: LoadConfig);
22 /**
23 * get Ethereum address for a given BIP 32 path.
24 * @param path a path in BIP 32 format
25 * @option boolDisplay optionally enable or not the display
26 * @option boolChaincode optionally enable or not the chaincode request
27 * @option chainId optionally display the network clearly on a Stax device
28 * @return an object with a publicKey, address and (optionally) chainCode
29 * @example
30 * eth.getAddress("44'/60'/0'/0/0").then(o => o.address)
31 */
32 getAddress(path: string, boolDisplay?: boolean, boolChaincode?: boolean, chainId?: string): Promise<{
33 publicKey: string;
34 address: string;
35 chainCode?: string;
36 }>;
37 /**
38 * You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign.
39 *
40 * @param path: the BIP32 path to sign the transaction on
41 * @param rawTxHex: the raw ethereum transaction in hexadecimal to sign
42 * @param 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.
43 * @example
44 import { ledgerService } from "@ledgerhq/hw-app-eth"
45 const tx = "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080"; // raw tx to sign
46 const resolution = await ledgerService.resolveTransaction(tx);
47 const result = eth.signTransaction("44'/60'/0'/0/0", tx, resolution);
48 console.log(result);
49 */
50 signTransaction(path: string, rawTxHex: string, resolution?: LedgerEthTransactionResolution | null): Promise<{
51 s: string;
52 v: string;
53 r: string;
54 }>;
55 /**
56 * Helper to get resolution and signature of a transaction in a single method
57 *
58 * @param path: the BIP32 path to sign the transaction on
59 * @param rawTxHex: the raw ethereum transaction in hexadecimal to sign
60 * @param resolutionConfig: configuration about what should be clear signed in the transaction
61 * @param throwOnError: optional parameter to determine if a failing resolution of the transaction should throw an error or not
62 * @example
63 const tx = "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080"; // raw tx to sign
64 const result = eth.clearSignTransaction("44'/60'/0'/0/0", tx, { erc20: true, externalPlugins: true, nft: true});
65 console.log(result);
66 */
67 clearSignTransaction(path: string, rawTxHex: string, resolutionConfig: ResolutionConfig, throwOnError?: boolean): Promise<{
68 r: string;
69 s: string;
70 v: string;
71 }>;
72 /**
73 */
74 getAppConfiguration(): Promise<{
75 arbitraryDataEnabled: number;
76 erc20ProvisioningNecessary: number;
77 starkEnabled: number;
78 starkv2Supported: number;
79 version: string;
80 }>;
81 /**
82 * 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.
83 * @example
84 eth.signPersonalMessage("44'/60'/0'/0/0", Buffer.from("test").toString("hex")).then(result => {
85 var v = result['v'] - 27;
86 v = v.toString(16);
87 if (v.length < 2) {
88 v = "0" + v;
89 }
90 console.log("Signature 0x" + result['r'] + result['s'] + v);
91 })
92 */
93 signPersonalMessage(path: string, messageHex: string): Promise<{
94 v: number;
95 s: string;
96 r: string;
97 }>;
98 /**
99 * Sign a prepared message following web3.eth.signTypedData specification. The host computes the domain separator and hashStruct(message)
100 * @example
101 eth.signEIP712HashedMessage("44'/60'/0'/0/0", Buffer.from("0101010101010101010101010101010101010101010101010101010101010101").toString("hex"), Buffer.from("0202020202020202020202020202020202020202020202020202020202020202").toString("hex")).then(result => {
102 var v = result['v'] - 27;
103 v = v.toString(16);
104 if (v.length < 2) {
105 v = "0" + v;
106 }
107 console.log("Signature 0x" + result['r'] + result['s'] + v);
108 })
109 */
110 signEIP712HashedMessage(path: string, domainSeparatorHex: string, hashStructMessageHex: string): Promise<{
111 v: number;
112 s: string;
113 r: string;
114 }>;
115 /**
116 * Sign an EIP-721 formatted message following the specification here:
117 * https://github.com/LedgerHQ/app-ethereum/blob/develop/doc/ethapp.asc#sign-eth-eip-712
118 * ⚠️ This method is not compatible with nano S (LNS). Make sure to use a try/catch to fallback on the signEIP712HashedMessage method ⚠️
119 @example
120 eth.signEIP721Message("44'/60'/0'/0/0", {
121 domain: {
122 chainId: 69,
123 name: "Da Domain",
124 verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
125 version: "1"
126 },
127 types: {
128 "EIP712Domain": [
129 { name: "name", type: "string" },
130 { name: "version", type: "string" },
131 { name: "chainId", type: "uint256" },
132 { name: "verifyingContract", type: "address" }
133 ],
134 "Test": [
135 { name: "contents", type: "string" }
136 ]
137 },
138 primaryType: "Test",
139 message: {contents: "Hello, Bob!"},
140 })
141 *
142 * @param {String} path derivationPath
143 * @param {Object} jsonMessage message to sign
144 * @param {Boolean} fullImplem use the legacy implementation
145 * @returns {Promise}
146 */
147 signEIP712Message(path: string, jsonMessage: EIP712Message, fullImplem?: boolean): Promise<{
148 v: number;
149 s: string;
150 r: string;
151 }>;
152 /**
153 * Method returning a 4 bytes TLV challenge as an hexa string
154 *
155 * @returns {Promise<string>}
156 */
157 getChallenge(): Promise<string>;
158 /**
159 * get Stark public key for a given BIP 32 path.
160 * @param path a path in BIP 32 format
161 * @option boolDisplay optionally enable or not the display
162 * @return the Stark public key
163 */
164 starkGetPublicKey(path: string, boolDisplay?: boolean): Promise<Buffer>;
165 /**
166 * sign a Stark order
167 * @param path a path in BIP 32 format
168 * @option sourceTokenAddress contract address of the source token (not present for ETH)
169 * @param sourceQuantization quantization used for the source token
170 * @option destinationTokenAddress contract address of the destination token (not present for ETH)
171 * @param destinationQuantization quantization used for the destination token
172 * @param sourceVault ID of the source vault
173 * @param destinationVault ID of the destination vault
174 * @param amountSell amount to sell
175 * @param amountBuy amount to buy
176 * @param nonce transaction nonce
177 * @param timestamp transaction validity timestamp
178 * @return the signature
179 */
180 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 | {
181 r: string;
182 s: string;
183 }>;
184 /**
185 * sign a Stark order using the Starkex V2 protocol
186 * @param path a path in BIP 32 format
187 * @option sourceTokenAddress contract address of the source token (not present for ETH)
188 * @param sourceQuantizationType quantization type used for the source token
189 * @option sourceQuantization quantization used for the source token (not present for erc 721 or mintable erc 721)
190 * @option sourceMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) associated to the source token
191 * @option destinationTokenAddress contract address of the destination token (not present for ETH)
192 * @param destinationQuantizationType quantization type used for the destination token
193 * @option destinationQuantization quantization used for the destination token (not present for erc 721 or mintable erc 721)
194 * @option destinationMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) associated to the destination token
195 * @param sourceVault ID of the source vault
196 * @param destinationVault ID of the destination vault
197 * @param amountSell amount to sell
198 * @param amountBuy amount to buy
199 * @param nonce transaction nonce
200 * @param timestamp transaction validity timestamp
201 * @return the signature
202 */
203 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 | {
204 r: string;
205 s: string;
206 }>;
207 /**
208 * sign a Stark transfer
209 * @param path a path in BIP 32 format
210 * @option transferTokenAddress contract address of the token to be transferred (not present for ETH)
211 * @param transferQuantization quantization used for the token to be transferred
212 * @param targetPublicKey target Stark public key
213 * @param sourceVault ID of the source vault
214 * @param destinationVault ID of the destination vault
215 * @param amountTransfer amount to transfer
216 * @param nonce transaction nonce
217 * @param timestamp transaction validity timestamp
218 * @return the signature
219 */
220 starkSignTransfer(path: string, transferTokenAddress: string | undefined, transferQuantization: BigNumber, targetPublicKey: string, sourceVault: number, destinationVault: number, amountTransfer: BigNumber, nonce: number, timestamp: number): Promise<Buffer | {
221 r: string;
222 s: string;
223 }>;
224 /**
225 * sign a Stark transfer or conditional transfer using the Starkex V2 protocol
226 * @param path a path in BIP 32 format
227 * @option transferTokenAddress contract address of the token to be transferred (not present for ETH)
228 * @param transferQuantizationType quantization type used for the token to be transferred
229 * @option transferQuantization quantization used for the token to be transferred (not present for erc 721 or mintable erc 721)
230 * @option transferMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) associated to the token to be transferred
231 * @param targetPublicKey target Stark public key
232 * @param sourceVault ID of the source vault
233 * @param destinationVault ID of the destination vault
234 * @param amountTransfer amount to transfer
235 * @param nonce transaction nonce
236 * @param timestamp transaction validity timestamp
237 * @option conditionalTransferAddress onchain address of the condition for a conditional transfer
238 * @option conditionalTransferFact fact associated to the condition for a conditional transfer
239 * @return the signature
240 */
241 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 | {
242 r: string;
243 s: string;
244 }>;
245 /**
246 * provide quantization information before singing a deposit or withdrawal Stark powered contract call
247 *
248 * It shall be run following a provideERC20TokenInformation call for the given contract
249 *
250 * @param operationContract contract address of the token to be transferred (not present for ETH)
251 * @param operationQuantization quantization used for the token to be transferred
252 */
253 starkProvideQuantum(operationContract: string | undefined, operationQuantization: BigNumber): Promise<boolean>;
254 /**
255 * provide quantization information before singing a deposit or withdrawal Stark powered contract call using the Starkex V2 protocol
256 *
257 * It shall be run following a provideERC20TokenInformation call for the given contract
258 *
259 * @param operationContract contract address of the token to be transferred (not present for ETH)
260 * @param operationQuantizationType quantization type of the token to be transferred
261 * @option operationQuantization quantization used for the token to be transferred (not present for erc 721 or mintable erc 721)
262 * @option operationMintableBlobOrTokenId mintable blob (mintable erc 20 / mintable erc 721) or token id (erc 721) of the token to be transferred
263 */
264 starkProvideQuantum_v2(operationContract: string | undefined, operationQuantizationType: StarkQuantizationType, operationQuantization?: BigNumber, operationMintableBlobOrTokenId?: BigNumber): Promise<boolean>;
265 /**
266 * sign the given hash over the Stark curve
267 * It is intended for speed of execution in case an unknown Stark model is pushed and should be avoided as much as possible.
268 * @param path a path in BIP 32 format
269 * @param hash hexadecimal hash to sign
270 * @return the signature
271 */
272 starkUnsafeSign(path: string, hash: string): Promise<Buffer | {
273 r: string;
274 s: string;
275 }>;
276 /**
277 * get an Ethereum 2 BLS-12 381 public key for a given BIP 32 path.
278 * @param path a path in BIP 32 format
279 * @option boolDisplay optionally enable or not the display
280 * @return an object with a publicKey
281 * @example
282 * eth.eth2GetPublicKey("12381/3600/0/0").then(o => o.publicKey)
283 */
284 eth2GetPublicKey(path: string, boolDisplay?: boolean): Promise<{
285 publicKey: string;
286 }>;
287 /**
288 * Set the index of a Withdrawal key used as withdrawal credentials in an ETH 2 deposit contract call signature
289 *
290 * It shall be run before the ETH 2 deposit transaction is signed. If not called, the index is set to 0
291 *
292 * @param withdrawalIndex index path in the EIP 2334 path m/12381/3600/withdrawalIndex/0
293 * @return True if the method was executed successfully
294 */
295 eth2SetWithdrawalIndex(withdrawalIndex: number): Promise<boolean>;
296 /**
297 * get a public encryption key on Curve25519 according to EIP 1024
298 * @param path a path in BIP 32 format
299 * @option boolDisplay optionally enable or not the display
300 * @return an object with a publicKey
301 * @example
302 * eth.getEIP1024PublicEncryptionKey("44'/60'/0'/0/0").then(o => o.publicKey)
303 */
304 getEIP1024PublicEncryptionKey(path: string, boolDisplay?: boolean): Promise<{
305 publicKey: string;
306 }>;
307 /**
308 * get a shared secret on Curve25519 according to EIP 1024
309 * @param path a path in BIP 32 format
310 * @param remotePublicKeyHex remote Curve25519 public key
311 * @option boolDisplay optionally enable or not the display
312 * @return an object with a shared secret
313 * @example
314 * eth.getEIP1024SharedSecret("44'/60'/0'/0/0", "87020e80af6e07a6e4697f091eacadb9e7e6629cb7e5a8a371689a3ed53b3d64").then(o => o.sharedSecret)
315 */
316 getEIP1024SharedSecret(path: string, remotePublicKeyHex: string, boolDisplay?: boolean): Promise<{
317 sharedSecret: string;
318 }>;
319 /**
320 * provides a trusted description of an ERC 20 token to associate a contract address with a ticker and number of decimals.
321 *
322 * @param data stringified buffer of ERC20 signature
323 * @returns a boolean
324 */
325 provideERC20TokenInformation(data: string): Promise<boolean>;
326 /**
327 * 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.
328 *
329 * @param payload external plugin data
330 * @option signature optionally signature for the plugin
331 * @returns a boolean
332 */
333 setExternalPlugin(payload: string, signature?: string): Promise<boolean>;
334 /**
335 * 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.
336 *
337 * @param data stringified buffer of plugin signature
338 * @returns a boolean
339 */
340 setPlugin(data: string): Promise<boolean>;
341 /**
342 * provides a trusted description of an NFT to associate a contract address with a collectionName.
343 *
344 * @param data stringified buffer of the NFT description
345 * @returns a boolean
346 */
347 provideNFTInformation(data: string): Promise<boolean>;
348 /**
349 * 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.
350 *
351 * @param data an stringied buffer of some TLV encoded data to represent the domain
352 * @returns a boolean
353 */
354 provideDomainName(data: string): Promise<boolean>;
355}
356//# sourceMappingURL=Eth.d.ts.map
\No newline at end of file