Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

feat(lsk): implement LedgerService #240

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 30 additions & 0 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file modified .yarn/install-state.gz
Binary file not shown.
6 changes: 5 additions & 1 deletion packages/platform-sdk-lsk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
},
"dependencies": {
"@arkecosystem/platform-sdk": "workspace:packages/platform-sdk",
"@ledgerhq/hw-transport-node-hid-singleton": "^5.15.0",
"@liskhq/lisk-client": "^2.0",
"@liskhq/lisk-cryptography": "^2.4",
"@liskhq/lisk-transactions": "^2.0"
"@liskhq/lisk-transactions": "^2.0",
"dpos-ledger-api": "^3.0.1",
"lisk-elements": "^2.4.3"
},
"devDependencies": {
"@ledgerhq/hw-transport-mocker": "^5.15.0",
"@sindresorhus/tsconfig": "^0.7.0",
"@types/eslint": "^6.8.0",
"@types/eslint-plugin-prettier": "^3.1.0",
Expand Down
40 changes: 33 additions & 7 deletions packages/platform-sdk-lsk/src/services/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
import { Contracts, Exceptions } from "@arkecosystem/platform-sdk";
import { Contracts, Exceptions, Utils } from "@arkecosystem/platform-sdk";
import LedgerTransport from "@ledgerhq/hw-transport-node-hid-singleton";
import { DposLedger, LedgerAccount, SupportedCoin } from "dpos-ledger-api";
import Lisk from "lisk-elements";

export class LedgerService implements Contracts.LedgerService {
readonly #ledger: LedgerTransport;
readonly #transport: DposLedger;

private constructor(transport: Contracts.LedgerTransport) {
//
this.#ledger = transport;
// @ts-ignore
this.#transport = new DposLedger(transport);
}

public static async construct(opts: Contracts.LedgerOptions): Promise<LedgerService> {
return new LedgerService(opts.transport);
return new LedgerService(opts.transport || (await LedgerTransport.create()));
}

public async destruct(): Promise<void> {
//
await this.#ledger.close();
}

public async getVersion(): Promise<string> {
throw new Exceptions.NotImplemented(this.constructor.name, "getVersion");
}

public async getPublicKey(path: string): Promise<string> {
throw new Exceptions.NotImplemented(this.constructor.name, "getPublicKey");
const { publicKey } = await this.#transport.getPubKey(this.getLedgerAccount(path));

return publicKey;
}

public async signTransaction(path: string, payload: Buffer): Promise<string> {
throw new Exceptions.NotImplemented(this.constructor.name, "signTransaction");
const signature: Buffer = await this.#transport.signTX(
this.getLedgerAccount(path),
// @ts-ignore
Lisk.transaction.utils.getTransactionBytes(payload)
);

return signature.toString("hex");
}

public async signTransactionWithSchnorr(path: string, payload: Buffer): Promise<string> {
throw new Exceptions.NotImplemented(this.constructor.name, "signTransactionWithSchnorr");
}

public async signMessage(path: string, payload: Buffer): Promise<string> {
throw new Exceptions.NotImplemented(this.constructor.name, "signMessage");
const signature: Buffer = await this.#transport.signMSG(this.getLedgerAccount(path), payload);

return signature.slice(0, 64).toString("hex");
}

public async signMessageWithSchnorr(path: string, payload: Buffer): Promise<string> {
throw new Exceptions.NotImplemented(this.constructor.name, "signMessageWithSchnorr");
}

private getLedgerAccount(path: string): LedgerAccount {
const account: LedgerAccount = new LedgerAccount();
account.coinIndex(SupportedCoin.LISK);
account.account(Utils.BIP44.parse(path).account);

return account;
}
}
19 changes: 19 additions & 0 deletions packages/platform-sdk/src/utils/bip44.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,23 @@ export class BIP44 {

return bip32.fromSeed(bip39.mnemonicToSeedSync(passphrase));
}

