Skip to content

Commit da9401e

Browse files
Merge pull request #5076 from BitGo/BTC-1578-follow-up
chore(utxo-coredao): force there to only be timelock or redeem script
2 parents cfba04b + aed3a91 commit da9401e

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

modules/utxo-coredao/src/transaction.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ export const CORE_DAO_TESTNET_CHAIN_ID = Buffer.alloc(2, 0x1115);
33
export const CORE_DAO_MAINNET_CHAIN_ID = Buffer.alloc(2, 0x1116);
44
export const CORE_DAO_SATOSHI_PLUS_IDENTIFIER = Buffer.alloc(4, 0x5341542b);
55

6+
type BaseParams = {
7+
version: number;
8+
chainId: Buffer;
9+
delegator: Buffer;
10+
validator: Buffer;
11+
fee: number;
12+
};
13+
14+
type OpReturnParams = BaseParams & ({ redeemScript: Buffer } | { timelock: number });
15+
616
/**
717
* Create a CoreDAO OP_RETURN output script
818
*
@@ -21,17 +31,8 @@ export function createCoreDaoOpReturnOutputScript({
2131
delegator,
2232
validator,
2333
fee,
24-
redeemScript,
25-
timelock,
26-
}: {
27-
version: number;
28-
chainId: Buffer;
29-
delegator: Buffer;
30-
validator: Buffer;
31-
fee: number;
32-
redeemScript?: Buffer;
33-
timelock?: number;
34-
}): Buffer {
34+
...rest
35+
}: OpReturnParams): Buffer {
3536
/**
3637
* As of v2, this is the construction of the OP_RETURN:
3738
* Source: https://docs.coredao.org/docs/Learn/products/btc-staking/design#op_return-output
@@ -83,14 +84,11 @@ export function createCoreDaoOpReturnOutputScript({
8384
throw new Error('Invalid fee');
8485
}
8586

86-
if (!redeemScript && !timelock) {
87-
throw new Error('Either redeemScript or timelock must be provided');
88-
}
89-
const redeemScriptBuffer = redeemScript ?? Buffer.from([]);
90-
if (timelock && (timelock < 0 || timelock > 4294967295)) {
87+
const redeemScriptBuffer = 'redeemScript' in rest ? rest.redeemScript : Buffer.from([]);
88+
if ('timelock' in rest && (rest.timelock < 0 || rest.timelock > 4294967295)) {
9189
throw new Error('Invalid timelock - out of range');
9290
}
93-
const timelockBuffer = timelock ? Buffer.alloc(4, timelock).reverse() : Buffer.from([]);
91+
const timelockBuffer = 'timelock' in rest ? Buffer.alloc(4, rest.timelock).reverse() : Buffer.from([]);
9492

9593
const totalLength =
9694
CORE_DAO_SATOSHI_PLUS_IDENTIFIER.length +

modules/utxo-coredao/test/unit/transaction.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ describe('OP_RETURN', function () {
8080
timelock: -1,
8181
})
8282
);
83-
assert.throws(() =>
84-
createCoreDaoOpReturnOutputScript({
85-
version: validVersion,
86-
chainId: validChainId,
87-
delegator: validDelegator,
88-
validator: validValidator,
89-
fee: validFee,
90-
})
91-
);
9283
});
9384

9485
it('should return a buffer with the correct length', function () {

0 commit comments

Comments
 (0)