-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(abstract-utxo): implement signTx for descriptor wallets
- Loading branch information
Showing
6 changed files
with
130 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
47 changes: 47 additions & 0 deletions
47
modules/abstract-utxo/src/transaction/descriptor/signPsbt.ts
This file contains 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,47 @@ | ||
import * as utxolib from '@bitgo/utxo-lib'; | ||
import { DescriptorMap } from '../../core/descriptor'; | ||
import { findDescriptorForInput } from '../../core/descriptor/psbt/findDescriptors'; | ||
|
||
export class ErrorUnknownInput extends Error { | ||
constructor(public vin: number) { | ||
super(`missing descriptor for input ${vin}`); | ||
} | ||
} | ||
|
||
/** | ||
* Sign a PSBT with the given keychain. | ||
* | ||
* Checks the descriptor map for each input in the PSBT. If the input is not | ||
* found in the descriptor map, the behavior is determined by the `onUnknownInput` | ||
* parameter. | ||
* | ||
* | ||
* @param tx - psbt to sign | ||
* @param descriptorMap - map of input index to descriptor | ||
* @param signerKeychain - key to sign with | ||
* @param params - onUnknownInput: 'throw' | 'skip' | 'sign'. | ||
* Determines what to do when an input is not found in the | ||
* descriptor map. | ||
*/ | ||
export function signPsbt( | ||
tx: utxolib.Psbt, | ||
descriptorMap: DescriptorMap, | ||
signerKeychain: utxolib.BIP32Interface, | ||
params: { | ||
onUnknownInput: 'throw' | 'skip' | 'sign'; | ||
} | ||
): void { | ||
for (const [vin, input] of tx.data.inputs.entries()) { | ||
if (!findDescriptorForInput(input, descriptorMap)) { | ||
switch (params.onUnknownInput) { | ||
case 'skip': | ||
continue; | ||
case 'throw': | ||
throw new ErrorUnknownInput(vin); | ||
case 'sign': | ||
break; | ||
} | ||
} | ||
tx.signInputHD(vin, signerKeychain); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,30 @@ | ||
import { mockPsbtDefaultWithDescriptorTemplate } from '../../core/descriptor/psbt/mock.utils'; | ||
import { signPsbt } from '../../../src/transaction/descriptor'; | ||
import { getKeyTriple } from '../../core/key.utils'; | ||
import { getDescriptorMap } from '../../core/descriptor/descriptor.utils'; | ||
import assert from 'assert'; | ||
import { ErrorUnknownInput } from '../../../src/transaction/descriptor/signPsbt'; | ||
|
||
describe('sign', function () { | ||
const psbtUnsigned = mockPsbtDefaultWithDescriptorTemplate('Wsh2Of3'); | ||
const keychain = getKeyTriple('a'); | ||
const descriptorMap = getDescriptorMap('Wsh2Of3', keychain); | ||
const emptyDescriptorMap = new Map(); | ||
|
||
it('should sign a transaction', async function () { | ||
const psbt = psbtUnsigned.clone(); | ||
signPsbt(psbt, descriptorMap, keychain[0], { onUnknownInput: 'throw' }); | ||
assert(psbt.validateSignaturesOfAllInputs()); | ||
}); | ||
|
||
it('should be sensitive to onUnknownInput', async function () { | ||
const psbt = psbtUnsigned.clone(); | ||
assert.throws(() => { | ||
signPsbt(psbt, emptyDescriptorMap, keychain[0], { onUnknownInput: 'throw' }); | ||
}, new ErrorUnknownInput(0)); | ||
signPsbt(psbt, emptyDescriptorMap, keychain[0], { onUnknownInput: 'skip' }); | ||
assert(psbt.data.inputs[0].partialSig === undefined); | ||
signPsbt(psbt, emptyDescriptorMap, keychain[0], { onUnknownInput: 'sign' }); | ||
assert(psbt.validateSignaturesOfAllInputs()); | ||
}); | ||
}); |