Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dsdk 276 create keyring-eth module #6792

Merged
merged 2 commits into from
May 27, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/ledgerjs/packages/hw-app-eth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ provides the name of a trusted binding of a plugin with a contract address and a
##### Parameters

* `payload` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** external plugin data
* `signature` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** signature for the plugin
* `signature` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** 

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)>** a boolean

Expand Down
6 changes: 3 additions & 3 deletions libs/ledgerjs/packages/hw-app-eth/src/Eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,12 +1198,12 @@ export default class Eth {
* 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.
*
* @param payload external plugin data
* @param signature signature for the plugin
* @option signature optionally signature for the plugin
* @returns a boolean
*/
setExternalPlugin(payload: string, signature: string): Promise<boolean> {
setExternalPlugin(payload: string, signature?: string): Promise<boolean> {
const payloadBuffer = Buffer.from(payload, "hex");
const signatureBuffer = Buffer.from(signature, "hex");
const signatureBuffer = Buffer.from(signature ?? "", "hex");
const buffer = Buffer.concat([payloadBuffer, signatureBuffer]);
return this.transport.send(0xe0, 0x12, 0x00, 0x00, buffer).then(
() => true,
Expand Down
6 changes: 6 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import baseConfig from "../../jest.config";

export default {
...baseConfig,
rootDir: __dirname,
};
31 changes: 31 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@ledgerhq/keyring-eth",
"version": "0.0.1",
"description": "Ledger keyring eth module",
"files": [
"./lib"
],
"scripts": {
"build": "rimraf lib && tsc",
"test": "jest"
},
"keywords": [],
"license": "Apache-2.0",
"devDependencies": {
"@tsconfig/recommended": "^1.0.6",
"@types/jest": "^29.5.10",
"@types/node": "^20.8.10",
"jest": "^29.7.0",
"rimraf": "^4.4.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.7.0",
"typescript": "^5.4.5"
},
"dependencies": {
"@ledgerhq/context-module": "workspace:^",
"@ledgerhq/hw-app-eth": "workspace:^",
"@ledgerhq/hw-transport": "workspace:^",
"@ledgerhq/types-live": "workspace:^",
"ethers": "^5.7.2"
}
}
18 changes: 18 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/src/DefaultKeyringEth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Transport from "@ledgerhq/hw-transport";
import { DefaultKeyringEth } from "./DefaultKeyringEth";
import { Transaction } from "ethers";

describe("DefaultEthKeyring", () => {
describe("signTransaction function", () => {
it("Test1", async () => {
const keyring = new DefaultKeyringEth({
send: jest.fn(),
decorateAppAPIMethods: jest.fn(),
} as unknown as Transport);

const result = await keyring.signTransaction("", {} as Transaction, {});

expect(result).toEqual({});
});
});
});
46 changes: 46 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/src/DefaultKeyringEth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Transaction } from "ethers";
import Transport from "@ledgerhq/hw-transport";
import AppBinding from "@ledgerhq/hw-app-eth";
import {
EcdsaSignature,
KeyringEth,
GetAddressOptions,
SignMessageOptions,
SignMessagePayload,
SignTransactionOptions,
GetAddressResult,
} from "./KeyringEth";

export class DefaultKeyringEth implements KeyringEth {
private _appBinding: AppBinding;

constructor(transport: Transport) {
this._appBinding = new AppBinding(transport);
}

public async signTransaction(
_derivationPath: string,
_transaction: Transaction,
_options: SignTransactionOptions,
) {
// TODO: implement
return Promise.resolve({} as EcdsaSignature);
}

public async signMessage(
_derivationPath: string,
_message: SignMessagePayload,
_options: SignMessageOptions,
): Promise<EcdsaSignature> {
// TODO: implement
return Promise.resolve({} as EcdsaSignature);
}

public async getAddress(
_derivationPath: string,
_options?: GetAddressOptions,
): Promise<GetAddressResult> {
// TODO: implement
return Promise.resolve({} as GetAddressResult);
}
}
13 changes: 13 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/src/Keyring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export abstract class Keyring {
abstract getAddress(derivationPath: string, options: unknown): Promise<unknown>;
abstract signTransaction(
derivationPath: string,
transaction: unknown,
options: unknown,
): Promise<unknown>;
abstract signMessage(
derivationPath: string,
message: unknown,
options: unknown,
): Promise<unknown>;
}
41 changes: 41 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/src/KeyringEth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Transaction } from "ethers";
import { Keyring } from "./Keyring";
import { LoaderOptions } from "@ledgerhq/context-module/src/shared/model/LoaderOptions";
import { EIP712Message } from "@ledgerhq/types-live";

export type EcdsaSignature = { r: `0x${string}`; s: `0x${string}`; v: number };

export type EIP712Params = { domainSeparator: `0x${string}`; hashStruct: `0x${string}` };

export type SignTransactionOptions = LoaderOptions["options"];

export type SignMessagePayload = string | EIP712Message | EIP712Params;
aussedatlo marked this conversation as resolved.
Show resolved Hide resolved

// TODO: reinforce the type of the options
export type SignMessageOptions = { method: "personalSign" | "eip712" | "eip712Hashed" };

export type GetAddressResult = {
publicKey: string;
address: `0x${string}`;
};

export type GetAddressOptions = {
displayOnDevice?: boolean;
chainId?: string;
};

export interface KeyringEth extends Keyring {
getAddress(derivationPath: string, options?: GetAddressOptions): Promise<GetAddressResult>;

signTransaction(
derivationPath: string,
transaction: Transaction,
options: SignTransactionOptions,
): Promise<EcdsaSignature>;

signMessage(
derivationPath: string,
message: SignMessagePayload,
options: SignMessageOptions,
): Promise<EcdsaSignature>;
}
2 changes: 2 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./DefaultKeyringEth";
export * from "./KeyringEth";
7 changes: 7 additions & 0 deletions libs/ledgerjs/packages/keyring-eth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "lib"
},
"include": ["src/**/*"]
}