当前位置: 首页 > 软件库 > Web3 > 区块链 >

eth-crypto

授权协议 MIT License
开发语言
所属分类 Web3、 区块链
软件类型 开源软件
地区 不详
投 递 者 谷梁晟
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

eth-crypto

Cryptographic javascript-functions for ethereum and tutorials on how to use them together with web3js and solidity.

Tutorials

Functions

Install

npm install eth-crypto --save
// es6
import EthCrypto from 'eth-crypto';

// node
const EthCrypto = require('eth-crypto');

API

createIdentity()

Creates a new ethereum-identity with privateKey, publicKey and address as hex-string.

const identity = 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.

const entropy = Buffer.from('f2dacf...', 'utf-8'); // must contain at least 128 chars
  const identity = EthCrypto.createIdentity(entropy);
  /* > {
      address: '0x59c8d4d645B0a3b230DE368d815ebDE372d37Ea8',
      privateKey: '0x18cea40e44624867ddfd775b2898cdb2da29b4be92ee072b9eb02d43b6f2473a',
      publicKey: '991ce4643653ef452327ee3d1a56af19c84599d340ffd427e784...'
  } */

publicKeyByPrivateKey()

Derives the publicKey from a privateKey and returns it as hex-string.

const publicKey = EthCrypto.publicKeyByPrivateKey(
      '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07'
  );
  // > 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...'

publicKey.toAddress()

Derives the ethereum-address from the publicKey.

const address = EthCrypto.publicKey.toAddress(
      'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece...'
  );
  // > '0x3f243FdacE01Cfd9719f7359c94BA11361f32471'

publicKey.compress()

Compresses an uncompressed publicKey.

const address = EthCrypto.publicKey.compress(
      '04a34d6aef3eb42335fb3cacb59...'
  );
  // > '03a34d6aef3eb42335fb3cacb59478c0b44c0bbeb8bb4ca427dbc7044157a5d24b' // compressed keys start with '02' or '03'

publicKey.decompress()

Decompresses a compressed publicKey.

const address = EthCrypto.publicKey.decompress(
      '03a34d6aef3eb42335fb3c...'
  );
  // > 'a34d6aef3eb42335fb3cacb5947' // non-compressed keys start with '04' or no prefix

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', // privateKey
      messageHash // hash of message
  );
  // > '0xc04b809d8f33c46ff80c44ba58e866ff0d5..'

recover()

Recovers the signers address from the signature.

const signer = EthCrypto.recover(
      '0xc04b809d8f33c46ff80c44ba58e866ff0d5..',
      EthCrypto.hash.keccak256('foobar') // signed message hash
  );
  // > '0x3f243FdacE01Cfd9719f7359c94BA11361f32471'

recoverPublicKey()

Recovers the signers publicKey from the signature.

const signer = EthCrypto.recoverPublicKey(
      '0xc04b809d8f33c46ff80c44ba58e866ff0d5..', // signature
      EthCrypto.hash.keccak256('foobar') // message hash
  );
  // > 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06ece..'

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...', // publicKey
        'foobar' // message
    );
    /* >  {
            iv: '02aeac54cb45283b427bd1a5028552c1',
            ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
            ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
            mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
        } */

decryptWithPrivateKey()

Decrypts the encrypted data with the privateKey. Returns (async) the message as string.

const message = await EthCrypto.decryptWithPrivateKey(
        '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07', // privateKey
        {
            iv: '02aeac54cb45283b427bd1a5028552c1',
            ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
            ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
            mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
        } // encrypted-data
    );
    // 'foobar'

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'
});
// > '59ab06532fc965b0107977f43e69e5a4038db32099dab281c8f5aece2852...'

cipher.parse()

Parses the string-representation back into the encrypted object.

const str = EthCrypto.cipher.parse('59ab06532fc965b0107977f43e69e5a4038db32099dab281c8f5aece2852...');
/* >  {
        iv: '02aeac54cb45283b427bd1a5028552c1',
        ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
        ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
        mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
    } */

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);
// > '071d3a2040a2d2cb...'

// you can now send the tx to the node
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 SolidityCli = require('solidity-cli');

// create compiled solidity-code
const compiled = await SolidityCli.compileCode(
    'contract ExampleContract {...'
)[':ExampleContract'];

const createCode = EthCrypto.txDataByCompiled(
    compiled.interface, // abi
    compiled.bytecode, // bytecode
    [identity.address] // constructor-arguments
);

// now you can submit this to the blockchain
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.

// pre-calculate address
const calculatedAddress = EthCrypto.calculateContractAddress(
    account.address, // address of the sender
    3 // nonce with which the contract will be deployed
);

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);
// > true

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'; // 66 chars

