@@ -4,12 +4,11 @@ pragma solidity 0.8.26;
44library CorsaBitcoin {
55 address private constant BTC_PRECOMPILE = address (0x999 );
66
7- bytes4 private constant BROADCAST_LEADING_BYTE = 0x00000000 ;
8- bytes4 private constant GET_BLOCK_HEIGHT_LEADING_BYTE = 0x00000001 ;
9- bytes4 private constant DECODE_LEADING_BYTE = 0x00000002 ;
10- bytes4 private constant CHECKSIG_LEADING_BYTE = 0x00000003 ;
11- bytes4 private constant ADDRESS_CONVERT_LEADING_BYTE = 0x00000004 ;
12- bytes4 private constant SEND_BTC_LEADING_BYTE = 0x00000005 ;
7+ bytes4 private constant BROADCAST_LEADING_BYTES = 0x00000001 ;
8+ bytes4 private constant DECODE_LEADING_BYTES = 0x00000002 ;
9+ bytes4 private constant CHECKSIG_LEADING_BYTES = 0x00000003 ;
10+ bytes4 private constant ADDRESS_CONVERT_LEADING_BYTES = 0x00000004 ;
11+ bytes4 private constant CREATE_AND_SIGN_LEADING_BYTES = 0x00000005 ;
1312
1413 struct Output {
1514 string addr;
@@ -33,39 +32,50 @@ library CorsaBitcoin {
3332
3433 error PrecompileCallFailed ();
3534
36- function decodeBitcoinTx (bytes calldata signedTx ) internal view returns (BitcoinTx memory ) {
35+ function decodeBitcoinTx (bytes memory signedTx ) internal view returns (BitcoinTx memory ) {
3736 (bool success , bytes memory returndata ) =
38- BTC_PRECOMPILE.staticcall (abi.encodePacked (DECODE_LEADING_BYTE , signedTx));
37+ BTC_PRECOMPILE.staticcall (abi.encodePacked (DECODE_LEADING_BYTES , signedTx));
3938 if (! success) revert PrecompileCallFailed ();
4039 return abi.decode (returndata, (BitcoinTx));
4140 }
4241
4342 function checkSignature (bytes calldata signedTx ) internal view returns (bool ) {
44- (bool success ,) = BTC_PRECOMPILE.staticcall (abi.encodePacked (CHECKSIG_LEADING_BYTE , signedTx));
43+ (bool success ,) = BTC_PRECOMPILE.staticcall (abi.encodePacked (CHECKSIG_LEADING_BYTES , signedTx));
4544 return success;
4645 }
4746
4847 function convertEthToBtcAddress (address ethAddress ) internal returns (bytes memory ) {
4948 (bool success , bytes memory returndata ) =
50- BTC_PRECOMPILE.call (abi.encodePacked (ADDRESS_CONVERT_LEADING_BYTE , ethAddress));
49+ BTC_PRECOMPILE.call (abi.encodePacked (ADDRESS_CONVERT_LEADING_BYTES , ethAddress));
5150 if (! success) revert PrecompileCallFailed ();
5251 return returndata;
5352 }
5453
55- function broadcastBitcoinTx (bytes calldata signedTx ) internal returns (bool ) {
56- (bool success ,) = BTC_PRECOMPILE. call ( abi.encodePacked (BROADCAST_LEADING_BYTE, signedTx));
57- return success ;
58- }
54+ function broadcastBitcoinTx (bytes memory signedTx ) internal returns (bytes32 ) {
55+ (bool success , bytes memory returndata ) =
56+ BTC_PRECOMPILE. call ( abi.encodePacked (BROADCAST_LEADING_BYTES, signedTx)) ;
57+ if ( ! success) revert PrecompileCallFailed ();
5958
60- function sendBitcoin (address from , uint256 amount , string calldata destination ) internal returns (bool ) {
61- (bool success ,) = BTC_PRECOMPILE.call (abi.encodePacked (SEND_BTC_LEADING_BYTE, from, amount, destination));
62- return success;
59+ require (returndata.length == 32 , "Invalid txid length " );
60+
61+ bytes32 txid;
62+ assembly {
63+ txid := mload (add (returndata, 32 ))
64+ }
65+
66+ return txid;
6367 }
6468
65- function getCurrentBlockHeight () internal view returns (uint256 ) {
66- (bool success , bytes memory returndata ) =
67- BTC_PRECOMPILE.staticcall (abi.encodePacked (GET_BLOCK_HEIGHT_LEADING_BYTE));
69+ function createAndSignBitcoinTx (address signer , uint64 amount , uint64 blockHeight , string memory destinationAddress )
70+ internal
71+ returns (bytes memory )
72+ {
73+ bytes memory inputData =
74+ abi.encode (CREATE_AND_SIGN_LEADING_BYTES, signer, amount, blockHeight, destinationAddress);
75+
76+ (bool success , bytes memory returndata ) = BTC_PRECOMPILE.call (inputData);
6877 if (! success) revert PrecompileCallFailed ();
69- return abi.decode (returndata, (uint256 ));
78+
79+ return returndata;
7080 }
7181}
0 commit comments