Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
add op_return parsing utilities
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Aug 19, 2020
1 parent bfe67be commit bace76b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
14 changes: 14 additions & 0 deletions contracts/Script.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ library Script {
bytes1 internal constant OP_CHECKLOCKTIMEVERIFY = 0xb1;
bytes1 internal constant OP_DROP = 0x75;
bytes1 internal constant OP_0 = 0x00;
bytes1 internal constant OP_RETURN = 0x6a;

// EXCEPTION MESSAGES
string internal constant ERR_INVALID_SIZE = 'Invalid size';
Expand Down Expand Up @@ -65,6 +66,19 @@ library Script {
return toBytes20(script.slice(2, 20));
}

function isOpReturn(bytes memory script) internal pure returns (bool) {
return script[0] == OP_RETURN;
}

function OpReturn(bytes memory script)
internal
pure
returns (bytes memory)
{
bytes memory output = script.slice(1, script.length - 1);
return output.slice(1, uint8(output[0]));
}

// 04 9f7b2a5c b1 75 76 a9 14 371c20fb2e9899338ce5e99908e64fd30b789313 88 ac
function isCLTV(bytes memory script)
internal
Expand Down
8 changes: 8 additions & 0 deletions contracts/ScriptDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ contract ScriptDelegate {
return script.P2SH();
}

function isOpReturn(bytes memory script) public pure returns (bool) {
return script.isOpReturn();
}

function OpReturn(bytes memory script) public pure returns (bytes memory) {
return script.OpReturn();
}

function isCLTV(bytes memory script)
public
pure
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/btc-relay-sol",
"version": "0.3.9",
"version": "0.3.10",
"description": "BTC Relay in Solidity",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 14 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ describe('Parser', () => {
const result = await parser.extractOutputValueAtIndex(p2pkhTx, 1);
expect(result.toNumber()).to.eq(139296477262);
});

const p2wpkhTx =
'0x01000000000101747c038ceb0a5ab9edd61d39f3d3c611cd52ccd0f519d9ad93ccf1f81a0f' +
'c5d30100000000ffffffff026400000000000000160014867a55207369ad0fe47cf3cfd2ecba' +
'ef2446c8b40000000000000000226a2000000000000000000000000000000000000000000000' +
'0000000000000000000002473044022049a2913d41700d3076cec041d1d0906fe59c6d384c4a' +
'c4e67dc2b9bdc121f300022045a0c6edb241c39b2578bc9c2cb60e60821f18e273da799010bc' +
'c2a47c3d7e4e01210290465bd783baaa9d52df3b57e31cef7df72c0cbd10afe6a10e3cfbd947' +
'7c8acf00000000';

it('should successfully extract p2wpkh output', async () => {
const result = await parser.extractOutputScriptAtIndex(p2wpkhTx, 1);
console.log(result);
});
});
15 changes: 15 additions & 0 deletions test/scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ describe('Scripts', () => {
'tb1q2krsjrpj3z6xm7xvj2xxjy9gcxa755y0exegh6'
);
});

it('should accept op_return', async () => {
const result = await parser.isOpReturn(
'0x6a200000000000000000000000000000000000000000000000000000000000000000'
);
expect(result).to.be.true;

const data = await parser.OpReturn(
'0x6a200000000000000000000000000000000000000000000000000000000000000000'
);

expect(data).to.eq(
'0x0000000000000000000000000000000000000000000000000000000000000000'
);
});
});

0 comments on commit bace76b

Please sign in to comment.