FCKit nothing works

pull/1/head
pubkey 7 years ago
parent f6caf86955
commit d8bf98bb44
  1. 36
      contracts/TestContract.sol
  2. 67
      test/integration.test.js
  3. 2
      test/unit.test.js

@ -30,20 +30,50 @@ contract TestContract {
);
}
/**
* see https://ethereum.stackexchange.com/a/21037/1375
*/
function signHashLikeWeb3(
bytes32 _message
) public constant returns (bytes32) {
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(prefix, _message);
return prefixedHash;
}
/**
* checks if the signature is valid
* should be valid for signature created from the sign()-function in js
*/
function recoverSignature(
bytes32 msg,
bytes32 messageHash,
uint8 v,
bytes32 r,
bytes32 s
) public constant returns (address) {
address signer = ecrecover(
messageHash,
v, r, s
);
return signer;
}
/**
* recovers the signer from the message instead of the messageHash
*/
function recoverSignatureFromMessage(
bytes32 _message,
uint8 v,
bytes32 r,
bytes32 s
) public constant returns (address) {
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(prefix, _message);
address signer = ecrecover(
msg,
prefixedHash,
v, r, s
);
);
return signer;
}
}

@ -130,6 +130,18 @@ describe('integration.test.js', () => {
);
assert.equal(jsHash, sig.messageHash);
});
it('should be possible to create the same prefixed hash in solidity', async () => {
const ident = EthCrypto.createIdentity();
const str = EthCrypto.hash.solidityHash('foobar');
console.dir(str);
const jsHash = EthCrypto.hash.signHash(str);
console.log('jsHash: ' + jsHash);
const solHash = await state.contract
.methods
.signHashLikeWeb3(str)
.call();
assert.equal(jsHash, solHash);
});
});
describe('sign', () => {
it('should validate the signature on solidity', async () => {
@ -153,5 +165,60 @@ describe('integration.test.js', () => {
.call();
assert.equal(solSigner, ident.address);
});
it('should validate with the message instead of the hash', async () => {
const ident = EthCrypto.createIdentity();
// const message = EthCrypto.hash.solidityHash(AsyncTestUtil.randomString(12));
const message = 'foobar';
const messageHex = web3.utils.utf8ToHex(message);
const messageBytes = web3.utils.hexToBytes(messageHex);
console.log(111);
/*
console.log('message:');
console.dir(message);
console.dir(messageHex);
// pretest: hash should be equal to the web3-sign-hash
const signHash1 = web3.eth.accounts.sign(messageHex, ident.privateKey).messageHash;
const fullMessage = '\x19Ethereum Signed Message:\n' + messageHex.length + messageHex;
const ownHash = EthCrypto.hash.solidityHash(fullMessage);
const signHash2 = EthCrypto.hash.signHash(message);
console.log('signHash2:' + signHash2);
console.log('signHash1:' + signHash1);
console.log('ownHash:' + ownHash);
// assert.equal(signHash1, ownHash);
const solHash = await state.contract
.methods.signHashLikeWeb3(
message
)
.call();
console.log('solHash: ' + solHash);
console.log('--------------------------------------');
process.exit();
//"\x19Ethereum Signed Message:\n" + message.length + message
*/
const signature = await EthCrypto.sign(
ident.privateKey,
message
);
console.log(222);
const jsSigner = EthCrypto.recover(signature, message);
console.log(333);
assert.equal(jsSigner, ident.address);
const solSigner = await state.contract
.methods.recoverSignatureFromMessage(
messageHex,
signature.v,
signature.r,
signature.s
)
.call();
assert.equal(solSigner, ident.address);
});
});
});

@ -65,7 +65,7 @@ describe('unit.test.js', () => {
it('should not sign with wrong key', () => {
assert.throws(
() => EthCrypto.sign(
AsyncTestUtil.randomString(222),
'XXX' + AsyncTestUtil.randomString(222),
AsyncTestUtil.randomString(12)
)
);

Loading…
Cancel
Save