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: add ECDSA support, troubleshooting logs included, WIP #8886

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions libs/coin-framework/src/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ const modes: Readonly<Record<DerivationMode, ModeSpec>> = Object.freeze({
solanaSub: {
overridesDerivation: "44'/501'/<account>'",
},
hederaBip44: {
hederaBip44Ed25519: {
overridesDerivation: "44/3030",
},
hederaBip44Ecdsa: {
overridesDerivation: "44/3030",
},
cardano: {
Expand Down Expand Up @@ -191,7 +194,7 @@ const legacyDerivations: Partial<Record<CryptoCurrency["id"], DerivationMode[]>>
tezos: ["galleonL", "tezboxL", "tezosbip44h", "tezbox"],
stellar: ["sep5"],
polkadot: ["polkadotbip44"],
hedera: ["hederaBip44"],
hedera: ["hederaBip44Ed25519", "hederaBip44Ecdsa"],
filecoin: ["glifLegacy", "filecoinBIP44", "glif"],
internet_computer: ["internet_computer"],
casper: ["casper_wallet"],
Expand Down
14 changes: 14 additions & 0 deletions libs/coin-modules/coin-hedera/src/api/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const fetch = (path: string) => {
export interface Account {
accountId: AccountId;
balance: BigNumber;
keyType: string;
}

export async function getAccountsForPublicKey(publicKey: string): Promise<Account[]> {
Expand All @@ -38,12 +39,25 @@ export async function getAccountsForPublicKey(publicKey: string): Promise<Accoun
accounts.push({
accountId: AccountId.fromString(raw.account),
balance: accountBalance.balance,
keyType: raw.key._type,
});
}

return accounts;
}

export async function getKeyTypeForAccount(accountId: string): Promise<string> {
let r;
try {
r = await fetch(`/api/v1/accounts/${accountId}`);
} catch (e: any) {
if (e.name === "LedgerAPI4xx") return "";
throw e;
}

return r.data.key._type;
}

interface HederaMirrorTransaction {
transfers: HederaMirrorTransfer[];
charged_tx_fee: string;
Expand Down
5 changes: 4 additions & 1 deletion libs/coin-modules/coin-hedera/src/bridge/signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { buildOptimisticOperation } from "./buildOptimisticOperation";
import { buildUnsignedTransaction } from "../api/network";
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import { Transaction, HederaSigner } from "../types";
import { getKeyTypeForAccount } from "../api/mirror";

export const buildSignOperation =
(
Expand All @@ -25,9 +26,11 @@ export const buildSignOperation =

const accountPublicKey = PublicKey.fromString(account.seedIdentifier);

const keyType = await getKeyTypeForAccount(account.freshAddress);

const res = await signerContext(deviceId, async signer => {
await hederaTransaction.signWith(accountPublicKey, async bodyBytes => {
return await signer.signTransaction(bodyBytes);
return await signer.signTransaction(bodyBytes, keyType !== "ED25519");
});
return hederaTransaction.toBytes();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const getAccountShape: GetAccountShape<Account> = async (
};

export const buildIterateResult: IterateResultBuilder = async ({ result: rootResult }) => {
console.log(rootResult.publicKey);
const accounts = await getAccountsForPublicKey(rootResult.publicKey);
const addresses = accounts.map(a => a.accountId.toString());

Expand Down
10 changes: 8 additions & 2 deletions libs/coin-modules/coin-hedera/src/signer/getAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { SignerContext } from "@ledgerhq/coin-framework/signer";
import { GetAddressOptions } from "@ledgerhq/coin-framework/derivation";

const resolver = (signerContext: SignerContext<HederaSigner>): GetAddressFn => {
return async (deviceId: string, { path }: GetAddressOptions) => {
const publicKey = await signerContext(deviceId, signer => signer.getPublicKey(path));
return async (deviceId: string, { path, derivationMode }: GetAddressOptions) => {
let publicKey = "";
console.log(derivationMode);
if (derivationMode == "hederaBip44Ecdsa") {
publicKey = await signerContext(deviceId, signer => signer.getPublicKey(path, true));
console.log(publicKey);
}
publicKey = await signerContext(deviceId, signer => signer.getPublicKey(path));

return {
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const hedera: CurrenciesData<Transaction> = {
id: `js:2:hedera:0.0.751515:`,
seedIdentifier: "",
name: "Hedera 1",
derivationMode: "hederaBip44",
derivationMode: "hederaBip44Ed25519",
index: 0,
freshAddress: "0.0.751515",
freshAddressPath: "44/3030/0/0/0",
Expand Down
4 changes: 2 additions & 2 deletions libs/coin-modules/coin-hedera/src/types/signer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type HederaSignature = Uint8Array | undefined; // NOTE: check if undefined is possible
export interface HederaSigner {
getPublicKey(path: string): Promise<string>;
signTransaction(transaction: Uint8Array): Promise<Uint8Array>;
getPublicKey(path: string, ecdsa?: boolean): Promise<string>;
signTransaction(transaction: Uint8Array, ecdsa?: boolean): Promise<Uint8Array>;
}

export type HederaSignatureSdk = Uint8Array;
23 changes: 19 additions & 4 deletions libs/ledgerjs/packages/hw-app-hedera/src/Hedera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,26 @@ export default class Hedera {
* @param path a path in BIP-32 format
* @return the public key
*/
async getPublicKey(path: string): Promise<string> {
async getPublicKey(path: string, ecdsa?: boolean): Promise<string> {
console.log("path: " + path);
const bipPath = BIPPath.fromString(path).toPathArray();
const serializedPath = this._serializePath(bipPath);

//44/3030/0

const p1 = 0x01;
const p2 = 0x00;
let p2 = 0x00;

if (ecdsa) {
p2 = 1;
}

console.log("getting key...");

const response = await this.transport.send(CLA, INS.GET_PUBLIC_KEY, p1, p2, serializedPath);

console.log(response);

const returnCodeBytes = response.slice(-2);
const returnCode = (returnCodeBytes[0] << 8) | returnCodeBytes[1];

Expand All @@ -53,13 +64,17 @@ export default class Hedera {
}

// TODO: the BOLOS app does not support anything but index #0 for signing transactions
async signTransaction(transaction: Uint8Array): Promise<Uint8Array> {
async signTransaction(transaction: Uint8Array, ecdsa?: boolean): Promise<Uint8Array> {
const payload = Buffer.alloc(4 + transaction.length);
payload.writeUInt32LE(0);
payload.fill(transaction, 4);

const p1 = 0x00;
const p2 = 0x00;
let p2 = 0x00;

if (ecdsa) {
p2 = 1;
}

const response = await this.transport.send(CLA, INS.SIGN_TRANSACTION, p1, p2, payload);

Expand Down
2 changes: 2 additions & 0 deletions libs/ledgerjs/packages/hw-transport/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ export default class Transport {
}

tracer.trace("Starting an exchange", { abortTimeoutMs });
console.log("apdu: " + cla + " " + ins + " " + p1 + " " + p2 + " " + data);
console.log("p2: " + p2);
const response = await this.exchange(
// The size of the data is added in 1 byte just before `data`
Buffer.concat([Buffer.from([cla, ins, p1, p2]), Buffer.from([data.length]), data]),
Expand Down
3 changes: 2 additions & 1 deletion libs/ledgerjs/packages/types-live/src/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export type DerivationMode =
| "casper_wallet"
| "solanaMain"
| "solanaSub"
| "hederaBip44"
| "hederaBip44Ed25519"
| "hederaBip44Ecdsa"
| "cardano"
| "nearbip44h"
| "vechain"
Expand Down