eth-crypto
Cryptographic javascript-functions for ethereum and how to use them together with web3 and solidity.
Tutorials
Functions
Install
npm install eth-crypto --save
import EthCrypto from 'eth-crypto';
const EthCrypto = require('eth-crypto');
API
createIdentity()
Creates a new ethereum-identity with privateKey, publicKey and address as hex-string.
const identity = EthCrypto.createIdentity();
publicKeyByPrivateKey()
Derives the publicKey from a privateKey and returns it as hex-string.
const publicKey = EthCrypto.publicKeyByPrivateKey(
'0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07'
);
publicKey.toAddress()
Derives the ethereum-address from the publicKey.
const address = EthCrypto.publicKey.toAddress(
'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...'
);
publicKey.compress()
Compresses an uncompressed publicKey.
const address = EthCrypto.publicKey.compress(
'04a34d6aef3eb42335fb3cacb59...'
);
publicKey.decompress()
Decompresses a compressed publicKey.
const address = EthCrypto.publicKey.decompress(
'03a34d6aef3eb42335fb3c...'
);
sign()
Signs the hash with the privateKey. Returns the signature as hex-string.
const message = 'foobar';
const messageHash = EthCrypto.hash.keccak256(message);
const signature = EthCrypto.sign(
'0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07',
messageHash
);
recover()
Recovers the signers address from the signature.
const signer = EthCrypto.recover(
'0xc04b809d8f33c46ff80c44ba58e866ff0d5..',
EthCrypto.hash.keccak256('foobar')
);
recoverPublicKey()
Recovers the signers publicKey
from the signature.
const signer = EthCrypto.recoverPublicKey(
'0xc04b809d8f33c46ff80c44ba58e866ff0d5..',
EthCrypto.hash.keccak256('foobar')
);
encryptWithPublicKey()
Encrypts the message with the publicKey so that only the corresponding privateKey can decrypt it. Returns (async) the encrypted data as object with hex-strings.
const encrypted = await EthCrypto.encryptWithPublicKey(
'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...',
'foobar'
);
decryptWithPrivateKey()
Decrypts the encrypted data with the privateKey. Returns (async) the message as string.
const message = await EthCrypto.decryptWithPrivateKey(
'0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07',
{
iv: '02aeac54cb45283b427bd1a5028552c1',
ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
}
);
cipher.stringify()
Transforms the object with the encrypted data into a smaller string-representation.
const str = EthCrypto.cipher.stringify({
iv: '02aeac54cb45283b427bd1a5028552c1',
ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
});
cipher.parse()
Parses the string-representation back into the encrypted object.
const str = EthCrypto.cipher.parse('59ab06532fc965b0107977f43e69e5a4038db32099dab281c8f5aece2852...');
signTransaction()
Signs a raw transaction with the privateKey. Returns a serialized tx which can be submitted to the node.
const identity = EthCrypto.createIdentity();
const rawTx = {
from: identity.address,
to: '0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0',
value: 1000000000000000000,
gasPrice: 5000000000,
nonce: 0,
gasLimit: 21000
};
const signedTx = EthCrypto.signTransaction(
rawTx,
identity.privateKey
);
console.log(signedTx);
const receipt = await web3.eth.sendSignedTransaction(signedTx);
txDataByCompiled()
Creates the data-string which must be submitted with an transaction to create a contract-instance.
const solc = require('solc');
const compiled = solc.compile(
'contract ExampleContract {...',
1
).contracts[':ExampleContract'];
const createCode = EthCrypto.txDataByCompiled(
compiled.interface,
compiled.bytecode,
[identity.address]
);
const serializedTx = EthCrypto.signTransaction(
{
from: identity.address,
nonce: 0,
gasLimit: 5000000,
gasPrice: 5000000000,
data: createCode
},
identity.privateKey
);
const receipt = await web3.eth.sendSignedTransaction(serializedTx);
calculateContractAddress()
Calculates the address for the contract from the senders address and the nonce, without deploying it to the blockchain.
const calculatedAddress = EthCrypto.calculateContractAddress(
account.address,
3
);
const rawTx = {
from: account.address,
gasPrice: parseInt(gasPrice),
nonce: 3,
data: compiled.code
};
const receipt = await state.web3.eth.sendTransaction(rawTx);
console.log(receipt.contractAddress === calculatedAddress);
hex compress/decompress
Compress or decompress a hex-string to make it smaller. You can either compress to utf16 which reduces the size to about 1/4, or to base64 which reduces the size to about 4/5.
const hexString = '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07';
const utf16 = EthCrypto.hex.compress(hexString);
const base64 = EthCrypto.hex.compress(hexString, true);
EthCrypto.hex.decompress(utf16);
EthCrypto.hex.decompress(base64, true);