public static parse(path: string) {
if (!path.match(new RegExp("^((m/)?(44'?)){1}(/[0-9]+'?){2}(/[0-9]+){2}$", "g"))) {
throw new Error(path);
}

const result: number[] = [];
for (const level of path.replace("m/", "").split("/")) {
result.push(+level.replace("'", ""));
}

return {
purpose: result[0],
coinType: result[1],
account: result[2],
change: result[3],
addressIndex: result[4],
};
}
}
30 changes: 29 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ __metadata:
resolution: "@arkecosystem/platform-sdk-lsk@workspace:packages/platform-sdk-lsk"
dependencies:
"@arkecosystem/platform-sdk": "workspace:packages/platform-sdk"
"@ledgerhq/hw-transport-mocker": ^5.15.0
"@ledgerhq/hw-transport-node-hid-singleton": ^5.15.0
"@liskhq/lisk-client": ^2.0
"@liskhq/lisk-cryptography": ^2.4
"@liskhq/lisk-transactions": ^2.0
Expand All @@ -385,13 +387,15 @@ __metadata:
"@typescript-eslint/parser": ^2.30.0
codecov: ^3.6.5
cross-env: ^7.0.2
dpos-ledger-api: ^3.0.1
eslint: ^6.8.0
eslint-config-prettier: ^6.11.0
eslint-plugin-jest: ^23.8.2
eslint-plugin-prettier: ^3.1.3
eslint-plugin-simple-import-sort: ^5.0.3
jest: ^25.4.0
jest-extended: ^0.11.5
lisk-elements: ^2.4.3
nock: ^12.0.3
npm-check-updates: ^4.1.2
prettier: ^2.0.5
Expand Down Expand Up @@ -4218,7 +4222,7 @@ __metadata:
languageName: node
linkType: hard

"bip32-path@npm:0.4.2":
"bip32-path@npm:0.4.2, bip32-path@npm:^0.4.2":
version: 0.4.2
resolution: "bip32-path@npm:0.4.2"
checksum: 2/e4416f5b16993d27f5642c059d23d4416333344160b9c7044f6944b0c12fac8c92bdcd0c9a342a6d9d114edb6f3fff8fc4f36da6aaa9e0b6716761238262f79b
Expand Down Expand Up @@ -6169,6 +6173,16 @@ __metadata:
languageName: node
linkType: hard

"dpos-ledger-api@npm:^3.0.1":
version: 3.0.1
resolution: "dpos-ledger-api@npm:3.0.1"
dependencies:
bip32-path: ^0.4.2
crc: ^3.5.0
checksum: 2/17f466e6eddfc9153d7ec87bb34067e1f5f89a2a8b7bf380aa1a57b72872dabe085b4f429b10453f2103ed814b03a052cd61790b3dd8da1edaf292ae02b053a4
languageName: node
linkType: hard

"drbg.js@npm:^1.0.1":
version: 1.0.1
resolution: "drbg.js@npm:1.0.1"
Expand Down Expand Up @@ -10053,6 +10067,20 @@ fsevents@^2.1.2:
languageName: node
linkType: hard

"lisk-elements@npm:^2.4.3":
version: 2.4.3
resolution: "lisk-elements@npm:2.4.3"
dependencies:
"@liskhq/lisk-api-client": 2.0.3
"@liskhq/lisk-constants": 1.2.3
"@liskhq/lisk-cryptography": 2.3.0
"@liskhq/lisk-passphrase": 2.0.3
"@liskhq/lisk-transactions": 2.3.1
"@types/node": 10.12.21
checksum: 2/8996c8f2a477e284a2754bd26c6ce50baec46ddedb7dc5ad20094cd7182b74d558be0fb101cbaa86abe8e989d172167390c5b4b31fcbcff92bbb3c60b2d17a4b
languageName: node
linkType: hard

"load-json-file@npm:^1.0.0":
version: 1.1.0
resolution: "load-json-file@npm:1.1.0"
Expand Down