Skip to content

Commit

Permalink
Revert slip44 change
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Jul 20, 2024
1 parent 6947b1e commit 5c1ea20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 11 additions & 10 deletions packages/hw-ledger/src/LedgerGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { PolkadotGenericApp } from '@zondax/ledger-substrate';
import { transports } from '@polkadot/hw-ledger-transports';
import { hexAddPrefix, u8aToBuffer, u8aWrapBytes } from '@polkadot/util';

import { POLKADOT_SLIP } from './constants.js';
import { ledgerApps } from './defaults.js';

export { packageInfo } from './packageInfo.js';
Expand Down Expand Up @@ -48,8 +47,8 @@ async function wrapError <T extends WrappedResult> (promise: Promise<T>): Promis
}

/** @internal Wraps a sign/signRaw call and returns the associated signature */
function sign (method: 'sign' | 'signRaw', message: Uint8Array, accountIndex = 0, addressOffset = 0): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
const bip42Path = `m/44'/${POLKADOT_SLIP}'/${accountIndex}'/${0}'/${addressOffset}'`;
function sign (method: 'sign' | 'signRaw', message: Uint8Array, slip44: number, accountIndex = 0, addressOffset = 0): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
const bip42Path = `m/44'/${slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;

return async (app: PolkadotGenericApp): Promise<LedgerSignature> => {
const { signature } = await wrapError(app[method](bip42Path, u8aToBuffer(message)));
Expand All @@ -61,8 +60,8 @@ function sign (method: 'sign' | 'signRaw', message: Uint8Array, accountIndex = 0
}

/** @internal Wraps a signWithMetadata call and returns the associated signature */
function signWithMetadata (message: Uint8Array, accountIndex = 0, addressOffset = 0, { metadata }: Partial<AccountOptionsGeneric> = {}): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
const bip42Path = `m/44'/${POLKADOT_SLIP}'/${accountIndex}'/${0}'/${addressOffset}'`;
function signWithMetadata (message: Uint8Array, slip44: number, accountIndex = 0, addressOffset = 0, { metadata }: Partial<AccountOptionsGeneric> = {}): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
const bip42Path = `m/44'/${slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;

return async (app: PolkadotGenericApp): Promise<LedgerSignature> => {
if (!metadata) {
Expand All @@ -89,6 +88,7 @@ function signWithMetadata (message: Uint8Array, accountIndex = 0, addressOffset
*/
export class LedgerGeneric {
readonly #transportDef: TransportDef;
readonly #slip44: number;
/**
* The chainId is represented by the chains token in all lowercase. Example: Polkadot -> dot
*/
Expand All @@ -102,7 +102,7 @@ export class LedgerGeneric {

#app: PolkadotGenericApp | null = null;

constructor (transport: TransportType, chain: Chain, chainId?: string, metaUrl?: string) {
constructor (transport: TransportType, chain: Chain, slip44: number, chainId?: string, metaUrl?: string) {
const ledgerName = ledgerApps[chain];
const transportDef = transports.find(({ type }) => type === transport);

Expand All @@ -114,6 +114,7 @@ export class LedgerGeneric {

this.#metaUrl = metaUrl;
this.#chainId = chainId;
this.#slip44 = slip44;
this.#transportDef = transportDef;
}

Expand All @@ -122,7 +123,7 @@ export class LedgerGeneric {
* asks for on-device confirmation
*/
public async getAddress (ss58Prefix: number, confirm = false, accountIndex = 0, addressOffset = 0): Promise<LedgerAddress> {
const bip42Path = `m/44'/${POLKADOT_SLIP}'/${accountIndex}'/${0}'/${addressOffset}'`;
const bip42Path = `m/44'/${this.#slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;

return this.withApp(async (app: PolkadotGenericApp): Promise<LedgerAddress> => {
const { address, pubKey } = await wrapError(app.getAddress(bip42Path, ss58Prefix, confirm));
Expand Down Expand Up @@ -153,21 +154,21 @@ export class LedgerGeneric {
* @description Signs a transaction on the Ledger device. This requires the LedgerGeneric class to be instantiated with `chainId`, and `metaUrl`
*/
public async sign (message: Uint8Array, accountIndex?: number, addressOffset?: number): Promise<LedgerSignature> {
return this.withApp(sign('sign', message, accountIndex, addressOffset));
return this.withApp(sign('sign', message, this.#slip44, accountIndex, addressOffset));
}

/**
* @description Signs a message (non-transactional) on the Ledger device
*/
public async signRaw (message: Uint8Array, accountIndex?: number, addressOffset?: number): Promise<LedgerSignature> {
return this.withApp(sign('signRaw', u8aWrapBytes(message), accountIndex, addressOffset));
return this.withApp(sign('signRaw', u8aWrapBytes(message), this.#slip44, accountIndex, addressOffset));
}

/**
* @description Signs a transaction on the ledger device provided some metadata.
*/
public async signWithMetadata (message: Uint8Array, accountIndex?: number, addressOffset?: number, options?: Partial<AccountOptionsGeneric>): Promise<LedgerSignature> {
return this.withApp(signWithMetadata(message, accountIndex, addressOffset, options));
return this.withApp(signWithMetadata(message, this.#slip44, accountIndex, addressOffset, options));
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/hw-ledger/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ export const LEDGER_DEFAULT_CHANGE = 0x80000000;
export const LEDGER_DEFAULT_INDEX = 0x80000000;

export const LEDGER_SUCCESS_CODE = 0x9000;

export const POLKADOT_SLIP = 0x00000162;

0 comments on commit 5c1ea20

Please sign in to comment.