ADD hash-test

pull/1/head
pubkey 7 years ago
parent c39e4e22a2
commit 81492d687b
  1. 2
      README.md
  2. 34
      contracts/DonationBag.sol
  3. 1
      src/hash.js
  4. 65
      test/tutorials.test.js
  5. 9
      tutorials/signed-data.md

@ -136,7 +136,7 @@ Decrypts the encrypted data with the privateKey. Returns (async) the message as
Signs a raw transaction with the privateKey. Returns a serialized tx which can be submitted to the node.
```javascript
const ident = EthCrypto.createIdentity();
const identity = EthCrypto.createIdentity();
const rawTx = {
from: identity.address,
to: '0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0',

@ -35,10 +35,27 @@ contract DonationBag {
*/
string public signPrefix = "Signed for DonationBag:";
/**
* generates a prefixed hash of the address
* We hash the following together:
* - signPrefix
* - address of this contract
* - the recievers-address
*/
function prefixedHash(
address receiver
) public constant returns(bytes32) {
bytes32 hash = keccak256(
signPrefix,
address(this),
receiver
);
return hash;
}
/**
* validates if the signature is valid
* by checking if the correct message was signed
* which consists of <signPrefix><contractAddress><receiverAddress>
*/
function isSignatureValid(
address receiver,
@ -46,17 +63,13 @@ contract DonationBag {
bytes32 r,
bytes32 s
) public constant returns (bool correct) {
bytes32 mustBeSigned = keccak256(
signPrefix,
this,
receiver
);
bytes32 mustBeSigned = prefixedHash(receiver);
address signer = ecrecover(
mustBeSigned,
v, r, s
);
return (signer == owner && receiver == msg.sender);
return (signer == owner);
}
/**
@ -64,7 +77,6 @@ contract DonationBag {
* if yes we send some wei to the submitter
*/
function recieveDonation(
address receiver,
uint8 v,
bytes32 r,
bytes32 s
@ -75,7 +87,7 @@ contract DonationBag {
// signature not valid -> revert
if (isSignatureValid(
receiver,
msg.sender,
v, r, s
) == false) {
revert();
@ -91,8 +103,4 @@ contract DonationBag {
function getBalance() public constant returns (uint256 balance) {
return this.balance;
}
function recieve() public returns (string) {
return "thx!";
}
}

@ -19,7 +19,6 @@ export const SIGN_PREFIX = '\x19Ethereum Signed Message:\n32';
* '\x19Ethereum Signed Message:\n32'
*/
export function prefixedHash(hash) {
if (!web3.utils.isHexStrict(hash))
throw new Error('EthCrypto.hash.prefixedHash(): please insert an hash');
return web3.eth.accounts.hashMessage({

@ -27,16 +27,25 @@ describe('tutorials.test.js', () => {
web3.setProvider(ganacheProvider);
const solc = require('solc');
const fs = require('fs');
const path = require('path');
const contractPath = path.join(__dirname, '../contracts/DonationBag.sol');
let compiled;
const fastMode = true; // TODO check in config if really fast-mode
if (!fastMode) {
const solc = require('solc');
const fs = require('fs');
const path = require('path');
const contractPath = path.join(__dirname, '../contracts/DonationBag.sol');
// read solidity-code from file
const contractCode = fs.readFileSync(contractPath, 'utf8');
// read solidity-code from file
const contractCode = fs.readFileSync(contractPath, 'utf8');
// compile the code into an object
const compiled = solc.compile(contractCode, 1).contracts[':DonationBag'];
// compile the code into an object
compiled = solc.compile(contractCode, 1).contracts[':DonationBag'];
} else {
compiled = require('../gen/DonationBag.json');
compiled.bytecode = compiled.code;
compiled.interface = JSON.stringify(compiled.interface);
}
const createCode = EthCrypto.txDataByCompiled(
compiled.interface, // abi
@ -44,6 +53,7 @@ describe('tutorials.test.js', () => {
[creatorIdentity.address] // constructor-arguments
);
// create create-tx
const rawTx = {
from: creatorIdentity.address,
@ -98,5 +108,44 @@ describe('tutorials.test.js', () => {
const balance = await contractInstance.methods.getBalance().call();
assert.equal(balance, web3.utils.toWei('1', 'ether'));
// check prefixedHash
const solHash = await contractInstance
.methods
.prefixedHash(recieverIdentity.address)
.call();
console.log('solHash: ' + solHash);
// sign message
const hashToSign = EthCrypto.hash.keccak256([{
type: 'string',
value: 'Signed for DonationBag:'
}, {
type: 'address',
value: contractAddress
}, {
type: 'address',
value: recieverIdentity.address
}]);
console.log('signHash: ' + hashToSign);
assert.equal(hashToSign, solHash);
const signature = EthCrypto.sign(
creatorIdentity.privateKey,
hashToSign
);
console.dir(signature);
const isValid = await contractInstance
.methods.isSignatureValid(
recieverIdentity.address,
signature.v,
signature.r,
signature.s
).call();
assert.ok(isValid);
// TODO receiveDonation
});
});

@ -140,3 +140,12 @@ const receipt2 = await web3.eth.sendSignedTransaction(serializedTx2);
const balance = await contractInstance.methods.getBalance().call();
console.log(balance); // > '1000000000000000000'
```
## Sign the message
Lets sign a message with the `creatorIdentity` where the donator validates a donation to the `recieverIdentity`.
```javascript
```

Loading…
Cancel
Save