-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitcoin.sol
194 lines (141 loc) · 5.78 KB
/
Bitcoin.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
pragma solidity ^0.5.0;
contract Bitcoin {
bytes32 constant private mask4 = 0xffffffff00000000000000000000000000000000000000000000000000000000;
bytes constant private ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
bytes1 private network;
bytes1 private networkPrefix;
address private recoverPublicKey;
mapping (string => mapping(bytes32 => bytes)) private signatures;
event RevealedSignature(bytes signature, bytes32 messageHash, string witness);
constructor(address ecrecoverContract, bytes1 networkByte, bytes1 networkPrefixByte) public {
recoverPublicKey = ecrecoverContract;
network = networkByte;
networkPrefix = networkPrefixByte;
}
function getSignature(string memory witness, bytes32 messageHash)
public view returns (bytes memory signature)
{
signature = signatures[witness][messageHash];
}
function saveSignature(
bytes32 messageHash,
bytes memory signature
)
public
{
string memory bitcoinAddress = recoverBitcoinAddress(messageHash, signature);
require(getSignature(bitcoinAddress, messageHash).length == 0);
signatures[bitcoinAddress][messageHash] = signature;
emit RevealedSignature(signature, messageHash, bitcoinAddress);
}
function recoverBitcoinAddress(bytes32 hash, bytes memory signature)
internal returns (string memory)
{
bytes memory compressedPublicKey = recoverCompressedPublicKey(hash, signature);
string memory bitcoinAddress = getBitcoinAddress(compressedPublicKey);
return bitcoinAddress;
}
function recoverCompressedPublicKey(bytes32 hash, bytes memory signature)
internal returns (bytes memory)
{
(bytes32 publicKeyX, bytes32 publicKeyY) = recoverPublicKeyCoordinates(hash, signature);
uint8 publicKeyPrefix;
if (isEven(uint256(publicKeyY))) {
publicKeyPrefix = 2;
} else {
publicKeyPrefix = 3;
}
bytes memory compressedPublicKey = abi.encodePacked(publicKeyPrefix, publicKeyX);
return compressedPublicKey;
}
function recoverPublicKeyCoordinates(bytes32 hash, bytes memory signature)
internal returns (bytes32 publicKeyX, bytes32 publicKeyY)
{
address recoverPublicKeyAddress = recoverPublicKey;
bytes4 recoverPublicKeySignature = bytes4(keccak256("ecrecover(uint256,uint256,uint256,uint256)"));
assembly {
let r := mload(add(signature, 0x20))
let s := mload(add(signature, 0x40))
let v := byte(0, mload(add(signature, 0x60)))
let recoverInput := mload(0x40)
mstore(recoverInput, recoverPublicKeySignature)
mstore(add(recoverInput, 0x04), hash)
mstore(add(recoverInput, 0x24), v)
mstore(add(recoverInput, 0x44), r)
mstore(add(recoverInput, 0x64), s)
let recoveredPublicKey := mload(0x40)
if iszero(call(sub(gas, 2000), recoverPublicKeyAddress, 0, recoverInput, 0x84, recoveredPublicKey, 0x80)) {
revert(0, 0)
}
publicKeyX := mload(add(recoveredPublicKey, 0x40))
publicKeyY := mload(add(recoveredPublicKey, 0x60))
mstore(0x40, add(recoverInput, 0x84))
}
}
function isEven(uint256 number)
public pure returns (bool)
{
if (number & 1 == 0) {
return true;
} else {
return false;
}
}
function getBitcoinAddress(bytes memory publicKey) public view returns (string memory bitcoinAddress) {
bytes20 publicKeyHash = ripemd160(abi.encodePacked(sha256(abi.encodePacked(publicKey))));
bytes32 checksumFull = sha256(abi.encodePacked(sha256(abi.encodePacked(network, publicKeyHash))));
bytes4 checksumSliced = bytes4(checksumFull&mask4);
bytes memory addressBytecode = abi.encodePacked(network, publicKeyHash, checksumSliced);
bytes memory bitcoinAddressBase58 = bytesToBase58(addressBytecode);
bitcoinAddress = string(abi.encodePacked(networkPrefix, bitcoinAddressBase58));
}
function bytesToBase58(bytes memory source)
public pure returns (bytes memory)
{
if (source.length == 0) return new bytes(0);
uint8[] memory digits = new uint8[](40);
digits[0] = 0;
uint8 digitlength = 1;
for (uint8 i = 0; i < source.length; ++i) {
uint carry = uint8(source[i]);
for (uint8 j = 0; j < digitlength; ++j) {
carry += uint(digits[j]) * 256;
digits[j] = uint8(carry % 58);
carry = carry / 58;
}
while (carry > 0) {
digits[digitlength] = uint8(carry % 58);
digitlength++;
carry = carry / 58;
}
}
return toAlphabet(reverse(truncate(digits, digitlength)));
}
function truncate(uint8[] memory array, uint8 length)
internal pure returns (uint8[] memory)
{
uint8[] memory output = new uint8[](length);
for (uint8 i = 0; i<length; i++) {
output[i] = array[i];
}
return output;
}
function reverse(uint8[] memory input)
internal pure returns (uint8[] memory)
{
uint8[] memory output = new uint8[](input.length);
for (uint8 i = 0; i<input.length; i++) {
output[i] = input[input.length-1-i];
}
return output;
}
function toAlphabet(uint8[] memory indices)
internal pure returns (bytes memory)
{
bytes memory output = new bytes(indices.length);
for (uint8 i = 0; i<indices.length; i++) {
output[i] = ALPHABET[indices[i]];
}
return output;
}
}