UNPKG

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