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

Enable tweak signer for taproot address #125

Merged
merged 9 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 5 additions & 0 deletions .changeset/moody-lobsters-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gobob/sats-wagmi": patch
---

Enable tweak signer for taproot address
2 changes: 1 addition & 1 deletion packages/sats-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3",
"@gobob/bob-sdk": "^3.0.3",
"@gobob/bob-sdk": "^3.0.4",
"@metamask/providers": "^12.0.0",
"@scure/base": "^1.1.6",
"@scure/btc-signer": "^1.3.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/sats-wagmi/src/connectors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ abstract class SatsConnector {
return this.ordinalsAddress;
}

/** Convience wrapper around the getAddressInfo function
/** Convenience wrapper around the getAddressInfo function
* @param address - The address to get the type of.
* @returns The address type of the address.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/sats-wagmi/src/connectors/okx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Network } from 'bitcoin-address-validation';
import { AddressType, Network } from 'bitcoin-address-validation';

import { okxLogo } from '../assets/okx';

Expand Down Expand Up @@ -157,7 +157,7 @@ class OKXConnector extends SatsConnector {
return {
index,
publicKey,
disableTweakSigner: true
disableTweakSigner: this.getAddressType(this.paymentAddress!) !== AddressType.p2tr
};
});

Expand Down
9 changes: 7 additions & 2 deletions packages/sats-wagmi/src/connectors/unisat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Network } from 'bitcoin-address-validation';
import { AddressType, Network } from 'bitcoin-address-validation';

import { bitgetLogo } from '../assets/bitget';

Expand Down Expand Up @@ -200,11 +200,16 @@ class UnisatConnector extends SatsConnector {
inputs.push(index);
}
}

if (!this.paymentAddress) {
throw new Error('No payment address specified');
}

const toSignInputs = inputs.map((index) => {
return {
index,
publicKey: this.publicKey,
disableTweakSigner: true
disableTweakSigner: this.getAddressType(this.paymentAddress!) !== AddressType.p2tr
slavastartsev marked this conversation as resolved.
Show resolved Hide resolved
};
});

Expand Down
46 changes: 43 additions & 3 deletions packages/sats-wagmi/src/hooks/useBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';

import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { EsploraClient } from '@gobob/bob-sdk';
import { EsploraClient, OrdinalsClient, OutPoint } from '@gobob/bob-sdk';
import { AddressType, getAddressInfo } from 'bitcoin-address-validation';

import { useSatsWagmi } from '../provider';
import { INTERVAL } from '../utils';
Expand Down Expand Up @@ -29,10 +30,49 @@ const useBalance = (props: UseBalanceProps = {}) => {
}

const esploraClient = new EsploraClient(network);
const ordinalsClient = new OrdinalsClient(network);

const { confirmed, unconfirmed, total } = await esploraClient.getBalance(address);
const addressInfo = getAddressInfo(address);

return { confirmed: BigInt(confirmed), unconfirmed: BigInt(unconfirmed), total: BigInt(total) };
if (addressInfo.type === AddressType.p2tr) {
const [outputs, cardinalOutputs] = await Promise.all([
esploraClient.getAddressUtxos(address),
// cardinal = return UTXOs not containing inscriptions or runes
ordinalsClient.getOutputsFromAddress(address, 'cardinal')
]);

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const total = outputs.reduce((acc, output) => {
if (cardinalOutputsSet.has(OutPoint.toString(output))) {
return acc + output.value;
}

return acc;
}, 0);
gregdhill marked this conversation as resolved.
Show resolved Hide resolved

const confirmed = outputs.reduce((acc, output) => {
if (cardinalOutputsSet.has(OutPoint.toString(output)) && output.confirmed) {
return acc + output.value;
}

return acc;
}, 0);
gregdhill marked this conversation as resolved.
Show resolved Hide resolved

return {
confirmed: BigInt(confirmed),
unconfirmed: BigInt(total - confirmed),
total: BigInt(total)
};
} else {
const { confirmed, unconfirmed, total } = await esploraClient.getBalance(address);

return {
confirmed: BigInt(confirmed),
unconfirmed: BigInt(unconfirmed),
total: BigInt(total)
};
}
},
...props
});
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.