This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Faust
committed
May 15, 2020
1 parent
0881ea3
commit 4a3cf5c
Showing
8 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters