From 7919707fe12aeae64a731d9fdafa3ef814cfdd44 Mon Sep 17 00:00:00 2001 From: Jon Tzeng Date: Wed, 24 Jul 2024 17:24:09 -0700 Subject: [PATCH] Add `EdgeCurrencyWallet.signBytes` --- CHANGELOG.md | 1 + .../currency/wallet/currency-wallet-api.ts | 26 +++++++++++++++++++ src/types/types.ts | 24 ++++++++++++++--- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35fdc6ff..06036c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- added: `EdgeCurrencyWallet.signBytes` to sign raw bytes with a private key. - changed: Randomize data-sync start times. ## 2.9.2 (2024-07-25) diff --git a/src/core/currency/wallet/currency-wallet-api.ts b/src/core/currency/wallet/currency-wallet-api.ts index c467aca3..bef52b7f 100644 --- a/src/core/currency/wallet/currency-wallet-api.ts +++ b/src/core/currency/wallet/currency-wallet-api.ts @@ -1,5 +1,6 @@ import { div, eq, mul } from 'biggystring' import { Disklet } from 'disklet' +import { base64 } from 'rfc4648' import { bridgifyObject, onMethod, watchMethod } from 'yaob' import { @@ -532,6 +533,31 @@ export function makeCurrencyWalletApi( ) }, + async signBytes( + bytes: Uint8Array, + opts: EdgeSignMessageOptions = {} + ): Promise { + const privateKeys = walletInfo.keys + + if (engine.signBytes != null) { + return await engine.signBytes(bytes, privateKeys, opts) + } + + // Various plugins expect specific encodings for signing messages + // (base16, base64, etc). + // Do the conversion here temporarily if `signMessage` is implemented + // while we migrate to `signBytes`. + else if (pluginId === 'bitcoin' && engine.signMessage != null) { + return await engine.signMessage( + base64.stringify(bytes), + privateKeys, + opts + ) + } + + throw new Error(`${pluginId} doesn't support signBytes`) + }, + async signMessage( message: string, opts: EdgeSignMessageOptions = {} diff --git a/src/types/types.ts b/src/types/types.ts index afc65841..599cbc3f 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -972,8 +972,8 @@ export interface EdgeCurrencyEngine { ) => Promise // Signing: - readonly signMessage?: ( - message: string, + readonly signBytes?: ( + bytes: Uint8Array, privateKeys: JsonObject, opts: EdgeSignMessageOptions ) => Promise @@ -1001,6 +1001,16 @@ export interface EdgeCurrencyEngine { /** @deprecated Provide EdgeCurrencyTools.getDisplayPublicKey: */ readonly getDisplayPublicSeed?: () => string | null + + /** + * @deprecated Replaced by `signBytes`. + * Various plugins implement this function with inconsistent encodings. + */ + readonly signMessage?: ( + message: string, + privateKeys: JsonObject, + opts: EdgeSignMessageOptions + ) => Promise } // currency plugin ----------------------------------------------------- @@ -1216,8 +1226,8 @@ export interface EdgeCurrencyWallet { ) => Promise // Signing: - readonly signMessage: ( - message: string, + readonly signBytes: ( + buf: Uint8Array, opts?: EdgeSignMessageOptions ) => Promise @@ -1240,6 +1250,12 @@ export interface EdgeCurrencyWallet { // Generic: readonly otherMethods: EdgeOtherMethods + + /** @deprecated Use `signBytes` instead. */ + readonly signMessage: ( + message: string, + opts?: EdgeSignMessageOptions + ) => Promise } export interface EdgeMemoryWallet {