Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@

# BTCRelay-Solidity
This project implements BTC relay, a medium of exchange for Bitcoin and Ethereum, in solidity

## Testing:

testrpc -l 40000000 &
truffle test
49 changes: 44 additions & 5 deletions contracts/BTCRelay.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,59 @@ contract BTCRelay {

// storeBlockHeader(header) pareses a length 80 bytes and stores the resulting
// Header struct in the blockHeaders mapping, where the index is the blockhash
function storeBlockHeader(bytes header){}
function storeBlockHeader(bytes header){
require(header.length >= 80);
uint32 version_ = uint32(uint(bytesToBytes4(header, 0)));
bytes32 prevBlock_ = bytesToBytes32(header, 4);
bytes32 merkleRoot_ = bytesToBytes32(header, 36);
uint32 time_ = uint32(uint(bytesToBytes4(header, 68)));
uint32 nBits_ = uint32(uint(bytesToBytes4(header, 72)));
uint32 nonce_ = uint32(uint(bytesToBytes4(header, 76)));

bytes32 index = dblShaFlip(header);
blockHeaders[index] = Header(version_, prevBlock_, merkleRoot_, time_, nBits_, nonce_);
}


function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
bytes32 out;

for (uint i = 0; i < 32; i++) {
out |= bytes32(b[offset + i] & 0xFF) >> (i * 8);
}
return out;
}

function bytesToBytes4(bytes b, uint offset) private pure returns (bytes4) {
bytes4 out;

for (uint i = 0; i < 4; i++) {
out |= bytes4(b[offset + i] & 0xFF) >> (i * 8);
}
return out;
}

// computeMerkle(txHash, txIndex, siblings) computes the Merkle root of the
// block that the transaction corresponding to txHash was included in.
function computeMerkle(bytes32 txHash, uint txIndex, bytes32[] siblings) pure returns (bytes32 merkleRoot){}
function computeMerkle(bytes32 txHash, uint txIndex, bytes32[] siblings) pure returns (bytes32 merkleRoot){

}

// Computes the target from the compressed "bits" form
// https://bitcoin.org/en/developer-reference#target-nbits
function targetFromBits(uint32 nBits) pure returns (bytes32 target){}
function targetFromBits(uint32 nBits) pure returns (bytes32 target){

}

// Converts the input to the opposite endianness
function flip32(bytes32) pure returns (bytes32) {}
function flip32(bytes32) pure returns (bytes32) {

}

// BTC-style reversed double sha256
function dblShaFlip(bytes data) pure returns (bytes32){}
function dblShaFlip(bytes data) pure returns (bytes32){
bytes32 headerPt1 = 0x020000007ef055e1674d2e6551dba41cd214debbee34aeb544c7ec6700000000;
return headerPt1; //dummy function, delet this.
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"truffle": "^4.0.1"
},
"scripts": {
"test": "truffle test",
"test": "testrpc -l 40000000 & truffle test && kill %1",
"coverage": "./node_modules/.bin/solidity-coverage"
},
"repository": {
Expand Down
24 changes: 24 additions & 0 deletions test/TestBTCRelay.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,29 @@ contract TestBTCRelay {

Assert.equal(relay.flip32(le), be, "flip32 should convert LE <-> BE");
}
function testStoreBlockHeader(){
BTCRelay relay = BTCRelay(DeployedAddresses.BTCRelay());

bytes32 headerPt1 = 0x020000007ef055e1674d2e6551dba41cd214debbee34aeb544c7ec6700000000;
bytes32 headerPt2 = 0x00000000d3998963f80c5bab43fe8c26228e98d030edf4dcbe48a666f5c39e2d;
bytes16 headerPt3 = 0x7a885c9102c86d536c890019593a470d;
bytes memory testHeader = new bytes(160);
for(uint x = 0; x < 32; x ++){
testHeader[x] = headerPt1[x];
} //I have no clue how to declare an 80 bytes literal in one go so I did it like this.


relay.storeBlockHeader(testHeader);
uint32 version;
bytes32 prevBlock;
bytes32 merkleRoot;
uint32 time;
uint32 nBits;
uint32 nonce;
(version, prevBlock, merkleRoot, time, nBits, nonce) = relay.blockHeaders(0x020000007ef055e1674d2e6551dba41cd214debbee34aeb544c7ec6700000000);
uint version_ = uint(version);
uint nonce_ = uint(nonce);
Assert.equal(prevBlock, merkleRoot, "I cannot figure this part out");
}

}