Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

EIP 1024 support #786

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages/hw-app-eth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ Ledger Hardware Wallet ETH JavaScript bindings.
* [Examples](#examples-5)
* [eth2SetWithdrawalIndex](#eth2setwithdrawalindex)
* [Parameters](#parameters-14)
* [getEIP1024PublicEncryptionKey](#geteip1024publicencryptionkey)
* [Parameters](#parameters-15)
* [Examples](#examples-6)
* [getEIP1024SharedSecret](#geteip1024sharedsecret)
* [Parameters](#parameters-16)
* [Examples](#examples-7)
* [loadInfosForContractMethod](#loadinfosforcontractmethod)
* [Parameters](#parameters-15)
* [Parameters](#parameters-17)
* [byContractAddressAndChainId](#bycontractaddressandchainid)
* [Parameters](#parameters-16)
* [Parameters](#parameters-18)
* [list](#list)
* [ResolutionConfig](#resolutionconfig)
* [Properties](#properties)
Expand Down Expand Up @@ -332,6 +338,41 @@ It shall be run before the ETH 2 deposit transaction is signed. If not called, t

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>** True if the method was executed successfully

#### getEIP1024PublicEncryptionKey

get a public encryption key on Curve25519 according to EIP 1024

##### Parameters

* `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** a path in BIP 32 format
* `boolDisplay` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**

##### Examples

```javascript
eth.getEIP1024PublicEncryptionKey("44'/60'/0'/0/0").then(o => o.publicKey)
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{publicKey: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>** an object with a publicKey

#### getEIP1024SharedSecret

get a shared secret on Curve25519 according to EIP 1024

##### Parameters

* `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** a path in BIP 32 format
* `remotePublicKeyHex` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** remote Curve25519 public key
* `boolDisplay` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**

##### Examples

```javascript
eth.getEIP1024SharedSecret("44'/60'/0'/0/0", "87020e80af6e07a6e4697f091eacadb9e7e6629cb7e5a8a371689a3ed53b3d64").then(o => o.sharedSecret)
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{sharedSecret: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>** an object with a shared secret

### loadInfosForContractMethod

Retrieve the metadatas a given contract address and a method selector
Expand Down
66 changes: 66 additions & 0 deletions packages/hw-app-eth/src/Eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export default class Eth {
"eth2SetWithdrawalIndex",
"setExternalPlugin",
"setPlugin",
"getEIP1024PublicEncryptionKey",
"getEIP1024SharedSecret",
],
scrambleKey
);
Expand Down Expand Up @@ -1133,6 +1135,70 @@ export default class Eth {
);
}

/**
* get a public encryption key on Curve25519 according to EIP 1024
* @param path a path in BIP 32 format
* @option boolDisplay optionally enable or not the display
* @return an object with a publicKey
* @example
* eth.getEIP1024PublicEncryptionKey("44'/60'/0'/0/0").then(o => o.publicKey)
*/
getEIP1024PublicEncryptionKey(
path: string,
boolDisplay?: boolean
): Promise<{
publicKey: string;
}> {
const paths = splitPath(path);
const buffer = Buffer.alloc(1 + paths.length * 4);
buffer[0] = paths.length;
paths.forEach((element, index) => {
buffer.writeUInt32BE(element, 1 + 4 * index);
});
return this.transport
.send(0xe0, 0x18, boolDisplay ? 0x01 : 0x00, 0x00, buffer)
.then((response) => {
return {
publicKey: response.slice(0, -2).toString("hex"),
};
});
}

/**
* get a shared secret on Curve25519 according to EIP 1024
* @param path a path in BIP 32 format
* @param remotePublicKeyHex remote Curve25519 public key
* @option boolDisplay optionally enable or not the display
* @return an object with a shared secret
* @example
* eth.getEIP1024SharedSecret("44'/60'/0'/0/0", "87020e80af6e07a6e4697f091eacadb9e7e6629cb7e5a8a371689a3ed53b3d64").then(o => o.sharedSecret)
*/
getEIP1024SharedSecret(
path: string,
remotePublicKeyHex: string,
boolDisplay?: boolean
): Promise<{
sharedSecret: string;
}> {
const paths = splitPath(path);
const remotePublicKey = hexBuffer(remotePublicKeyHex);
const buffer = Buffer.alloc(1 + paths.length * 4 + 32);
let offset = 0;
buffer[0] = paths.length;
paths.forEach((element, index) => {
buffer.writeUInt32BE(element, 1 + 4 * index);
});
offset = 1 + 4 * paths.length;
remotePublicKey.copy(buffer, offset);
return this.transport
.send(0xe0, 0x18, boolDisplay ? 0x01 : 0x00, 0x01, buffer)
.then((response) => {
return {
sharedSecret: response.slice(0, -2).toString("hex"),
};
});
}

provideERC20TokenInformation({ data }: { data: Buffer }): Promise<boolean> {
console.warn(
"hw-app-eth: eth.provideERC20TokenInformation is deprecated. signTransaction solves this for you when providing it in `resolution`."
Expand Down
28 changes: 28 additions & 0 deletions packages/hw-app-eth/tests/Eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,3 +889,31 @@ test("eth2SetWithdrawalIndex", async () => {
const result = await eth.eth2SetWithdrawalIndex(1);
expect(result).toEqual(true);
});
test("getEIP1024PublicEncryptionKey", async () => {
const transport = await openTransportReplayer(
RecordStore.fromString(`
=> e018000015058000002c8000003c800000000000000000000000
<= 2f720080750797da95a41b052cf5694be1be81c0a662d449cd15f946f376e76d9000
`)
);
const eth = new Eth(transport);
const result = await eth.getEIP1024PublicEncryptionKey("44'/60'/0'/0/0", false);
expect(result).toEqual({
publicKey:
"2f720080750797da95a41b052cf5694be1be81c0a662d449cd15f946f376e76d",
});
});
test("getEIP1024SharedSecret", async () => {
const transport = await openTransportReplayer(
RecordStore.fromString(`
=> e018000135058000002c8000003c8000000000000000000000009ee8bf81321bc2a9e74de286621e13c013b5e4187b1c2fe42b686000672c6f33
<= 241dc9af8ecae08df6cf899a73a750b43117b50a7f1470405b5ff10adcf49f769000
`)
);
const eth = new Eth(transport);
const result = await eth.getEIP1024SharedSecret("44'/60'/0'/0/0", "9ee8bf81321bc2a9e74de286621e13c013b5e4187b1c2fe42b686000672c6f33", false);
expect(result).toEqual({
sharedSecret:
"241dc9af8ecae08df6cf899a73a750b43117b50a7f1470405b5ff10adcf49f76",
});
});