-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './descriptor'; | ||
export * from './transaction'; |
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,96 @@ | ||
// Source: https://docs.coredao.org/docs/Learn/products/btc-staking/design | ||
export const CORE_DAO_TESTNET_CHAIN_ID = Buffer.from([0x1115]); | ||
export const CORE_DAO_MAINNET_CHAIN_ID = Buffer.from([0x1116]); | ||
export const CORE_DAO_SATOSHI_PLUS_IDENTIFIER = Buffer.from([0x5341542b]); | ||
|
||
/** As of v2, this is the construction of the OP_RETURN: | ||
* Source: https://docs.coredao.org/docs/Learn/products/btc-staking/design#op_return-output | ||
* | ||
* The OP_RETURN output should contain all staking information in order, and be composed in the following format: | ||
* | ||
* OP_RETURN: identifier 0x6a | ||
* LENGTH: which represents the total byte length after the OP_RETURN opcode. Note that all data has to be pushed with its appropriate size byte(s). | ||
* Satoshi Plus Identifier: (SAT+) 4 bytes | ||
* Version: (0x01) 1 byte | ||
* Chain ID: (0x1115 for Core Testnet and 0x1116 for Core Mainnet) 2 bytes | ||
* Delegator: The Core address to receive rewards, 20 bytes | ||
* Validator: The Core validator address to stake to, 20 bytes | ||
* Fee: Fee for relayer, 1 byte, range [0,255], measured in CORE | ||
* (Optional) RedeemScript | ||
* (Optional) Timelock: 4 bytes | ||
* | ||
* Either RedeemScript or Timelock must be available, the purpose is to allow relayer to | ||
* obtain the RedeemScript and submit transactions on Core. If a RedeemScript is provided, | ||
* relayer will use it directly. Otherwise, relayer will construct the redeem script based | ||
* on the timelock and the information in the transaction inputs. | ||
* | ||
* @returns Buffer OP_RETURN buffer | ||
*/ | ||
export function createCoreDaoOpReturnBuffer({ | ||
version, | ||
chainId, | ||
delegator, | ||
validator, | ||
fee, | ||
redeemScript, | ||
timelock, | ||
}: { | ||
version: number; | ||
chainId: string; | ||
delegator: string; | ||
validator: string; | ||
fee: string; | ||
redeemScript?: string; | ||
timelock?: number; | ||
}): Buffer { | ||
const versionBuffer = Buffer.alloc(1, version); | ||
|
||
const chainIdBuffer = Buffer.from(chainId, 'hex'); | ||
if (!(chainIdBuffer.equals(CORE_DAO_TESTNET_CHAIN_ID) || chainIdBuffer.equals(CORE_DAO_MAINNET_CHAIN_ID))) { | ||
throw new Error('Invalid chain ID'); | ||
} | ||
const delegatorBuffer = Buffer.from(delegator, 'hex'); | ||
if (delegatorBuffer.length !== 20) { | ||
throw new Error('Invalid delegator address'); | ||
} | ||
|
||
const validatorBuffer = Buffer.from(validator, 'hex'); | ||
if (validatorBuffer.length !== 20) { | ||
throw new Error('Invalid validator address'); | ||
} | ||
|
||
const feeBuffer = Buffer.from(fee, 'hex'); | ||
if (feeBuffer.length !== 1) { | ||
throw new Error('Invalid fee'); | ||
} | ||
|
||
const redeemScriptBuffer = Buffer.from(redeemScript ?? '', 'hex'); | ||
const timelockBuffer = timelock ? Buffer.alloc(4, timelock) : Buffer.from([]); | ||
|
||
const totalLength = | ||
CORE_DAO_SATOSHI_PLUS_IDENTIFIER.length + | ||
versionBuffer.length + | ||
chainIdBuffer.length + | ||
delegatorBuffer.length + | ||
validatorBuffer.length + | ||
feeBuffer.length + | ||
redeemScriptBuffer.length + | ||
timelockBuffer.length; | ||
|
||
if (totalLength > 76) { | ||
throw new Error('OP_RETURN data exceeds maximum length'); | ||
} | ||
|
||
return Buffer.concat([ | ||
Buffer.from([0x6a]), | ||
Buffer.alloc(1, totalLength), | ||
CORE_DAO_SATOSHI_PLUS_IDENTIFIER, | ||
versionBuffer, | ||
chainIdBuffer, | ||
delegatorBuffer, | ||
validatorBuffer, | ||
feeBuffer, | ||
redeemScriptBuffer, | ||
timelockBuffer, | ||
]); | ||
} |