-
Notifications
You must be signed in to change notification settings - Fork 302
fix: test cases for parseWithdrawPsbt #7750
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
modules/abstract-lightning/test/unit/lightning/createPsbt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import * as utxolib from '@bitgo/utxo-lib'; | ||
|
|
||
| export interface PsbtCreationOptions { | ||
| network: utxolib.Network; | ||
| inputValue?: number; | ||
| outputValue?: number; | ||
| outputAddress?: string; | ||
| changeValue?: number; | ||
| changeDerivationPath?: string; | ||
| changePurpose?: 49 | 84 | 86; | ||
| includeChangeOutput?: boolean; | ||
| masterKey?: utxolib.BIP32Interface; // Optional master key for deriving addresses | ||
| } | ||
|
|
||
| export interface PsbtCreationResult { | ||
| psbt: utxolib.Psbt; | ||
| masterKey: utxolib.BIP32Interface; | ||
| changeDerivationPath: string; | ||
| changePurpose: number; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a PSBT for testing purposes with customizable options. | ||
| * This helper function generates a PSBT with a fake input and configurable outputs. | ||
| * | ||
| * @param options - Configuration options for PSBT creation | ||
| * @returns A constructed PSBT instance and the master key used | ||
| */ | ||
| export function createTestPsbt(options: PsbtCreationOptions): PsbtCreationResult { | ||
| const { | ||
| network, | ||
| inputValue = 500000, | ||
| outputValue = 100000, | ||
| outputAddress, | ||
| changeValue, | ||
| changeDerivationPath = "m/84'/0'/0'/1/6", | ||
| changePurpose = 84, | ||
| includeChangeOutput = true, | ||
| masterKey, | ||
| } = options; | ||
| const fixedSeed = Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex'); | ||
| const accountMasterKey = masterKey || utxolib.bip32.fromSeed(fixedSeed, network); | ||
|
|
||
| const inputPrivateKey = Buffer.from('0202020202020202020202020202020202020202020202020202020202020202', 'hex'); | ||
| const inputKeyPair = utxolib.ECPair.fromPrivateKey(inputPrivateKey, { network }); | ||
| const p2wpkhInput = utxolib.payments.p2wpkh({ | ||
| pubkey: Buffer.from(inputKeyPair.publicKey), | ||
| network, | ||
| }); | ||
|
|
||
| // Create a new PSBT instance | ||
| const psbt = new utxolib.Psbt({ network }); | ||
|
|
||
| // Add a fake input to the PSBT | ||
| const fakeTxId = 'ca6852598b48230ac870814b935b0d982d3968eb00a1d97332dceb6cd9b8505e'; | ||
| const fakeVout = 1; | ||
|
|
||
| psbt.addInput({ | ||
| hash: fakeTxId, | ||
| index: fakeVout, | ||
| witnessUtxo: { | ||
| script: p2wpkhInput.output!, | ||
| value: BigInt(inputValue), | ||
| }, | ||
| bip32Derivation: [ | ||
| { | ||
| masterFingerprint: Buffer.alloc(4, 0), | ||
| path: "m/84'/0'/0'/0/0", | ||
| pubkey: Buffer.from(inputKeyPair.publicKey), | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // Add recipient output | ||
| let recipientAddress: string; | ||
| if (outputAddress) { | ||
| recipientAddress = outputAddress; | ||
| } else { | ||
| const recipientPrivateKey = Buffer.from('0303030303030303030303030303030303030303030303030303030303030303', 'hex'); | ||
| const recipientKeyPair = utxolib.ECPair.fromPrivateKey(recipientPrivateKey, { network }); | ||
| // P2TR requires x-only pubkey (32 bytes, without the prefix byte) | ||
| const xOnlyPubkey = | ||
| recipientKeyPair.publicKey.length === 33 | ||
| ? recipientKeyPair.publicKey.subarray(1, 33) | ||
| : recipientKeyPair.publicKey; | ||
| const recipientP2tr = utxolib.payments.p2tr( | ||
| { | ||
| pubkey: xOnlyPubkey, | ||
| network, | ||
| }, | ||
| { eccLib: utxolib.ecc } | ||
| ); | ||
| recipientAddress = recipientP2tr.address!; | ||
| } | ||
|
|
||
| psbt.addOutput({ | ||
| address: recipientAddress, | ||
| value: BigInt(outputValue), | ||
| }); | ||
|
|
||
| // Add change output if requested | ||
| if (includeChangeOutput) { | ||
| const calculatedChangeValue = changeValue !== undefined ? changeValue : inputValue - outputValue - 10000; // 10k sats fee | ||
|
|
||
| // Parse the derivation path to get the change and address indices | ||
| // Expected format: m/purpose'/coin_type'/account'/change/address_index | ||
| const pathSegments = changeDerivationPath.split('/'); | ||
| const changeIndex = Number(pathSegments[pathSegments.length - 2]); | ||
| const addressIndex = Number(pathSegments[pathSegments.length - 1]); | ||
|
|
||
| // Derive the change key from the master key | ||
| const changeNode = accountMasterKey.derive(changeIndex).derive(addressIndex); | ||
| const changePubkey = changeNode.publicKey; | ||
|
|
||
| let changeAddress: string; | ||
| let changePayment; | ||
|
|
||
| // Create change address based on purpose | ||
| switch (changePurpose) { | ||
| case 49: // P2SH-P2WPKH | ||
| changePayment = utxolib.payments.p2sh({ | ||
| redeem: utxolib.payments.p2wpkh({ | ||
| pubkey: changePubkey, | ||
| network, | ||
| }), | ||
| network, | ||
| }); | ||
| changeAddress = changePayment.address!; | ||
| break; | ||
| case 84: // P2WPKH | ||
| changePayment = utxolib.payments.p2wpkh({ | ||
| pubkey: changePubkey, | ||
| network, | ||
| }); | ||
| changeAddress = changePayment.address!; | ||
| break; | ||
| case 86: // P2TR | ||
| const xOnlyChangePubkey = changePubkey.length === 33 ? changePubkey.subarray(1, 33) : changePubkey; | ||
| changePayment = utxolib.payments.p2tr( | ||
| { | ||
| pubkey: xOnlyChangePubkey, | ||
| network, | ||
| }, | ||
| { eccLib: utxolib.ecc } | ||
| ); | ||
| changeAddress = changePayment.address!; | ||
| break; | ||
| default: | ||
| throw new Error(`Unsupported purpose: ${changePurpose}`); | ||
| } | ||
|
|
||
| psbt.addOutput({ | ||
| address: changeAddress, | ||
| value: BigInt(calculatedChangeValue), | ||
| }); | ||
|
|
||
| // Add bip32Derivation to the change output | ||
| psbt.updateOutput(1, { | ||
| bip32Derivation: [ | ||
| { | ||
| masterFingerprint: Buffer.alloc(4, 0), | ||
| path: changeDerivationPath, | ||
| pubkey: changePubkey, | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| psbt, | ||
| masterKey: accountMasterKey, | ||
| changeDerivationPath, | ||
| changePurpose, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.