const utf16 = EthCrypto.hex.compress(hexString); // compress to utf16
// > 'ၻ炞䆷襞ⶬ輦ꂩቊ쮷蛰ﴚ艡Řᨇ' // 16 chars

const base64 = EthCrypto.hex.compress(hexString, true); // compress to base64
// > 'EHvpRnCeQbeJXuqfLaz5mKCpEkrLt4bw/RqCYQFYGgc=' // 44 chars

EthCrypto.hex.decompress(utf16); // decompress from utf16
// > '0x107be946709e41b7895eea9f2d...'

EthCrypto.hex.decompress(base64, true); // decompress from base64
// > '0x107be946709e41b7895eea9f2d...'
  • 本文环境:       操作系统:windows 64;       node版本:v10.14.0; 参考文档:https://github.com/pubkey/eth-crypto; 1. 安装 npm install g -eth-crypto // es6 import EthCrypto from 'eth-crypto'; // node const EthCrypto = req

  • DPDK二层转发和加密结合在一起,因为最近需要引用到DPDK的加解密函数,所以研究了这个例子 配置运行: 因为DPDK默认只提供了NULL CRYPTO POLL MODE DRIVER,所以我们需要在编译DPDK的时候打开其他 CRYPTO POLL MODE DRIVER的开关,这里以OPENSSL CRYPTO POLL MODE DRIVER为例: 在$RTE_SDK/config/com

  • · A girl tries to live in China for 21 days with 0.21 bitcoin According to the bitcoinexchangeguide news, a series of documentary “21 Days Digital Survival Challenge” has been broadcast, telling a Chi

  • 原文地址:The JavaScript Developer’s Intro to Crypto 原文作者:Eric Elliott 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m… 译者:Xuyuey 校对者:portandbridge, Fengziyin 如何在价值互联网(the Internet of Value)上构建 App 在接下来的 2-4 年中,

  • wallet-eth-android wallet-eth 以太坊代币钱包 助记词 私钥 keystore 转账(bip39、bip32、bip44、web3j) 生成钱包地址 // 生成钱包地址 Wallet wallet = WalletManager.generateWalletAddress(); // 根据助记词获取地址 WalletManager.generateAddress(wal

  • 本系列教程是根据实际项目开发中总结的经验所得,如发现有不对的地方,还请指正。 目录 Autosar Configuration(一)Davinci Developer-工具介绍 Autosar Configuration(二)Davinci Developer-SWC配置 Autosar Configuration(三) Security之Crypto配置 Autosar Configuratio

  • 本系列教程是根据实际项目开发中总结的经验所得,如发现有不对的地方,还请指正。 目录 Autosar Configuration(一)Davinci Developer-工具介绍 Autosar Configuration(二)Davinci Developer-SWC配置 Autosar Configuration(三) Security之Crypto配置 Autosar Configuratio

 相关资料
  • 包含以太坊区块链相关的方法 示例: var eth = web3.eth;

  • 简介 RT-Thread 为了方便用户开发网络应用,引入了网络设备框架,同时 RT-Thread 还提供了数量丰富的网络组件包,方便用户快速开发自己的网络应用。 本文将基于正点原子 stm32f407-atk-explorer 开发板主要介绍如何使用 RT-Thread Studio 来添加以太网驱动和 lwIP 协议栈。 ETH 设备驱动的开发可总结为如下: 新建 RT-Thread 完整版项目

  • Create Eth App Create Ethereum-powered apps with one command. Create Eth App works on macOS, Windows, and Linux. If something doesn’t work, please file an issue. If you have questions or need help, pl

  • Deprecated Note that this repo is archived and been eclipsed by https://github.com/vulcanize/go-codec-dageth go-ipld-eth go-ipld-eth is the set of Ethereum elements as a part of the IPLD merkle-forest

  • IPFS in Ethereum Smart Contracts This is a simple blog application that is a proof of concept of connecting Ethereum's smart contracts to JSON database stored on IPFS. The communication is based on Or

  • 永久注册中心是负责顶级域名.eth的分配和更新的智能合约。目前使用旧版的维克里拍卖的方式进行域名分配和注册。新版注册中心旨在简化这一过程,同时为未来的改进提供一个稳定的平台,这里的稳定指的是使API的变化最小化。(LBB译注:ENS团队已经在2019年5月4日部署了新版永久注册中心) 永久注册中心的计划部署日期是2019年5月4日。这里只是提供初步的文档,目的是为一些开发者提供一个起点,这些开发者