-
Notifications
You must be signed in to change notification settings - Fork 300
feat: Verify BIP322 proofs in both transaction and psbt formats #6878
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './toSpend'; | ||
| export * from './toSign'; | ||
| export * from './utils'; | ||
| export * from './verify'; |
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
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,140 @@ | ||
| import * as assert from 'assert'; | ||
|
|
||
| import * as utxolib from '@bitgo/utxo-lib'; | ||
|
|
||
| import { buildToSpendTransaction } from './toSpend'; | ||
|
|
||
| export type MessageInfo = { | ||
| address: string; | ||
| message: string; | ||
| // Hex encoded pubkeys | ||
| pubkeys: string[]; | ||
| scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3; | ||
| }; | ||
|
|
||
| export function assertBaseTx(tx: utxolib.bitgo.UtxoTransaction<bigint>): void { | ||
| assert.deepStrictEqual(tx.version, 0, 'Transaction version must be 0.'); | ||
| assert.deepStrictEqual(tx.locktime, 0, 'Transaction locktime must be 0.'); | ||
| assert.deepStrictEqual(tx.outs.length, 1, 'Transaction must have exactly 1 output.'); | ||
| assert.deepStrictEqual(tx.outs[0].value, BigInt(0), 'Transaction output value must be 0.'); | ||
| assert.deepStrictEqual(tx.outs[0].script.toString('hex'), '6a', 'Transaction output script must be OP_RETURN.'); | ||
| } | ||
|
|
||
| export function assertTxInput( | ||
| tx: utxolib.bitgo.UtxoTransaction<bigint>, | ||
| inputIndex: number, | ||
| prevOuts: utxolib.TxOutput<bigint>[], | ||
| info: MessageInfo, | ||
| checkSignature: boolean | ||
| ): void { | ||
| assert.ok( | ||
| inputIndex < tx.ins.length, | ||
| `inputIndex ${inputIndex} is out of range for tx with ${tx.ins.length} inputs.` | ||
| ); | ||
| const input = tx.ins[inputIndex]; | ||
| assert.deepStrictEqual(input.index, 0, `transaction input ${inputIndex} must have index=0.`); | ||
| assert.deepStrictEqual(input.sequence, 0, `transaction input ${inputIndex} sequence must be 0.`); | ||
|
|
||
| // Make sure that the message is correctly encoded into the input of the transaction and | ||
| // verify that the message info corresponds | ||
| const scriptPubKey = utxolib.bitgo.outputScripts.createOutputScript2of3( | ||
| info.pubkeys.map((pubkey) => Buffer.from(pubkey, 'hex')), | ||
| info.scriptType, | ||
| tx.network | ||
| ).scriptPubKey; | ||
| assert.deepStrictEqual( | ||
| info.address, | ||
| utxolib.address.fromOutputScript(scriptPubKey, tx.network).toString(), | ||
| `Address does not match derived scriptPubKey for input ${inputIndex}.` | ||
| ); | ||
|
|
||
| const txid = utxolib.bitgo.getOutputIdForInput(input).txid; | ||
| const toSpendTx = buildToSpendTransaction(scriptPubKey, info.message); | ||
| assert.deepStrictEqual( | ||
| txid, | ||
| toSpendTx.getId(), | ||
| `Input ${inputIndex} derived to_spend transaction is not encoded in the input.` | ||
| ); | ||
|
|
||
| if (checkSignature) { | ||
| const signatureScript = utxolib.bitgo.parseSignatureScript2Of3(input); | ||
| const scriptType = | ||
| signatureScript.scriptType === 'taprootKeyPathSpend' | ||
| ? 'p2trMusig2' | ||
| : signatureScript.scriptType === 'taprootScriptPathSpend' | ||
| ? 'p2tr' | ||
| : signatureScript.scriptType; | ||
| assert.deepStrictEqual(scriptType, info.scriptType, 'Script type does not match.'); | ||
| utxolib.bitgo.verifySignatureWithPublicKeys( | ||
| tx, | ||
| inputIndex, | ||
| prevOuts, | ||
| info.pubkeys.map((pubkey) => Buffer.from(pubkey, 'hex')) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export function assertBip322TxProof(tx: utxolib.bitgo.UtxoTransaction<bigint>, messageInfo: MessageInfo[]): void { | ||
| assertBaseTx(tx); | ||
| assert.deepStrictEqual( | ||
| tx.ins.length, | ||
| messageInfo.length, | ||
| 'Transaction must have the same number of inputs as messageInfo entries.' | ||
| ); | ||
| const prevOuts = messageInfo.map((info) => { | ||
| return { | ||
| value: 0n, | ||
| script: utxolib.bitgo.outputScripts.createOutputScript2of3( | ||
| info.pubkeys.map((pubkey) => Buffer.from(pubkey, 'hex')), | ||
| info.scriptType, | ||
| tx.network | ||
| ).scriptPubKey, | ||
| }; | ||
| }); | ||
| tx.ins.forEach((input, inputIndex) => assertTxInput(tx, inputIndex, prevOuts, messageInfo[inputIndex], true)); | ||
| } | ||
|
|
||
| export function assertBip322PsbtProof(psbt: utxolib.bitgo.UtxoPsbt, messageInfo: MessageInfo[]): void { | ||
| const unsignedTx = psbt.getUnsignedTx(); | ||
|
|
||
| assertBaseTx(unsignedTx); | ||
| assert.deepStrictEqual( | ||
| psbt.data.inputs.length, | ||
| messageInfo.length, | ||
| 'PSBT must have the same number of inputs as messageInfo entries.' | ||
| ); | ||
|
|
||
| const prevOuts = psbt.data.inputs.map((input, inputIndex) => { | ||
| assert.ok(input.witnessUtxo, `PSBT input ${inputIndex} is missing witnessUtxo`); | ||
| return input.witnessUtxo; | ||
| }); | ||
|
|
||
| psbt.data.inputs.forEach((input, inputIndex) => { | ||
| // Check that the metadata in the PSBT matches the messageInfo, then check the input data | ||
| const info = messageInfo[inputIndex]; | ||
|
|
||
| // Check that the to_spend transaction is encoded in the nonWitnessUtxo | ||
| assert.ok(input.nonWitnessUtxo, `PSBT input ${inputIndex} is missing nonWitnessUtxo`); | ||
| const toSpendTx = buildToSpendTransaction(prevOuts[inputIndex].script, info.message); | ||
| assert.deepStrictEqual(input.nonWitnessUtxo.toString('hex'), toSpendTx.toHex()); | ||
|
|
||
| if (input.bip32Derivation) { | ||
| input.bip32Derivation.forEach((b) => { | ||
| const pubkey = b.pubkey.toString('hex'); | ||
| assert.ok( | ||
| info.pubkeys.includes(pubkey), | ||
| `PSBT input ${inputIndex} has a pubkey in (tap)bip32Derivation that is not in messageInfo` | ||
| ); | ||
| }); | ||
| } else if (!input.tapBip32Derivation) { | ||
| throw new Error(`PSBT input ${inputIndex} is missing (tap)bip32Derivation when it should have it.`); | ||
lcovar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Verify the signature on the input | ||
| assert.ok(psbt.validateSignaturesOfInputCommon(inputIndex), `PSBT input ${inputIndex} has an invalid signature.`); | ||
|
|
||
| // Do not check the signature when using the PSBT, the signature is not there. We are going | ||
| // to signatures in the PSBT. | ||
| assertTxInput(unsignedTx, inputIndex, prevOuts, info, false); | ||
| }); | ||
| } | ||
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.