Skip to content

Commit

Permalink
Add new address type support to ckbtc (#370)
Browse files Browse the repository at this point in the history
# Motivation
ckBTC now support p2tr and p2wsh adresses.
<!-- Describe the motivation that lead to the PR -->
  • Loading branch information
leokazz authored Oct 12, 2023
1 parent ef4a5e9 commit 5273278
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
2 changes: 2 additions & 0 deletions packages/ckbtc/src/enums/btc.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export enum BtcAddressType {
P2wpkhV0,
P2pkh,
P2sh,
P2wsh,
P2tr,
}
65 changes: 53 additions & 12 deletions packages/ckbtc/src/utils/btc.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,32 +224,73 @@ describe("BTC utils", () => {
});
});

describe("not supported address types", () => {
it("fails on Mainnet Bech32 P2WSH", () => {
describe("p2wsh and P2tr address type", () => {
it("Mainnet Bech32 P2WSH", () => {
const address =
"bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3";

expect(() =>
parseBtcAddress({ address, network: BtcNetwork.Mainnet }),
).toThrow();
expect(parseBtcAddress({ address, network: BtcNetwork.Mainnet })).toEqual(
{
address,
network: BtcNetwork.Mainnet,
type: BtcAddressType.P2wsh,
parser: "bip-173",
},
);
});

it("fails on Testnet Bech32 P2WSH", () => {
it("Testnet Bech32 P2WSH", () => {
const address =
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7";

expect(() =>
parseBtcAddress({ address, network: BtcNetwork.Testnet }),
).toThrow();
expect(parseBtcAddress({ address, network: BtcNetwork.Testnet })).toEqual(
{
address,
network: BtcNetwork.Testnet,
type: BtcAddressType.P2wsh,
parser: "bip-173",
},
);
});

it("Regtest Bech32 P2WSH", () => {
const address =
"bcrt1q5n2k3frgpxces3dsw4qfpqk4kksv0cz96pldxdwxrrw0d5ud5hcqzzx7zt";

expect(parseBtcAddress({ address, network: BtcNetwork.Regtest })).toEqual(
{
address,
network: BtcNetwork.Regtest,
type: BtcAddressType.P2wsh,
parser: "bip-173",
},
);
});
it("fails on Regtest Bech32 P2WSH", () => {
const address =
"bcrt1q5n2k3frgpxces3dsw4qfpqk4kksv0cz96pldxdwxrrw0d5ud5hcqzzx7zt";

expect(() =>
parseBtcAddress({ address, network: BtcNetwork.Regtest }),
).toThrow();
expect(parseBtcAddress({ address, network: BtcNetwork.Regtest })).toEqual(
{
address,
network: BtcNetwork.Regtest,
type: BtcAddressType.P2wsh,
parser: "bip-173",
},
);
});
it("Mainnet Bech32m P2tr", () => {
const address =
"bc1pz37fc4cn9ah8anwm4xqqhvxygjf9rjf2resrw8h8w4tmvcs0863sa2e586";

expect(parseBtcAddress({ address, network: BtcNetwork.Mainnet })).toEqual(
{
address,
network: BtcNetwork.Mainnet,
type: BtcAddressType.P2tr,
parser: "bip-173",
},
);
});
});
});
35 changes: 18 additions & 17 deletions packages/ckbtc/src/utils/btc.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,30 @@ const parseBip173Address = ({

const [witnessVersion, ...rest] = words;

if (witnessVersion !== 0) {
if (witnessVersion > 1) {
throw new ParseBtcAddressUnsupportedWitnessVersionError();
}

const data = bech32.fromWords(rest);

if (data.length !== 20) {
// Note: We throw an error for other length because the Minter canister applies such a policy and does not support P2tr and P2wsh yet.
// else if (witnessVersion === 1) {
// type = BtcAddressType.P2tr;
// } else {
// type = BtcAddressType.P2wsh;
// }

throw new ParseBtcAddressBadWitnessLengthError();
switch (data.length) {
case 20:
return {
address,
network,
type: BtcAddressType.P2wpkhV0,
parser: "bip-173",
};
case 32:
return {
address,
network,
type: witnessVersion === 0 ? BtcAddressType.P2wsh : BtcAddressType.P2tr,
parser: "bip-173",
};
default:
throw new ParseBtcAddressBadWitnessLengthError();
}

return {
address,
network,
type: BtcAddressType.P2wpkhV0,
parser: "bip-173",
};
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
AccountIdentifier,
SubAccount,
accountIdentifierFromBytes,
principalToAccountIdentifier,
SubAccount,
} from "@dfinity/ledger-icp";
import type {
ListNeuronsResponse,
Expand Down

0 comments on commit 5273278

Please sign in to comment.