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

Feat: cache pubkey #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
16 changes: 11 additions & 5 deletions app/src/utils/btcsnap-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function anyPubToXpub(xyzpub: string, network: bitcoin.Network) {
}

export class BtcSnapSigner implements RemoteSigner {
pubKeyHex?: string;
async _getBtcSnapNetwork(): Promise<BitcoinNetwork> {
return (await getNetworkInSnap()) === "test"
? BitcoinNetwork.Test
Expand All @@ -105,8 +106,12 @@ export class BtcSnapSigner implements RemoteSigner {
}
}

// TODO: cache this or something so MetaMask doesn't prompt multiple times
async getPublicKey(): Promise<string> {
// 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);
Expand Down Expand Up @@ -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
Expand All @@ -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<string> {
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);
Expand Down