Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const EthCrypto = require('eth-crypto');
const Client = require('./client.js');
const Paypal = require('./paypal.js');
console.log('/////////////////////////////////////');
console.log('// Hashing and Public/Private Keys //');
console.log('/////////////////////////////////////');
// Hashing A Message
console.log("\nLet's hash a message!");
const message = 'Hello World';
console.log('The message is: ', message);
const messageHash = EthCrypto.hash.keccak256(message);
console.log('The hash of that message is: ', messageHash);
// Creating Public/Private Keys
console.log('\nCreating public/private key pairs to sign and verify messages.');
// Init Alice
const alice = new Client();
console.log("Init Alice's Client\n", alice);
// Init Bob
const bob = new Client();
console.log("Init Bob's Client\n", bob);
// Init Carol
const carol = new Client();
console.log("Init Carol's Client\n", carol);
const EthCrypto = require('eth-crypto');
const Client = require('./Client.js');
const Paypal = require('./Paypal.js');
console.log('/////////////////////////////////////');
console.log('// Hashing and Public/Private Keys //');
console.log('/////////////////////////////////////');
// Hashing A Message
console.log("\nLet's hash a message!");
const message = 'Hello World';
console.log('The message is: ', message);
const messageHash = EthCrypto.hash.keccak256(message);
console.log('The hash of that message is: ', messageHash);
// Creating Public/Private Keys
console.log('\nCreating public/private key pairs to sign and verify messages.');
// Init Alice
const alice = new Client();
console.log("Init Alice's Client\n", alice);
// Init Bob
const bob = new Client();
console.log("Init Bob's Client\n", bob);
// Init Carol
const carol = new Client();
console.log("Init Carol's Client\n", carol);
async signMessage (message) {
const hex = Buffer.from(message).toString('hex')
const messageHash = EthCrypto.hash.keccak256(message);
const signature = EthCrypto.sign(
ensure0x(this.wallet.getPrivateKey().toString('hex')), // private key
messageHash // hash of message
);
return remove0x(signature)
}
toHash(data) {
// ?! why is this JSON.stringify thing here, but not in 1.1 ???
const dataStr = JSON.stringify(data)
return EthCrypto.hash.keccak256(dataStr)
}
it(`signs messages and recovers keys`, async () => {
const wallet = await Wallet.generate_entity();
const message = 'SHIPTest';
const hash = EthCrypto.hash.keccak256([{value: message, type: 'string'}]);
expect(hash).toEqual("0x6eba83aa272e398b5cddf2055bb404f84c3fd8e42dd96e80edb204f64bff73ab");
const signed = wallet.sign_hash(hash);
expect(Wallet.recover_signer_address(signed, hash)).toEqual(wallet.address);
expect(Wallet.recover_signer_public_key(signed, hash)).toEqual(wallet.public_key);
});
};
function getTxHash (tx) {
return EthCrypto.hash.keccak256(JSON.stringify(tx))
}
function getTxHash(tx) {
return EthCrypto.hash.keccak256(JSON.stringify(tx));
}
function keccak256Hash(data: string): string {
return EthCrypto.hash.keccak256(data);
}
export function stringHash(value: string, alg?: string) {
if (!alg) {
alg = DEFAULT_HASH_ALG;
}
switch (alg) {
case 'sha256':
const hash = crypto.createHash('sha256');
hash.update(value);
return '0x' + hash.digest('hex');
case 'keccak256':
return EthCrypto.hash.keccak256([{ value: value, type: 'string' }]);
default:
throw new Error(`Invalid hashing algorithm ${alg}`);
}
}
function getTxHash (tx) {
return EthCrypto.hash.keccak256(JSON.stringify(tx))
}