Skip to content

Commit

Permalink
feature(signet): add signet for unisat and sats-connect (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedoublejay authored Nov 22, 2024
1 parent 2ef6e53 commit 5428084
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
10 changes: 0 additions & 10 deletions packages/sdk/src/browser-wallets/internal/sats-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ async function satsConnectWalletGetAddresses(
getProvider: () => Promise<BitcoinProvider>,
network: BrowserWalletNetwork = "mainnet",
): Promise<WalletAddress[]> {
if (network === "signet") {
throw new OrditSDKError("signet network is not supported");
}

const result: WalletAddress[] = [];

const handleOnFinish = (response: GetAddressResponse) => {
Expand Down Expand Up @@ -120,9 +116,6 @@ async function satsConnectWalletSignPsbt(
inputsToSign,
}: SatsConnectSignPSBTOptions = { network: "mainnet", inputsToSign: [] },
): Promise<BrowserWalletSignResponse> {
if (network === "signet") {
throw new OrditSDKError("signet network is not supported");
}
if (!finalize && extractTx) {
throw new BrowserWalletExtractTxFromNonFinalizedPsbtError();
}
Expand Down Expand Up @@ -220,9 +213,6 @@ async function satsConnectWalletSignMessage(
address: string,
network: BrowserWalletNetwork = "mainnet",
): Promise<BrowserWalletSignResponse> {
if (network === "signet") {
throw new OrditSDKError("signet network is not supported");
}
if (!message || !network || !address) {
throw new OrditSDKError("Invalid options provided");
}
Expand Down
22 changes: 18 additions & 4 deletions packages/sdk/src/browser-wallets/unisat/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ describe("Unisat Wallet", () => {
expect(getAddresses("testnet")).resolves.toEqual([mockData]);
});

test("should return error from signet", () => {
expect(getAddresses("signet")).rejects.toThrowError(
"signet network is not supported",
);
test("should return address from signet", () => {
const mockData: WalletAddress = {
publicKey:
"02e522672eb584da7a5b5d61db1601584e7937471416388e4f43d69c720d62fe0f",
address:
"tb1p8mkym678ltgdyda5lkd0vqtqyps5mlthkpee6q43hhqmevjxzzyqrc4pfg",
format: "taproot",
};
const network = "signet";

vi.stubGlobal("unisat", {
getNetwork: vi
.fn()
.mockResolvedValue(NETWORK_TO_UNISAT_NETWORK[network]),
requestAccounts: vi.fn().mockResolvedValue([mockData.address]),
getPublicKey: vi.fn().mockResolvedValue(mockData.publicKey),
});
expect(getAddresses("signet")).resolves.toEqual([mockData]);
});

test("should return address from fractal bitcoin mainnet", () => {
Expand Down
4 changes: 0 additions & 4 deletions packages/sdk/src/browser-wallets/unisat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ async function getAddresses(
options: UnisatGetAddressesOptions = {},
): Promise<WalletAddress[]> {
const { readOnly = false } = options;

if (network === "signet") {
throw new OrditSDKError("signet network is not supported");
}
if (!isInstalled()) {
throw new BrowserWalletNotInstalledError("Unisat not installed");
}
Expand Down
50 changes: 46 additions & 4 deletions packages/sdk/src/browser-wallets/xverse/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,57 @@ describe("Xverse Wallet", () => {
expect(getAddresses("testnet")).resolves.toEqual(mockData);
});

test("should throw error on signet", () => {
test("should return address from signet", () => {
const mockData: WalletAddress[] = [
{
address:
"tb1pygh40yljmeknedr3cgwgyyjnh20g6zghqkzufz5z7d5njjsgx8ts8x4kzs",
publicKey:
"02b9907521ddb85e0e6a37622b7c685efbdc8ae53a334928adbd12cf204ad4e717",
format: "taproot",
},
{
// https://allprivatekeys.com/bitcoin-address-format
address: "2MwFLsoQMCRKaXkwT7j2PZ4pdZfhH9jXhQz",
publicKey:
"028a045950a789ff01350b95cd17544c5574f8b023aedce30ba6d0893ec119092f",
format: "p2sh-p2wpkh",
},
];
const mockResponse: GetAddressResponse = {
addresses: [
{
address:
"tb1pygh40yljmeknedr3cgwgyyjnh20g6zghqkzufz5z7d5njjsgx8ts8x4kzs",
publicKey:
"02b9907521ddb85e0e6a37622b7c685efbdc8ae53a334928adbd12cf204ad4e717",
purpose: satsConnect.AddressPurpose.Ordinals,
addressType: satsConnect.AddressType.p2tr,
},
{
address: "2MwFLsoQMCRKaXkwT7j2PZ4pdZfhH9jXhQz",
publicKey:
"028a045950a789ff01350b95cd17544c5574f8b023aedce30ba6d0893ec119092f",
purpose: satsConnect.AddressPurpose.Payment,
addressType: satsConnect.AddressType.p2wpkh,
},
],
};

vi.stubGlobal("window", {
XverseProviders: {
BitcoinProvider: {},
},
});
expect(getAddresses("signet")).rejects.toThrowError(
"signet network is not supported",
);

const getAddressSpy = vi.spyOn(satsConnect, "getAddress");

getAddressSpy.mockImplementation((options: GetAddressOptions) => {
options.onFinish(mockResponse);
return Promise.resolve();
});
expect(typeof window).not.toBeUndefined();
expect(getAddresses("signet")).resolves.toEqual(mockData);
});

test("should throw error on user cancel", () => {
Expand Down

0 comments on commit 5428084

Please sign in to comment.