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

Revert slip44 change #1931

Merged
merged 1 commit into from
Jul 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading