Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork163
Cryptographic javascript-functions for ethereum and tutorials to use them with web3js and solidity
License
pubkey/eth-crypto
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Cryptographic javascript-functions for ethereum and tutorials on how to use them together with web3js and solidity.
Creating keys and use them for ethereum transactions
In this tutorial we will create an ethereum-identity and use it to send transactions to the blockchain.
Sign and validate data with solidity
In this tutorial we will sign data in javascript and validate the signature inside of a smart-contract.
Sending encrypted and signed data to other identities
In this tutorial we will use the ethereum-identities and asymmetric cryptography to send an encrypted and signed message from Alice to Bob.
npm install eth-crypto --save
// es6importEthCryptofrom'eth-crypto';// nodeconstEthCrypto=require('eth-crypto');
- createIdentity()
- publicKeyByPrivateKey()
- publicKey.toAddress()
- publicKey.compress()
- publicKey.decompress()
- sign()
- recover()
- recoverPublicKey()
- encryptWithPublicKey()
- decryptWithPrivateKey()
- cipher.stringify()
- cipher.parse()
- signTransaction()
- txDataByCompiled()
- calculateContractAddress()
- hex compress/decompress
Creates a new ethereum-identity with privateKey, publicKey and address as hex-string.
constidentity=EthCrypto.createIdentity();/* > { address: '0x3f243FdacE01Cfd9719f7359c94BA11361f32471', privateKey: '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07', publicKey: 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...' } */
You can also create an identity by providing your own entropy-buffer. Use this with caution, a bad entropy can result in an unsecure private key.
constentropy=Buffer.from('f2dacf...','utf-8');// must contain at least 128 charsconstidentity=EthCrypto.createIdentity(entropy);/* > { address: '0x59c8d4d645B0a3b230DE368d815ebDE372d37Ea8', privateKey: '0x18cea40e44624867ddfd775b2898cdb2da29b4be92ee072b9eb02d43b6f2473a', publicKey: '991ce4643653ef452327ee3d1a56af19c84599d340ffd427e784...' } */
Derives the publicKey from a privateKey and returns it as hex-string.
constpublicKey=EthCrypto.publicKeyByPrivateKey('0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07');// > 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...'
Derives the ethereum-address from the publicKey.
constaddress=EthCrypto.publicKey.toAddress('bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...');// > '0x3f243FdacE01Cfd9719f7359c94BA11361f32471'
Compresses an uncompressed publicKey.
constaddress=EthCrypto.publicKey.compress('04a34d6aef3eb42335fb3cacb59...');// > '03a34d6aef3eb42335fb3cacb59478c0b44c0bbeb8bb4ca427dbc7044157a5d24b' // compressed keys start with '02' or '03'
Decompresses a compressed publicKey.
constaddress=EthCrypto.publicKey.decompress('03a34d6aef3eb42335fb3c...');// > 'a34d6aef3eb42335fb3cacb5947' // non-compressed keys start with '04' or no prefix
Signs the hash with the privateKey. Returns the signature as hex-string.
constmessage='foobar';constmessageHash=EthCrypto.hash.keccak256(message);constsignature=EthCrypto.sign('0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07',// privateKeymessageHash// hash of message);// > '0xc04b809d8f33c46ff80c44ba58e866ff0d5..'
Recovers the signers address from the signature.
constsigner=EthCrypto.recover('0xc04b809d8f33c46ff80c44ba58e866ff0d5..',EthCrypto.hash.keccak256('foobar')// signed message hash);// > '0x3f243FdacE01Cfd9719f7359c94BA11361f32471'
Recovers the signerspublicKey from the signature.
constsigner=EthCrypto.recoverPublicKey('0xc04b809d8f33c46ff80c44ba58e866ff0d5..',// signatureEthCrypto.hash.keccak256('foobar')// message hash);// > 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece..'
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.
constencrypted=awaitEthCrypto.encryptWithPublicKey('bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...',// publicKey'foobar'// message);/* > { iv: '02aeac54cb45283b427bd1a5028552c1', ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...', ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96', mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1' } */
Decrypts the encrypted data with the privateKey. Returns (async) the message as string.
constmessage=awaitEthCrypto.decryptWithPrivateKey('0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07',// privateKey{iv:'02aeac54cb45283b427bd1a5028552c1',ephemPublicKey:'044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',ciphertext:'5fbbcc1a44ee19f7499dbc39cfc4ce96',mac:'96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'}// encrypted-data);// 'foobar'
Transforms the object with the encrypted data into a smaller string-representation.
conststr=EthCrypto.cipher.stringify({iv:'02aeac54cb45283b427bd1a5028552c1',ephemPublicKey:'044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',ciphertext:'5fbbcc1a44ee19f7499dbc39cfc4ce96',mac:'96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'});// > '59ab06532fc965b0107977f43e69e5a4038db32099dab281c8f5aece2852...'
Parses the string-representation back into the encrypted object.
conststr=EthCrypto.cipher.parse('59ab06532fc965b0107977f43e69e5a4038db32099dab281c8f5aece2852...');/* > { iv: '02aeac54cb45283b427bd1a5028552c1', ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...', ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96', mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1' } */
Signs a raw transaction with the privateKey. Returns aPromise that resolves a serialized tx-string which can be submitted to the node.
constidentity=EthCrypto.createIdentity();constrawTx={from:identity.address,to:'0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0',value:newBN('1000000000000000000'),gasPrice:5000000000,nonce:0,gasLimit:21000};constsignedTx=awaitEthCrypto.signTransaction(rawTx,identity.privateKey);console.log(signedTx);// > '071d3a2040a2d2cb...'// you can now send the tx to the nodeconstreceipt=awaitweb3.eth.sendSignedTransaction(signedTx);
Creates the data-string which must be submitted with an transaction to create a contract-instance.
constSolidityCli=require('solidity-cli');// create compiled solidity-codeconstcompiled=awaitSolidityCli.compileCode('contract ExampleContract {...')[':ExampleContract'];constcreateCode=EthCrypto.txDataByCompiled(compiled.interface,// abicompiled.bytecode,// bytecode[identity.address]// constructor-arguments);// now you can submit this to the blockchainconstserializedTx=awaitEthCrypto.signTransaction({from:identity.address,nonce:0,gasLimit:5000000,gasPrice:5000000000,data:createCode},identity.privateKey);constreceipt=awaitweb3.eth.sendSignedTransaction(serializedTx);
Calculates the address for the contract from the senders address and the nonce, without deploying it to the blockchain.
// pre-calculate addressconstcalculatedAddress=EthCrypto.calculateContractAddress(account.address,// address of the sender3// nonce with which the contract will be deployed);constrawTx={from:account.address,gasPrice:parseInt(gasPrice),nonce:3,data:compiled.code};constreceipt=awaitstate.web3.eth.sendTransaction(rawTx);console.log(receipt.contractAddress===calculatedAddress);// > true
"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. This is not a real compression, it just make your string smaller when you have to store it in utf-16 anyways.
consthexString='0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07';// 66 charsconstutf16=EthCrypto.hex.compress(hexString);// compress to utf16// > 'ၻ炞䆷襞ⶬ輦ꂩቊ쮷蛰ﴚ艡Řᨇ' // 16 charsconstbase64=EthCrypto.hex.compress(hexString,true);// compress to base64// > 'EHvpRnCeQbeJXuqfLaz5mKCpEkrLt4bw/RqCYQFYGgc=' // 44 charsEthCrypto.hex.decompress(utf16);// decompress from utf16// > '0x107be946709e41b7895eea9f2d...'EthCrypto.hex.decompress(base64,true);// decompress from base64// > '0x107be946709e41b7895eea9f2d...'
About
Cryptographic javascript-functions for ethereum and tutorials to use them with web3js and solidity
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.