Skip to content

Commit

Permalink
refactor: throw error when OP_RETURN is too big
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Aug 22, 2024
1 parent e13ea32 commit 474315e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gobob/bob-snap",
"version": "2.2.1",
"version": "2.2.2",
"description": "BOB: Metamask snap to manage your BTC, ordinals, and more",
"contributors": [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "2.2.1",
"version": "2.2.2",
"description": "BOB: Metamask snap to manage your Bitcoin",
"proposedName": "BOB",
"repository": {
"type": "git",
"url": "https://github.com/bob-collective/bob-snap"
},
"source": {
"shasum": "HHxMGzE7qqYdjyYNB6L+2droaS0ElKFS1CIyDl4pIo0=",
"shasum": "tUeqZ/Wk3Ot1VfrXoGa8QVw1dtLO3Sm1kQ3/3+UVjIk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/snap/src/bitcoin/PsbtValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class PsbtValidator {
} else {
const scriptPubKey = script.decompile(this.psbt.txOutputs[index].script);
if (scriptPubKey.length == 2 && scriptPubKey[0] == opcodes.OP_RETURN && Buffer.isBuffer(scriptPubKey[1])) {
if (scriptPubKey[1].byteLength > 80) {
// miners will reject anything over 80 bytes
this.error = SnapError.of(PsbtValidateErrors.InvalidOpReturn);
}
// as an exception we allow OP_RETURN outputs
return true;
}
Expand Down
13 changes: 12 additions & 1 deletion packages/snap/src/bitcoin/__tests__/PsbtValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('psbtValidator', () => {
expect(psbtValidator.validate(signer)).toBe(true);
});

it('should return true given a valid psbt with op return', function () {
it('should return true given a valid psbt with OP_RETURN', function () {
const psbt = Psbt.fromBase64(psbtFixture.base64, { network: networks.testnet })
psbt.addOutput({
script: script.compile([opcodes.OP_RETURN, Buffer.alloc(20, 0)]),
Expand All @@ -202,4 +202,15 @@ describe('psbtValidator', () => {
const psbtValidator = new PsbtValidator(psbt, BitcoinNetwork.Test);
expect(psbtValidator.validate(signer)).toBe(true);
});

it('should throw error when OP_RETURN is too big', function () {
const psbt = Psbt.fromBase64(psbtFixture.base64, { network: networks.testnet })
psbt.addOutput({
script: script.compile([opcodes.OP_RETURN, Buffer.alloc(81, 0)]),
value: 0,
})

const psbtValidator = new PsbtValidator(psbt, BitcoinNetwork.Test);
expect(() => { psbtValidator.validate(signer) }).toThrowError(`Transaction has an invalid OP_RETURN`);
});
});
4 changes: 4 additions & 0 deletions packages/snap/src/errors/constant/PsbtValidateErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ export const PsbtValidateErrors = {
AmountNotMatch: {
code: 10007,
message: 'Transaction input amount not match'
},
InvalidOpReturn: {
code: 10008,
message: 'Transaction has an invalid OP_RETURN'
}
}

0 comments on commit 474315e

Please sign in to comment.