From 0ec50b6159be38d6baf90ca208cdb095634f44ee Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 21 Jun 2023 09:15:10 +0300 Subject: [PATCH] Support for signRaw on Ledger SubstrateApp (#1849) * Support for signRaw on Ledger SubstrateApp * Adjust with callback return * Ensure message is wrapped before signing --- CHANGELOG.md | 2 ++ packages/hw-ledger/src/bundle.ts | 29 ++++++++++++++++++++--------- packages/util/src/u8a/toBuffer.ts | 2 +- packages/util/src/u8a/wrap.ts | 6 ++++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e4414fae..df54fb8f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ Changes: +- Add support for Ledger `signRaw` - Upgrade dependencies to latest stable versions - Use `stringify/import.meta.url` helpers in specs + ## 12.3.2 Jun 12, 2023 Changes: diff --git a/packages/hw-ledger/src/bundle.ts b/packages/hw-ledger/src/bundle.ts index f6ddc9f57..ae2e16fd8 100644 --- a/packages/hw-ledger/src/bundle.ts +++ b/packages/hw-ledger/src/bundle.ts @@ -8,7 +8,7 @@ import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerVersion } fr import { newSubstrateApp } from '@zondax/ledger-substrate'; import { transports } from '@polkadot/hw-ledger-transports'; -import { hexAddPrefix, u8aToBuffer } from '@polkadot/util'; +import { hexAddPrefix, u8aToBuffer, u8aWrapBytes } from '@polkadot/util'; import { LEDGER_DEFAULT_ACCOUNT, LEDGER_DEFAULT_CHANGE, LEDGER_DEFAULT_INDEX, LEDGER_SUCCESS_CODE } from './constants.js'; import { ledgerApps } from './defaults.js'; @@ -30,6 +30,17 @@ async function wrapError (promise: Promise): Promis return result; } +/** @internal Wraps a sign/signRaw call and returns the associated signature */ +function sign (method: 'sign' | 'signRaw', message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial = {}): (app: SubstrateApp) => Promise { + return async (app: SubstrateApp): Promise => { + const { signature } = await wrapError(app[method](account + accountOffset, change, addressIndex + addressOffset, u8aToBuffer(message))); + + return { + signature: hexAddPrefix(signature.toString('hex')) + }; + }; +} + /** * @name Ledger * @@ -91,15 +102,15 @@ export class Ledger { /** * Signs a transaction on the Ledger device */ - public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial = {}): Promise { - return this.withApp(async (app: SubstrateApp): Promise => { - const buffer = u8aToBuffer(message); - const { signature } = await wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer)); + public async sign (message: Uint8Array, accountOffset?: number, addressOffset?: number, options?: Partial): Promise { + return this.withApp(sign('sign', message, accountOffset, addressOffset, options)); + } - return { - signature: hexAddPrefix(signature.toString('hex')) - }; - }); + /** + * Signs a message (non-transactional) on the Ledger device + */ + public async signRaw (message: Uint8Array, accountOffset?: number, addressOffset?: number, options?: Partial): Promise { + return this.withApp(sign('signRaw', u8aWrapBytes(message), accountOffset, addressOffset, options)); } /** diff --git a/packages/util/src/u8a/toBuffer.ts b/packages/util/src/u8a/toBuffer.ts index a367278e0..cf8d68b4e 100644 --- a/packages/util/src/u8a/toBuffer.ts +++ b/packages/util/src/u8a/toBuffer.ts @@ -12,7 +12,7 @@ * ```javascript * import { u8aToBuffer } from '@polkadot/util'; * - * console.log('Buffer', u8aToBuffer('0x123480001f')); + * console.log('Buffer', u8aToBuffer(new Uint8Array([1, 2, 3]))); * ``` */ export function u8aToBuffer (value?: Uint8Array | null): Buffer { diff --git a/packages/util/src/u8a/wrap.ts b/packages/util/src/u8a/wrap.ts index f530bdc83..f23a97623 100644 --- a/packages/util/src/u8a/wrap.ts +++ b/packages/util/src/u8a/wrap.ts @@ -51,12 +51,14 @@ export function u8aUnwrapBytes (bytes: U8aLike): Uint8Array { /** * @name u8aWrapBytes - * @description Adds a ... wrapper to the supplied value (if not already existing) + * @description + * Adds a ... wrapper to the supplied value, if + * - We don't already have a Bytes wrapper + * - The message is not an Ethereum-style message */ export function u8aWrapBytes (bytes: U8aLike): Uint8Array { const u8a = u8aToU8a(bytes); - // if Ethereum-wrapping, we don't add our wrapping bytes return u8aIsWrapped(u8a, true) ? u8a : u8aConcatStrict([U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX]);