From 3f7c3322d84e2aad6d1c2711eb7d904e1f68685b Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Fri, 23 Feb 2024 12:48:56 +1100 Subject: [PATCH] feat: cache pubkey in the scpe of a BtcSnapSigner instance --- app/src/utils/btcsnap-signer.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/utils/btcsnap-signer.ts b/app/src/utils/btcsnap-signer.ts index 03f2dbd..346ca9c 100644 --- a/app/src/utils/btcsnap-signer.ts +++ b/app/src/utils/btcsnap-signer.ts @@ -87,6 +87,7 @@ function anyPubToXpub(xyzpub: string, network: bitcoin.Network) { } export class BtcSnapSigner implements RemoteSigner { + pubKeyHex?: string; async _getBtcSnapNetwork(): Promise { return (await getNetworkInSnap()) === "test" ? BitcoinNetwork.Test @@ -105,8 +106,12 @@ export class BtcSnapSigner implements RemoteSigner { } } - // TODO: cache this or something so MetaMask doesn't prompt multiple times async getPublicKey(): Promise { + // poor man's, naive cache. Beware: we need a new signer instance created to "clear" the cached value. + if (this.pubKeyHex != undefined) { + return this.pubKeyHex; + } + const network = await this.getNetwork(); if (network === bitcoin.networks.bitcoin) { await updateNetworkInSnap(BitcoinNetwork.Test); @@ -255,8 +260,8 @@ export class BtcSnapSigner implements RemoteSigner { } } -export async function createOrdinal(address: string, inscription: Inscription) { - const signer = new BtcSnapSigner(); +export async function createOrdinal(address: string, inscription: Inscription, maybeSigner?: BtcSnapSigner) { + const signer = maybeSigner || new BtcSnapSigner(); // fee rate is 1 for testnet const feeRate = await getFeeRate(); const tx = await inscribeData(signer, address, feeRate, inscription); // 546 @@ -270,9 +275,10 @@ export async function createOrdinal(address: string, inscription: Inscription) { export async function sendInscription( address: string, - inscriptionId: string + inscriptionId: string, + maybeSigner?: BtcSnapSigner ): Promise { - const signer = new BtcSnapSigner(); + const signer = maybeSigner || new BtcSnapSigner(); // fee rate is 1 for testnet const feeRate = await getFeeRate(); const txid = await transferInscription(signer, address, inscriptionId, feeRate);