@@ -4,12 +4,11 @@ pragma solidity 0.8.26;
4
4
library CorsaBitcoin {
5
5
address private constant BTC_PRECOMPILE = address (0x999 );
6
6
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 ;
13
12
14
13
struct Output {
15
14
string addr;
@@ -33,39 +32,50 @@ library CorsaBitcoin {
33
32
34
33
error PrecompileCallFailed ();
35
34
36
- function decodeBitcoinTx (bytes calldata signedTx ) internal view returns (BitcoinTx memory ) {
35
+ function decodeBitcoinTx (bytes memory signedTx ) internal view returns (BitcoinTx memory ) {
37
36
(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));
39
38
if (! success) revert PrecompileCallFailed ();
40
39
return abi.decode (returndata, (BitcoinTx));
41
40
}
42
41
43
42
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));
45
44
return success;
46
45
}
47
46
48
47
function convertEthToBtcAddress (address ethAddress ) internal returns (bytes memory ) {
49
48
(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));
51
50
if (! success) revert PrecompileCallFailed ();
52
51
return returndata;
53
52
}
54
53
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 ();
59
58
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;
63
67
}
64
68
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);
68
77
if (! success) revert PrecompileCallFailed ();
69
- return abi.decode (returndata, (uint256 ));
78
+
79
+ return returndata;
70
80
}
71
81
}
0 commit comments