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(SDK): support p2wsh format #124

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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/`
}
25 changes: 25 additions & 0 deletions packages/sdk/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export function createTransaction(
network: networkObj
})
}
if (type === "p2wsh") {
return bitcoin.payments.p2wsh({
redeem: bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: key, network: networkObj }),
network: networkObj
}),
network: networkObj
})
}
dcorral marked this conversation as resolved.
Show resolved Hide resolved

return bitcoin.payments[type]({ pubkey: key, network: networkObj })
}
Expand All @@ -54,6 +63,7 @@ export function getDerivationPath(formatType: AddressFormats, account = 0, addre
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 +189,13 @@ export const isP2PKH = (script: Buffer, network: Network): IsBitcoinPaymentRespo
payload: p2pkh
}
}
export const isP2WSH = (script: Buffer, network: Network): IsBitcoinPaymentResponse => {
const p2wpkh = isPaymentFactory(bitcoin.payments.p2wsh, network)(script)
dcorral marked this conversation as resolved.
Show resolved Hide resolved
return {
type: "p2wsh",
payload: p2wpkh
}
}
export const isP2WPKH = (script: Buffer, network: Network): IsBitcoinPaymentResponse => {
const p2wpkh = isPaymentFactory(bitcoin.payments.p2wpkh, network)(script)
return {
Expand Down Expand Up @@ -224,6 +241,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
Loading