Skip to content

Commit

Permalink
feat(SDK): support p2wsh format (#124)
Browse files Browse the repository at this point in the history
* feat(SDK): support p2wsh format

* resolve comments
  • Loading branch information
kevzzsk authored Jul 31, 2024
1 parent 6edd246 commit 7a2d645
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/sdk/src/addresses/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ export const addressFormats = {
p2pkh: /^[1][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2sh: /^[3][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2wpkh: /^(bc1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2wsh: /^(bc1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2tr: /^(bc1p)[a-zA-HJ-NP-Z0-9]{14,74}$/
},
testnet: {
p2pkh: /^[mn][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2sh: /^[2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2wpkh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2wsh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2tr: /^(tb1p|bcrt1p)[a-zA-HJ-NP-Z0-9]{14,74}$/
},
signet: {
p2pkh: /^[mn][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2sh: /^[2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2wpkh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2wsh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2tr: /^(tb1p|bcrt1p)[a-zA-HJ-NP-Z0-9]{14,74}$/
},
regtest: {
p2pkh: /^[mn][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2sh: /^[2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2wpkh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2wsh: /^(tb1[qp]|bcrt1[qp])[a-zA-HJ-NP-Z0-9]{14,74}$/,
p2tr: /^(tb1p|bcrt1p)[a-zA-HJ-NP-Z0-9]{14,74}$/
}
} as const

export const addressTypeToName = {
p2pkh: "legacy",
p2sh: "nested-segwit",
p2wsh: "native-segwit",
p2wpkh: "segwit",
p2tr: "taproot"
} as const
Expand All @@ -36,6 +41,7 @@ export const addressNameToType = {
legacy: "p2pkh",
segwit: "p2wpkh",
"nested-segwit": "p2sh",
"native-segwit": "p2wsh",
taproot: "p2tr"
} as const

Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/fee/FeeEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export default class FeeEstimator {
case "taproot":
return { input: 42, output: 43, txHeader: 10.5, witness: 66 } // witness size is different for non-default sigHash

case "native-segwit":
// based of 1-of-1 multisig (more other config will change the witness)
return { input: 42, output: 43, txHeader: 10.5, witness: 112.5 }

case "segwit":
return { input: 41, output: 31, txHeader: 10.5, witness: 105 }

Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AddressFormats } from ".."
export const DERIVATION_PATHS_WITHOUT_INDEX: Record<AddressFormats, string> = {
legacy: `m/44'/0'/0'/0/`,
"nested-segwit": `m/49'/0'/0'/0/`,
"native-segwit": `m/84'/0'/0'/0/`,
segwit: `m/84'/0'/0'/0/`,
taproot: `m/86'/0'/0'/0/`
}
31 changes: 29 additions & 2 deletions packages/sdk/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,29 @@ export function createTransaction(
if (type === "p2sh") {
return bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: key, network: networkObj }),
network: networkObj
network: networkObj,
...paymentOptions
})
}
if (type === "p2wsh") {
return bitcoin.payments.p2wsh({
redeem: bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: key, network: networkObj }),
network: networkObj
}),
network: networkObj,
...paymentOptions
})
}

return bitcoin.payments[type]({ pubkey: key, network: networkObj })
return bitcoin.payments[type]({ pubkey: key, network: networkObj, ...paymentOptions })
}

export function getDerivationPath(formatType: AddressFormats, account = 0, addressIndex = 0) {
const pathFormat = {
legacy: `m/44'/0'/${account}'/0/${addressIndex}`,
"nested-segwit": `m/49'/0'/${account}'/0/${addressIndex}`,
"native-segwit": `m/84'/0'/${account}'/0/${addressIndex}`,
segwit: `m/84'/0'/${account}'/0/${addressIndex}`,
taproot: `m/86'/0'/${account}'/0/${addressIndex}`
}
Expand Down Expand Up @@ -179,6 +191,13 @@ export const isP2PKH = (script: Buffer, network: Network): IsBitcoinPaymentRespo
payload: p2pkh
}
}
export const isP2WSH = (script: Buffer, network: Network): IsBitcoinPaymentResponse => {
const p2wsh = isPaymentFactory(bitcoin.payments.p2wsh, network)(script)
return {
type: "p2wsh",
payload: p2wsh
}
}
export const isP2WPKH = (script: Buffer, network: Network): IsBitcoinPaymentResponse => {
const p2wpkh = isPaymentFactory(bitcoin.payments.p2wpkh, network)(script)
return {
Expand Down Expand Up @@ -224,6 +243,14 @@ export function getScriptType(script: Buffer, network: Network): GetScriptTypeRe
}
}

const p2wsh = isP2WSH(script, network)
if (p2wsh.payload) {
return {
format: addressTypeToName["p2wsh"],
...p2wpkh
}
}

const p2sh = isP2SHScript(script, network)
if (p2sh.payload) {
return {
Expand Down

0 comments on commit 7a2d645

Please sign in to comment.