parent
f2ed39850f
commit
874c9cccaa
@ -1,46 +1,15 @@ |
||||
{ |
||||
"plugins": [ |
||||
["transform-es2015-template-literals", { |
||||
"loose": true |
||||
}], |
||||
"transform-es2015-literals", |
||||
"transform-es2015-function-name", |
||||
"transform-es2015-arrow-functions", |
||||
"transform-es2015-block-scoped-functions", ["transform-es2015-classes", { |
||||
"loose": true |
||||
}], |
||||
"transform-es2015-object-super", |
||||
"transform-es2015-shorthand-properties", ["transform-es2015-computed-properties", { |
||||
"loose": true |
||||
}], |
||||
["transform-es2015-for-of", { |
||||
"loose": true |
||||
}], |
||||
"transform-es2015-sticky-regex", |
||||
"transform-es2015-unicode-regex", |
||||
"check-es2015-constants", ["transform-es2015-spread", { |
||||
"loose": true |
||||
}], |
||||
"transform-es2015-parameters", ["transform-es2015-destructuring", { |
||||
"loose": true |
||||
}], |
||||
"transform-es2015-block-scoping", |
||||
"transform-object-rest-spread", |
||||
"transform-es3-member-expression-literals", |
||||
"transform-es3-property-literals", |
||||
"transform-async-to-generator", |
||||
"transform-regenerator", |
||||
["transform-runtime", { |
||||
"polyfill": true, |
||||
"regenerator": true |
||||
}] |
||||
], |
||||
"env": { |
||||
"es5": { |
||||
"presets": ["es2015"] |
||||
}, |
||||
"es6": { |
||||
"presets": ["es2017"] |
||||
} |
||||
"presets": ["@babel/preset-env"], |
||||
"env": { |
||||
"es6": { |
||||
"presets": [ |
||||
[ |
||||
"@babel/preset-env", |
||||
{ |
||||
"modules": false |
||||
} |
||||
] |
||||
] |
||||
} |
||||
} |
||||
} |
||||
|
@ -1,8 +1,7 @@ |
||||
import { generateAddress, toChecksumAddress, toBuffer } from 'ethereumjs-util'; |
||||
import { addLeading0x } from './util'; |
||||
|
||||
export default function calculateContractAddress(creatorAddress, nonce) { |
||||
var addressBuffer = generateAddress(toBuffer(addLeading0x(creatorAddress)), toBuffer(nonce)); |
||||
var address = addressBuffer.toString('hex'); |
||||
return toChecksumAddress(addLeading0x(address)); |
||||
var addressBuffer = generateAddress(toBuffer(addLeading0x(creatorAddress)), toBuffer(nonce)); |
||||
var address = addressBuffer.toString('hex'); |
||||
return toChecksumAddress(addLeading0x(address)); |
||||
} |
@ -1,34 +1,25 @@ |
||||
import { compress, decompress } from './public-key'; |
||||
|
||||
export function stringify(cipher) { |
||||
if (typeof cipher === 'string') return cipher; |
||||
|
||||
// use compressed key because it's smaller
|
||||
var compressedKey = compress(cipher.ephemPublicKey); |
||||
|
||||
var ret = Buffer.concat([Buffer.from(cipher.iv, 'hex'), // 16bit
|
||||
Buffer.from(compressedKey, 'hex'), // 33bit
|
||||
Buffer.from(cipher.mac, 'hex'), // 32bit
|
||||
Buffer.from(cipher.ciphertext, 'hex') // var bit
|
||||
]); |
||||
|
||||
return ret.toString('hex'); |
||||
if (typeof cipher === 'string') return cipher; // use compressed key because it's smaller
|
||||
|
||||
var compressedKey = compress(cipher.ephemPublicKey); |
||||
var ret = Buffer.concat([Buffer.from(cipher.iv, 'hex'), // 16bit
|
||||
Buffer.from(compressedKey, 'hex'), // 33bit
|
||||
Buffer.from(cipher.mac, 'hex'), // 32bit
|
||||
Buffer.from(cipher.ciphertext, 'hex') // var bit
|
||||
]); |
||||
return ret.toString('hex'); |
||||
} |
||||
|
||||
export function parse(str) { |
||||
if (typeof str !== 'string') return str; |
||||
|
||||
var buf = Buffer.from(str, 'hex'); |
||||
|
||||
var ret = { |
||||
iv: buf.toString('hex', 0, 16), |
||||
ephemPublicKey: buf.toString('hex', 16, 49), |
||||
mac: buf.toString('hex', 49, 81), |
||||
ciphertext: buf.toString('hex', 81, buf.length) |
||||
}; |
||||
|
||||
// decompress publicKey
|
||||
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey); |
||||
|
||||
return ret; |
||||
if (typeof str !== 'string') return str; |
||||
var buf = Buffer.from(str, 'hex'); |
||||
var ret = { |
||||
iv: buf.toString('hex', 0, 16), |
||||
ephemPublicKey: buf.toString('hex', 16, 49), |
||||
mac: buf.toString('hex', 49, 81), |
||||
ciphertext: buf.toString('hex', 81, buf.length) |
||||
}; // decompress publicKey
|
||||
|
||||
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey); |
||||
return ret; |
||||
} |
@ -1,40 +1,39 @@ |
||||
import publicKeyByPrivateKey from './public-key-by-private-key'; |
||||
|
||||
import { fromPrivate } from 'eth-lib/lib/account'; |
||||
import { keccak256 } from 'eth-lib/lib/hash'; |
||||
import Bytes from 'eth-lib/lib/bytes'; |
||||
|
||||
var MIN_ENTROPY_SIZE = 128; |
||||
|
||||
/** |
||||
* create a privateKey from the given entropy or a new one |
||||
* @param {Buffer} entropy |
||||
* @return {string} |
||||
*/ |
||||
|
||||
export function createPrivateKey(entropy) { |
||||
if (entropy) { |
||||
if (!Buffer.isBuffer(entropy)) throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer'); |
||||
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE) throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE); |
||||
if (entropy) { |
||||
if (!Buffer.isBuffer(entropy)) throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer'); |
||||
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE) throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE); |
||||
var outerHex = keccak256(entropy); |
||||
return outerHex; |
||||
} else { |
||||
// @link https://github.com/MaiaVictor/eth-lib/blob/master/lib/account.js#L8
|
||||
var innerHex = keccak256(Bytes.concat(Bytes.random(32), Bytes.random(32))); |
||||
var middleHex = Bytes.concat(Bytes.concat(Bytes.random(32), innerHex), Bytes.random(32)); |
||||
|
||||
var outerHex = keccak256(entropy); |
||||
return outerHex; |
||||
} else { |
||||
// @link https://github.com/MaiaVictor/eth-lib/blob/master/lib/account.js#L8
|
||||
var innerHex = keccak256(Bytes.concat(Bytes.random(32), Bytes.random(32))); |
||||
var middleHex = Bytes.concat(Bytes.concat(Bytes.random(32), innerHex), Bytes.random(32)); |
||||
var _outerHex = keccak256(middleHex); |
||||
return _outerHex; |
||||
} |
||||
} |
||||
var _outerHex = keccak256(middleHex); |
||||
|
||||
return _outerHex; |
||||
} |
||||
} |
||||
/** |
||||
* creates a new object with |
||||
* private-, public-Key and address |
||||
* @param {Buffer?} entropy if provided, will use that as single random-source |
||||
*/ |
||||
|
||||
export default function createIdentity(entropy) { |
||||
var privateKey = createPrivateKey(entropy); |
||||
var identity = fromPrivate(privateKey); |
||||
identity.publicKey = publicKeyByPrivateKey(identity.privateKey); |
||||
return identity; |
||||
var privateKey = createPrivateKey(entropy); |
||||
var identity = fromPrivate(privateKey); |
||||
identity.publicKey = publicKeyByPrivateKey(identity.privateKey); |
||||
return identity; |
||||
} |
@ -1,22 +1,17 @@ |
||||
import { decrypt } from 'eccrypto'; |
||||
import { parse } from './cipher'; |
||||
import { removeLeading0x } from './util'; |
||||
|
||||
export default function decryptWithPrivateKey(privateKey, encrypted) { |
||||
encrypted = parse(encrypted); // remove trailing '0x' from privateKey
|
||||
|
||||
encrypted = parse(encrypted); |
||||
|
||||
// remove trailing '0x' from privateKey
|
||||
var twoStripped = removeLeading0x(privateKey); |
||||
|
||||
var encryptedBuffer = { |
||||
iv: Buffer.from(encrypted.iv, 'hex'), |
||||
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'), |
||||
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'), |
||||
mac: Buffer.from(encrypted.mac, 'hex') |
||||
}; |
||||
|
||||
return decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { |
||||
return decryptedBuffer.toString(); |
||||
}); |
||||
var twoStripped = removeLeading0x(privateKey); |
||||
var encryptedBuffer = { |
||||
iv: Buffer.from(encrypted.iv, 'hex'), |
||||
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'), |
||||
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'), |
||||
mac: Buffer.from(encrypted.mac, 'hex') |
||||
}; |
||||
return decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { |
||||
return decryptedBuffer.toString(); |
||||
}); |
||||
} |
@ -1,21 +1,17 @@ |
||||
import { encrypt } from 'eccrypto'; |
||||
import { decompress } from './public-key'; |
||||
|
||||
export default function encryptWithPublicKey(publicKey, message) { |
||||
// ensure its an uncompressed publicKey
|
||||
publicKey = decompress(publicKey); // re-add the compression-flag
|
||||
|
||||
// ensure its an uncompressed publicKey
|
||||
publicKey = decompress(publicKey); |
||||
|
||||
// re-add the compression-flag
|
||||
var pubString = '04' + publicKey; |
||||
|
||||
return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message)).then(function (encryptedBuffers) { |
||||
var encrypted = { |
||||
iv: encryptedBuffers.iv.toString('hex'), |
||||
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'), |
||||
ciphertext: encryptedBuffers.ciphertext.toString('hex'), |
||||
mac: encryptedBuffers.mac.toString('hex') |
||||
}; |
||||
return encrypted; |
||||
}); |
||||
var pubString = '04' + publicKey; |
||||
return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message)).then(function (encryptedBuffers) { |
||||
var encrypted = { |
||||
iv: encryptedBuffers.iv.toString('hex'), |
||||
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'), |
||||
ciphertext: encryptedBuffers.ciphertext.toString('hex'), |
||||
mac: encryptedBuffers.mac.toString('hex') |
||||
}; |
||||
return encrypted; |
||||
}); |
||||
} |
@ -1,18 +1,18 @@ |
||||
import { utils as ethersUtils } from 'ethers'; |
||||
|
||||
export function keccak256(params) { |
||||
var types = []; |
||||
var values = []; |
||||
if (!Array.isArray(params)) { |
||||
types.push('string'); |
||||
values.push(params); |
||||
} else { |
||||
params.forEach(function (p) { |
||||
types.push(p.type); |
||||
values.push(p.value); |
||||
}); |
||||
} |
||||
return ethersUtils.solidityKeccak256(types, values); |
||||
} |
||||
var types = []; |
||||
var values = []; |
||||
|
||||
if (!Array.isArray(params)) { |
||||
types.push('string'); |
||||
values.push(params); |
||||
} else { |
||||
params.forEach(function (p) { |
||||
types.push(p.type); |
||||
values.push(p.value); |
||||
}); |
||||
} |
||||
|
||||
return ethersUtils.solidityKeccak256(types, values); |
||||
} |
||||
export var SIGN_PREFIX = '\x19Ethereum Signed Message:\n32'; |
@ -1,14 +1,14 @@ |
||||
import { privateToPublic, toBuffer } from 'ethereumjs-util'; |
||||
import { addLeading0x } from './util'; |
||||
|
||||
/** |
||||
* Generate publicKey from the privateKey. |
||||
* This creates the uncompressed publicKey, |
||||
* where 04 has stripped from left |
||||
* @returns {string} |
||||
*/ |
||||
|
||||
export default function publicKeyOfPrivateKey(privateKey) { |
||||
privateKey = addLeading0x(privateKey); |
||||
var publicKeyBuffer = privateToPublic(toBuffer(privateKey)); |
||||
return publicKeyBuffer.toString('hex'); |
||||
privateKey = addLeading0x(privateKey); |
||||
var publicKeyBuffer = privateToPublic(toBuffer(privateKey)); |
||||
return publicKeyBuffer.toString('hex'); |
||||
} |
@ -1,40 +1,31 @@ |
||||
import { publicKeyConvert } from 'secp256k1'; |
||||
import { pubToAddress, toChecksumAddress, toBuffer } from 'ethereumjs-util'; |
||||
import { hexToUnit8Array, uint8ArrayToHex, addLeading0x } from './util'; |
||||
|
||||
export function compress(startsWith04) { |
||||
|
||||
// add trailing 04 if not done before
|
||||
var testBuffer = Buffer.from(startsWith04, 'hex'); |
||||
if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; |
||||
|
||||
return uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith04), true)); |
||||
// add trailing 04 if not done before
|
||||
var testBuffer = Buffer.from(startsWith04, 'hex'); |
||||
if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; |
||||
return uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith04), true)); |
||||
} |
||||
|
||||
export function decompress(startsWith02Or03) { |
||||
// if already decompressed an not has trailing 04
|
||||
var testBuffer = Buffer.from(startsWith02Or03, 'hex'); |
||||
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; |
||||
var decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false)); // remove trailing 04
|
||||
|
||||
// if already decompressed an not has trailing 04
|
||||
var testBuffer = Buffer.from(startsWith02Or03, 'hex'); |
||||
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; |
||||
|
||||
var decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false)); |
||||
|
||||
// remove trailing 04
|
||||
decompressed = decompressed.substring(2); |
||||
return decompressed; |
||||
decompressed = decompressed.substring(2); |
||||
return decompressed; |
||||
} |
||||
|
||||
/** |
||||
* generates the ethereum-adress of the publicKey |
||||
* We create the checksum-adress which is case-sensitive |
||||
* @returns {string} address |
||||
*/ |
||||
export function toAddress(publicKey) { |
||||
|
||||
// normalize key
|
||||
publicKey = decompress(publicKey); |
||||
|
||||
var addressBuffer = pubToAddress(toBuffer(addLeading0x(publicKey))); |
||||
var checkSumAdress = toChecksumAddress(addLeading0x(addressBuffer.toString('hex'))); |
||||
return checkSumAdress; |
||||
export function toAddress(publicKey) { |
||||
// normalize key
|
||||
publicKey = decompress(publicKey); |
||||
var addressBuffer = pubToAddress(toBuffer(addLeading0x(publicKey))); |
||||
var checkSumAdress = toChecksumAddress(addLeading0x(addressBuffer.toString('hex'))); |
||||
return checkSumAdress; |
||||
} |
@ -1,25 +1,22 @@ |
||||
import { ecdsaRecover } from 'secp256k1'; |
||||
import { removeLeading0x, hexToUnit8Array, uint8ArrayToHex } from './util'; |
||||
|
||||
/** |
||||
* returns the publicKey for the privateKey with which the messageHash was signed |
||||
* @param {string} signature |
||||
* @param {string} hash |
||||
* @return {string} publicKey |
||||
*/ |
||||
export default function recoverPublicKey(signature, hash) { |
||||
signature = removeLeading0x(signature); |
||||
|
||||
// split into v-value and sig
|
||||
var sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
|
||||
var vValue = signature.slice(-2); // last 2 chars
|
||||
export default function recoverPublicKey(signature, hash) { |
||||
signature = removeLeading0x(signature); // split into v-value and sig
|
||||
|
||||
var recoveryNumber = vValue === '1c' ? 1 : 0; |
||||
var sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
|
||||
|
||||
var pubKey = uint8ArrayToHex(ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeLeading0x(hash)), false)); |
||||
var vValue = signature.slice(-2); // last 2 chars
|
||||
|
||||
// remove trailing '04'
|
||||
pubKey = pubKey.slice(2); |
||||
var recoveryNumber = vValue === '1c' ? 1 : 0; |
||||
var pubKey = uint8ArrayToHex(ecdsaRecover(hexToUnit8Array(sigOnly), recoveryNumber, hexToUnit8Array(removeLeading0x(hash)), false)); // remove trailing '04'
|
||||
|
||||
return pubKey; |
||||
pubKey = pubKey.slice(2); |
||||
return pubKey; |
||||
} |
@ -1,14 +1,14 @@ |
||||
import recoverPublicKey from './recover-public-key'; |
||||
import { toAddress as addressByPublicKey } from './public-key'; |
||||
|
||||
/** |
||||
* returns the adress with which the messageHash was signed |
||||
* @param {string} sigString |
||||
* @param {string} hash |
||||
* @return {string} address |
||||
*/ |
||||
|
||||
export default function recover(sigString, hash) { |
||||
var pubkey = recoverPublicKey(sigString, hash); |
||||
var address = addressByPublicKey(pubkey); |
||||
return address; |
||||
var pubkey = recoverPublicKey(sigString, hash); |
||||
var address = addressByPublicKey(pubkey); |
||||
return address; |
||||
} |
@ -1,21 +1,15 @@ |
||||
|
||||
import { Transaction } from '@ethereumjs/tx'; |
||||
import publicKeyByPrivateKey from './public-key-by-private-key'; |
||||
import { toAddress as addressByPublicKey } from './public-key'; |
||||
|
||||
export default function signTransaction(rawTx, privateKey) { |
||||
var txOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
||||
|
||||
|
||||
// check if privateKey->address matches rawTx.from
|
||||
var publicKey = publicKeyByPrivateKey(privateKey); |
||||
var address = addressByPublicKey(publicKey); |
||||
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey'); |
||||
|
||||
var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); |
||||
|
||||
var tx = Transaction.fromTxData(rawTx, txOptions); |
||||
var signedTx = tx.sign(privateKeyBuffer); |
||||
var serializedTx = signedTx.serialize().toString('hex'); |
||||
return serializedTx; |
||||
var txOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
||||
// check if privateKey->address matches rawTx.from
|
||||
var publicKey = publicKeyByPrivateKey(privateKey); |
||||
var address = addressByPublicKey(publicKey); |
||||
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey'); |
||||
var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); |
||||
var tx = Transaction.fromTxData(rawTx, txOptions); |
||||
var signedTx = tx.sign(privateKeyBuffer); |
||||
var serializedTx = signedTx.serialize().toString('hex'); |
||||
return serializedTx; |
||||
} |
@ -1,13 +1,21 @@ |
||||
import { ContractFactory } from 'ethers'; |
||||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } |
||||
|
||||
export default function txDataByCompiled(abi, bytecode, args) { |
||||
// solc returns a string which is often passed instead of the json
|
||||
if (typeof abi === 'string') abi = JSON.parse(abi); |
||||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } |
||||
|
||||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } |
||||
|
||||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } |
||||
|
||||
// Construct a Contract Factory
|
||||
var factory = new ContractFactory(abi, '0x' + bytecode); |
||||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } |
||||
|
||||
var deployTransaction = factory.getDeployTransaction.apply(factory, args); |
||||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } |
||||
|
||||
import { ContractFactory } from 'ethers'; |
||||
export default function txDataByCompiled(abi, bytecode, args) { |
||||
// solc returns a string which is often passed instead of the json
|
||||
if (typeof abi === 'string') abi = JSON.parse(abi); // Construct a Contract Factory
|
||||
|
||||
return deployTransaction.data; |
||||
var factory = new ContractFactory(abi, '0x' + bytecode); |
||||
var deployTransaction = factory.getDeployTransaction.apply(factory, _toConsumableArray(args)); |
||||
return deployTransaction.data; |
||||
} |
@ -1,15 +1,12 @@ |
||||
export function removeLeading0x(str) { |
||||
if (str.startsWith('0x')) return str.substring(2);else return str; |
||||
if (str.startsWith('0x')) return str.substring(2);else return str; |
||||
} |
||||
|
||||
export function addLeading0x(str) { |
||||
if (!str.startsWith('0x')) return '0x' + str;else return str; |
||||
if (!str.startsWith('0x')) return '0x' + str;else return str; |
||||
} |
||||
|
||||
export function uint8ArrayToHex(arr) { |
||||
return Buffer.from(arr).toString('hex'); |
||||
return Buffer.from(arr).toString('hex'); |
||||
} |
||||
|
||||
export function hexToUnit8Array(str) { |
||||
return new Uint8Array(Buffer.from(str, 'hex')); |
||||
return new Uint8Array(Buffer.from(str, 'hex')); |
||||
} |
@ -1,25 +1,25 @@ |
||||
import { decodeSignature, encodeSignature } from 'eth-lib/lib/account'; |
||||
|
||||
/** |
||||
* split signature-hex into parts |
||||
* @param {string} hexString |
||||
* @return {{v: string, r: string, s: string}} |
||||
*/ |
||||
|
||||
export function fromString(hexString) { |
||||
var arr = decodeSignature(hexString); |
||||
return { |
||||
v: arr[0], |
||||
r: arr[1], |
||||
s: arr[2] |
||||
}; |
||||
var arr = decodeSignature(hexString); |
||||
return { |
||||
v: arr[0], |
||||
r: arr[1], |
||||
s: arr[2] |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* merge signature-parts to one string |
||||
* @param {{v: string, r: string, s: string}} sig |
||||
* @return {string} hexString |
||||
*/ |
||||
|
||||
export function toString(sig) { |
||||
var partsArray = [sig.v, sig.r, sig.s]; |
||||
return encodeSignature(partsArray); |
||||
var partsArray = [sig.v, sig.r, sig.s]; |
||||
return encodeSignature(partsArray); |
||||
} |
@ -1,4 +1,4 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
var EthCrypto = require('./index.js'); |
||||
|
||||
|
@ -1,16 +1,16 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports['default'] = calculateContractAddress; |
||||
exports["default"] = calculateContractAddress; |
||||
|
||||
var _ethereumjsUtil = require('ethereumjs-util'); |
||||
var _ethereumjsUtil = require("ethereumjs-util"); |
||||
|
||||
var _util = require('./util'); |
||||
var _util = require("./util"); |
||||
|
||||
function calculateContractAddress(creatorAddress, nonce) { |
||||
var addressBuffer = (0, _ethereumjsUtil.generateAddress)((0, _ethereumjsUtil.toBuffer)((0, _util.addLeading0x)(creatorAddress)), (0, _ethereumjsUtil.toBuffer)(nonce)); |
||||
var address = addressBuffer.toString('hex'); |
||||
return (0, _ethereumjsUtil.toChecksumAddress)((0, _util.addLeading0x)(address)); |
||||
var addressBuffer = (0, _ethereumjsUtil.generateAddress)((0, _ethereumjsUtil.toBuffer)((0, _util.addLeading0x)(creatorAddress)), (0, _ethereumjsUtil.toBuffer)(nonce)); |
||||
var address = addressBuffer.toString('hex'); |
||||
return (0, _ethereumjsUtil.toChecksumAddress)((0, _util.addLeading0x)(address)); |
||||
} |
@ -1,42 +1,35 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.stringify = stringify; |
||||
exports.parse = parse; |
||||
|
||||
var _publicKey = require('./public-key'); |
||||
var _publicKey = require("./public-key"); |
||||
|
||||
function stringify(cipher) { |
||||
if (typeof cipher === 'string') return cipher; |
||||
|
||||
// use compressed key because it's smaller
|
||||
var compressedKey = (0, _publicKey.compress)(cipher.ephemPublicKey); |
||||
|
||||
var ret = Buffer.concat([Buffer.from(cipher.iv, 'hex'), // 16bit
|
||||
Buffer.from(compressedKey, 'hex'), // 33bit
|
||||
Buffer.from(cipher.mac, 'hex'), // 32bit
|
||||
Buffer.from(cipher.ciphertext, 'hex') // var bit
|
||||
]); |
||||
|
||||
return ret.toString('hex'); |
||||
if (typeof cipher === 'string') return cipher; // use compressed key because it's smaller
|
||||
|
||||
var compressedKey = (0, _publicKey.compress)(cipher.ephemPublicKey); |
||||
var ret = Buffer.concat([Buffer.from(cipher.iv, 'hex'), // 16bit
|
||||
Buffer.from(compressedKey, 'hex'), // 33bit
|
||||
Buffer.from(cipher.mac, 'hex'), // 32bit
|
||||
Buffer.from(cipher.ciphertext, 'hex') // var bit
|
||||
]); |
||||
return ret.toString('hex'); |
||||
} |
||||
|
||||
function parse(str) { |
||||
if (typeof str !== 'string') return str; |
||||
|
||||
var buf = Buffer.from(str, 'hex'); |
||||
|
||||
var ret = { |
||||
iv: buf.toString('hex', 0, 16), |
||||
ephemPublicKey: buf.toString('hex', 16, 49), |
||||
mac: buf.toString('hex', 49, 81), |
||||
ciphertext: buf.toString('hex', 81, buf.length) |
||||
}; |
||||
|
||||
// decompress publicKey
|
||||
ret.ephemPublicKey = '04' + (0, _publicKey.decompress)(ret.ephemPublicKey); |
||||
|
||||
return ret; |
||||
if (typeof str !== 'string') return str; |
||||
var buf = Buffer.from(str, 'hex'); |
||||
var ret = { |
||||
iv: buf.toString('hex', 0, 16), |
||||
ephemPublicKey: buf.toString('hex', 16, 49), |
||||
mac: buf.toString('hex', 49, 81), |
||||
ciphertext: buf.toString('hex', 81, buf.length) |
||||
}; // decompress publicKey
|
||||
|
||||
ret.ephemPublicKey = '04' + (0, _publicKey.decompress)(ret.ephemPublicKey); |
||||
return ret; |
||||
} |
@ -1,56 +1,55 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.createPrivateKey = createPrivateKey; |
||||
exports['default'] = createIdentity; |
||||
exports["default"] = createIdentity; |
||||
|
||||
var _publicKeyByPrivateKey = require('./public-key-by-private-key'); |
||||
var _publicKeyByPrivateKey = _interopRequireDefault(require("./public-key-by-private-key")); |
||||
|
||||
var _publicKeyByPrivateKey2 = _interopRequireDefault(_publicKeyByPrivateKey); |
||||
var _account = require("eth-lib/lib/account"); |
||||
|
||||
var _account = require('eth-lib/lib/account'); |
||||
var _hash = require("eth-lib/lib/hash"); |
||||
|
||||
var _hash = require('eth-lib/lib/hash'); |
||||
var _bytes = _interopRequireDefault(require("eth-lib/lib/bytes")); |
||||
|
||||
var _bytes = require('eth-lib/lib/bytes'); |
||||
|
||||
var _bytes2 = _interopRequireDefault(_bytes); |
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } |
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
||||
|
||||
var MIN_ENTROPY_SIZE = 128; |
||||
|
||||
/** |
||||
* create a privateKey from the given entropy or a new one |
||||
* @param {Buffer} entropy |
||||
* @return {string} |
||||
*/ |
||||
|
||||
function createPrivateKey(entropy) { |
||||
if (entropy) { |
||||
if (!Buffer.isBuffer(entropy)) throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer'); |
||||
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE) throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE); |
||||
|
||||
var outerHex = (0, _hash.keccak256)(entropy); |
||||
return outerHex; |
||||
} else { |
||||
// @link https://github.com/MaiaVictor/eth-lib/blob/master/lib/account.js#L8
|
||||
var innerHex = (0, _hash.keccak256)(_bytes2['default'].concat(_bytes2['default'].random(32), _bytes2['default'].random(32))); |
||||
var middleHex = _bytes2['default'].concat(_bytes2['default'].concat(_bytes2['default'].random(32), innerHex), _bytes2['default'].random(32)); |
||||
var _outerHex = (0, _hash.keccak256)(middleHex); |
||||
return _outerHex; |
||||
} |
||||
} |
||||
if (entropy) { |
||||
if (!Buffer.isBuffer(entropy)) throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer'); |
||||
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE) throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE); |
||||
var outerHex = (0, _hash.keccak256)(entropy); |
||||
return outerHex; |
||||
} else { |
||||
// @link https://github.com/MaiaVictor/eth-lib/blob/master/lib/account.js#L8
|
||||
var innerHex = (0, _hash.keccak256)(_bytes["default"].concat(_bytes["default"].random(32), _bytes["default"].random(32))); |
||||
|
||||
var middleHex = _bytes["default"].concat(_bytes["default"].concat(_bytes["default"].random(32), innerHex), _bytes["default"].random(32)); |
||||
|
||||
var _outerHex = (0, _hash.keccak256)(middleHex); |
||||
|
||||
return _outerHex; |
||||
} |
||||
} |
||||
/** |
||||
* creates a new object with |
||||
* private-, public-Key and address |
||||
* @param {Buffer?} entropy if provided, will use that as single random-source |
||||
*/ |
||||
|
||||
|
||||
function createIdentity(entropy) { |
||||
var privateKey = createPrivateKey(entropy); |
||||
var identity = (0, _account.fromPrivate)(privateKey); |
||||
identity.publicKey = (0, _publicKeyByPrivateKey2['default'])(identity.privateKey); |
||||
return identity; |
||||
var privateKey = createPrivateKey(entropy); |
||||
var identity = (0, _account.fromPrivate)(privateKey); |
||||
identity.publicKey = (0, _publicKeyByPrivateKey["default"])(identity.privateKey); |
||||
return identity; |
||||
} |
@ -1,31 +1,27 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports['default'] = decryptWithPrivateKey; |
||||
exports["default"] = decryptWithPrivateKey; |
||||
|
||||
var _eccrypto = require('eccrypto'); |
||||
var _eccrypto = require("eccrypto"); |
||||
|
||||
var _cipher = require('./cipher'); |
||||
var _cipher = require("./cipher"); |
||||
|
||||
var _util = require('./util'); |
||||
var _util = require("./util"); |
||||
|
||||
function decryptWithPrivateKey(privateKey, encrypted) { |
||||
|
||||
encrypted = (0, _cipher.parse)(encrypted); |
||||
|
||||
// remove trailing '0x' from privateKey
|
||||
var twoStripped = (0, _util.removeLeading0x)(privateKey); |
||||
|
||||
var encryptedBuffer = { |
||||
iv: Buffer.from(encrypted.iv, 'hex'), |
||||
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'), |
||||
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'), |
||||
mac: Buffer.from(encrypted.mac, 'hex') |
||||
}; |
||||
|
||||
return (0, _eccrypto.decrypt)(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { |
||||
return decryptedBuffer.toString(); |
||||
}); |
||||
encrypted = (0, _cipher.parse)(encrypted); // remove trailing '0x' from privateKey
|
||||
|
||||
var twoStripped = (0, _util.removeLeading0x)(privateKey); |
||||
var encryptedBuffer = { |
||||
iv: Buffer.from(encrypted.iv, 'hex'), |
||||
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'), |
||||
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'), |
||||
mac: Buffer.from(encrypted.mac, 'hex') |
||||
}; |
||||
return (0, _eccrypto.decrypt)(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then(function (decryptedBuffer) { |
||||
return decryptedBuffer.toString(); |
||||
}); |
||||
} |
@ -1,29 +1,26 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports['default'] = encryptWithPublicKey; |
||||
exports["default"] = encryptWithPublicKey; |
||||
|
||||
var _eccrypto = require('eccrypto'); |
||||
var _eccrypto = require("eccrypto"); |
||||
|
||||
var _publicKey = require('./public-key'); |
||||
var _publicKey = require("./public-key"); |
||||
|
||||
function encryptWithPublicKey(publicKey, message) { |
||||
// ensure its an uncompressed publicKey
|
||||
publicKey = (0, _publicKey.decompress)(publicKey); // re-add the compression-flag
|
||||
|
||||
// ensure its an uncompressed publicKey
|
||||
publicKey = (0, _publicKey.decompress)(publicKey); |
||||
|
||||
// re-add the compression-flag
|
||||
var pubString = '04' + publicKey; |
||||
|
||||
return (0, _eccrypto.encrypt)(Buffer.from(pubString, 'hex'), Buffer.from(message)).then(function (encryptedBuffers) { |
||||
var encrypted = { |
||||
iv: encryptedBuffers.iv.toString('hex'), |
||||
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'), |
||||
ciphertext: encryptedBuffers.ciphertext.toString('hex'), |
||||
mac: encryptedBuffers.mac.toString('hex') |
||||
}; |
||||
return encrypted; |
||||
}); |
||||
var pubString = '04' + publicKey; |
||||
return (0, _eccrypto.encrypt)(Buffer.from(pubString, 'hex'), Buffer.from(message)).then(function (encryptedBuffers) { |
||||
var encrypted = { |
||||
iv: encryptedBuffers.iv.toString('hex'), |
||||
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'), |
||||
ciphertext: encryptedBuffers.ciphertext.toString('hex'), |
||||
mac: encryptedBuffers.mac.toString('hex') |
||||
}; |
||||
return encrypted; |
||||
}); |
||||
} |
@ -1,26 +1,29 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.SIGN_PREFIX = undefined; |
||||
exports.keccak256 = keccak256; |
||||
exports.SIGN_PREFIX = void 0; |
||||
|
||||
var _ethers = require('ethers'); |
||||
var _ethers = require("ethers"); |
||||
|
||||
function keccak256(params) { |
||||
var types = []; |
||||
var values = []; |
||||
if (!Array.isArray(params)) { |
||||
types.push('string'); |
||||
values.push(params); |
||||
} else { |
||||
params.forEach(function (p) { |
||||
types.push(p.type); |
||||
values.push(p.value); |
||||
}); |
||||
} |
||||
return _ethers.utils.solidityKeccak256(types, values); |
||||
var types = []; |
||||
var values = []; |
||||
|
||||
if (!Array.isArray(params)) { |
||||
types.push('string'); |
||||
values.push(params); |
||||
} else { |
||||
params.forEach(function (p) { |
||||
types.push(p.type); |
||||
values.push(p.value); |
||||
}); |
||||
} |
||||
|
||||
return _ethers.utils.solidityKeccak256(types, values); |
||||
} |
||||
|
||||
var SIGN_PREFIX = exports.SIGN_PREFIX = '\x19Ethereum Signed Message:\n32'; |
||||
var SIGN_PREFIX = '\x19Ethereum Signed Message:\n32'; |
||||
exports.SIGN_PREFIX = SIGN_PREFIX; |
@ -1,52 +1,54 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.compress = compress; |
||||
exports.decompress = decompress; |
||||
|
||||
var _util = require('./util'); |
||||
var _util = require("./util"); |
||||
|
||||
/** |
||||
* compress/decompress hex-strings to utf16 or base64 |
||||
* thx @juvian |
||||
* @link https://stackoverflow.com/a/40471908/3443137
|
||||
*/ |
||||
function compress(hex) { |
||||
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
||||
|
||||
hex = (0, _util.removeLeading0x)(hex); |
||||
|
||||
// if base64:true, we use our own function because it results in a smaller output
|
||||
if (base64 === true) return Buffer.from(hex, 'hex').toString('base64'); |
||||
|
||||
var string = ''; |
||||
while (hex.length % 4 != 0) { |
||||
// we need it to be multiple of 4
|
||||
hex = '0' + hex; |
||||
} |
||||
for (var i = 0; i < hex.length; i += 4) { |
||||
// get char from ascii code which goes from 0 to 65536
|
||||
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16)); |
||||
} |
||||
return string; |
||||
} /** |
||||
* compress/decompress hex-strings to utf16 or base64 |
||||
* thx @juvian |
||||
* @link https://stackoverflow.com/a/40471908/3443137
|
||||
*/ |
||||
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
||||
hex = (0, _util.removeLeading0x)(hex); // if base64:true, we use our own function because it results in a smaller output
|
||||
|
||||
if (base64 === true) return Buffer.from(hex, 'hex').toString('base64'); |
||||
var string = ''; |
||||
|
||||
while (hex.length % 4 != 0) { |
||||
// we need it to be multiple of 4
|
||||
hex = '0' + hex; |
||||
} |
||||
|
||||
for (var i = 0; i < hex.length; i += 4) { |
||||
// get char from ascii code which goes from 0 to 65536
|
||||
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16)); |
||||
} |
||||
|
||||
return string; |
||||
} |
||||
|
||||
function decompress(compressedString) { |
||||
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
||||
|
||||
|
||||
// if base64:true, we use our own function because it results in a smaller output
|
||||
if (base64 === true) { |
||||
var ret = Buffer.from(compressedString, 'base64').toString('hex'); |
||||
return (0, _util.addLeading0x)(ret); |
||||
} |
||||
|
||||
var hex = ''; |
||||
for (var i = 0; i < compressedString.length; i++) { |
||||
// get character ascii code and convert to hexa string, adding necessary 0s
|
||||
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4); |
||||
} |
||||
hex = hex.toLowerCase(); |
||||
return (0, _util.addLeading0x)(hex); |
||||
var base64 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
||||
|
||||
// if base64:true, we use our own function because it results in a smaller output
|
||||
if (base64 === true) { |
||||
var ret = Buffer.from(compressedString, 'base64').toString('hex'); |
||||
return (0, _util.addLeading0x)(ret); |
||||
} |
||||
|
||||
var hex = ''; |
||||
|
||||
for (var i = 0; i < compressedString.length; i++) { |
||||
// get character ascii code and convert to hexa string, adding necessary 0s
|
||||
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4); |
||||
} |
||||
|
||||
hex = hex.toLowerCase(); |
||||
return (0, _util.addLeading0x)(hex); |
||||
} |
@ -1,109 +1,138 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.util = exports.vrs = exports.hex = exports.hash = exports.calculateContractAddress = exports.txDataByCompiled = exports.signTransaction = exports.sign = exports.recoverPublicKey = exports.recover = exports.publicKeyByPrivateKey = exports.cipher = exports.encryptWithPublicKey = exports.decryptWithPrivateKey = exports.publicKey = exports.createIdentity = undefined; |
||||
|
||||
var _createIdentity = require('./create-identity'); |
||||
|
||||
var _createIdentity2 = _interopRequireDefault(_createIdentity); |
||||
|
||||
var _publicKey = require('./public-key'); |
||||
|
||||
var publicKey = _interopRequireWildcard(_publicKey); |
||||
|
||||
var _decryptWithPrivateKey = require('./decrypt-with-private-key'); |
||||
|
||||
var _decryptWithPrivateKey2 = _interopRequireDefault(_decryptWithPrivateKey); |
||||
|
||||
var _encryptWithPublicKey = require('./encrypt-with-public-key'); |
||||
|
||||
var _encryptWithPublicKey2 = _interopRequireDefault(_encryptWithPublicKey); |
||||
|
||||
var _cipher = require('./cipher'); |
||||
|
||||
var cipher = _interopRequireWildcard(_cipher); |
||||
|
||||
var _publicKeyByPrivateKey = require('./public-key-by-private-key'); |
||||
|
||||
var _publicKeyByPrivateKey2 = _interopRequireDefault(_publicKeyByPrivateKey); |
||||
|
||||
var _recover = require('./recover'); |
||||
Object.defineProperty(exports, "createIdentity", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _createIdentity["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "decryptWithPrivateKey", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _decryptWithPrivateKey["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "encryptWithPublicKey", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _encryptWithPublicKey["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "publicKeyByPrivateKey", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _publicKeyByPrivateKey["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "recover", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _recover["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "recoverPublicKey", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _recoverPublicKey["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "sign", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _sign["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "signTransaction", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _signTransaction["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "txDataByCompiled", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _txDataByCompiled["default"]; |
||||
} |
||||
}); |
||||
Object.defineProperty(exports, "calculateContractAddress", { |
||||
enumerable: true, |
||||
get: function get() { |
||||
return _calculateContractAddress["default"]; |
||||
} |
||||
}); |
||||
exports.util = exports.vrs = exports.hex = exports.hash = exports.cipher = exports.publicKey = exports["default"] = void 0; |
||||
|
||||
var _recover2 = _interopRequireDefault(_recover); |
||||
var _createIdentity = _interopRequireDefault(require("./create-identity")); |
||||
|
||||
var _recoverPublicKey = require('./recover-public-key'); |
||||
var publicKey = _interopRequireWildcard(require("./public-key")); |
||||
|
||||
var _recoverPublicKey2 = _interopRequireDefault(_recoverPublicKey); |
||||
exports.publicKey = publicKey; |
||||
|
||||
var _sign = require('./sign'); |
||||
var _decryptWithPrivateKey = _interopRequireDefault(require("./decrypt-with-private-key")); |
||||
|
||||
var _sign2 = _interopRequireDefault(_sign); |
||||
var _encryptWithPublicKey = _interopRequireDefault(require("./encrypt-with-public-key")); |
||||
|
||||
var _signTransaction = require('./sign-transaction'); |
||||
var cipher = _interopRequireWildcard(require("./cipher")); |
||||
|
||||
var _signTransaction2 = _interopRequireDefault(_signTransaction); |
||||
exports.cipher = cipher; |
||||
|
||||
var _txDataByCompiled = require('./tx-data-by-compiled'); |
||||
var _publicKeyByPrivateKey = _interopRequireDefault(require("./public-key-by-private-key")); |
||||
|
||||
var _txDataByCompiled2 = _interopRequireDefault(_txDataByCompiled); |
||||
var _recover = _interopRequireDefault(require("./recover")); |
||||
|
||||
var _calculateContractAddress = require('./calculate-contract-address'); |
||||
var _recoverPublicKey = _interopRequireDefault(require("./recover-public-key")); |
||||
|
||||
var _calculateContractAddress2 = _interopRequireDefault(_calculateContractAddress); |
||||
var _sign = _interopRequireDefault(require("./sign")); |
||||
|
||||
var _hash = require('./hash'); |
||||
var _signTransaction = _interopRequireDefault(require("./sign-transaction")); |
||||
|
||||
var hash = _interopRequireWildcard(_hash); |
||||
var _txDataByCompiled = _interopRequireDefault(require("./tx-data-by-compiled")); |
||||
|
||||
var _hex = require('./hex'); |
||||
var _calculateContractAddress = _interopRequireDefault(require("./calculate-contract-address")); |
||||
|
||||
var hex = _interopRequireWildcard(_hex); |
||||
var hash = _interopRequireWildcard(require("./hash")); |
||||
|
||||
var _vrs = require('./vrs'); |
||||
exports.hash = hash; |
||||
|
||||
var vrs = _interopRequireWildcard(_vrs); |
||||
var hex = _interopRequireWildcard(require("./hex")); |
||||
|
||||
var _util = require('./util'); |
||||
exports.hex = hex; |
||||
|
||||
var util = _interopRequireWildcard(_util); |
||||
var vrs = _interopRequireWildcard(require("./vrs")); |
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } |
||||
exports.vrs = vrs; |
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } |
||||
var util = _interopRequireWildcard(require("./util")); |
||||
|
||||
exports.createIdentity = _createIdentity2['default']; |
||||
exports.publicKey = publicKey; |
||||
exports.decryptWithPrivateKey = _decryptWithPrivateKey2['default']; |
||||
exports.encryptWithPublicKey = _encryptWithPublicKey2['default']; |
||||
exports.cipher = cipher; |
||||
exports.publicKeyByPrivateKey = _publicKeyByPrivateKey2['default']; |
||||
exports.recover = _recover2['default']; |
||||
exports.recoverPublicKey = _recoverPublicKey2['default']; |
||||
exports.sign = _sign2['default']; |
||||
exports.signTransaction = _signTransaction2['default']; |
||||
exports.txDataByCompiled = _txDataByCompiled2['default']; |
||||
exports.calculateContractAddress = _calculateContractAddress2['default']; |
||||
exports.hash = hash; |
||||
exports.hex = hex; |
||||
exports.vrs = vrs; |
||||
exports.util = util; |
||||
exports['default'] = { |
||||
createIdentity: _createIdentity2['default'], |
||||
publicKey: publicKey, |
||||
decryptWithPrivateKey: _decryptWithPrivateKey2['default'], |
||||
encryptWithPublicKey: _encryptWithPublicKey2['default'], |
||||
cipher: cipher, |
||||
publicKeyByPrivateKey: _publicKeyByPrivateKey2['default'], |
||||
recover: _recover2['default'], |
||||
recoverPublicKey: _recoverPublicKey2['default'], |
||||
sign: _sign2['default'], |
||||
signTransaction: _signTransaction2['default'], |
||||
txDataByCompiled: _txDataByCompiled2['default'], |
||||
calculateContractAddress: _calculateContractAddress2['default'], |
||||
hash: hash, |
||||
hex: hex, |
||||
vrs: vrs, |
||||
util: util |
||||
}; |
||||
|
||||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } |
||||
|
||||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } |
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
||||
|
||||
var _default = { |
||||
createIdentity: _createIdentity["default"], |
||||
publicKey: publicKey, |
||||
decryptWithPrivateKey: _decryptWithPrivateKey["default"], |
||||
encryptWithPublicKey: _encryptWithPublicKey["default"], |
||||
cipher: cipher, |
||||
publicKeyByPrivateKey: _publicKeyByPrivateKey["default"], |
||||
recover: _recover["default"], |
||||
recoverPublicKey: _recoverPublicKey["default"], |
||||
sign: _sign["default"], |
||||
signTransaction: _signTransaction["default"], |
||||
txDataByCompiled: _txDataByCompiled["default"], |
||||
calculateContractAddress: _calculateContractAddress["default"], |
||||
hash: hash, |
||||
hex: hex, |
||||
vrs: vrs, |
||||
util: util |
||||
}; |
||||
exports["default"] = _default; |
@ -1,51 +1,45 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.compress = compress; |
||||
exports.decompress = decompress; |
||||
exports.toAddress = toAddress; |
||||
|
||||
var _secp256k = require('secp256k1'); |
||||
var _secp256k = require("secp256k1"); |
||||
|
||||
var _ethereumjsUtil = require('ethereumjs-util'); |
||||
var _ethereumjsUtil = require("ethereumjs-util"); |
||||
|
||||
var _util = require('./util'); |
||||
var _util = require("./util"); |
||||
|
||||
function compress(startsWith04) { |
||||
|
||||
// add trailing 04 if not done before
|
||||
var testBuffer = Buffer.from(startsWith04, 'hex'); |
||||
if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; |
||||
|
||||
return (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith04), true)); |
||||
// add trailing 04 if not done before
|
||||
var testBuffer = Buffer.from(startsWith04, 'hex'); |
||||
if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; |
||||
return (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith04), true)); |
||||
} |
||||
|
||||
function decompress(startsWith02Or03) { |
||||
// if already decompressed an not has trailing 04
|
||||
var testBuffer = Buffer.from(startsWith02Or03, 'hex'); |
||||
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; |
||||
var decompressed = (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith02Or03), false)); // remove trailing 04
|
||||
|
||||
// if already decompressed an not has trailing 04
|
||||
var testBuffer = Buffer.from(startsWith02Or03, 'hex'); |
||||
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; |
||||
|
||||
var decompressed = (0, _util.uint8ArrayToHex)((0, _secp256k.publicKeyConvert)((0, _util.hexToUnit8Array)(startsWith02Or03), false)); |
||||
|
||||
// remove trailing 04
|
||||
decompressed = decompressed.substring(2); |
||||
return decompressed; |
||||
decompressed = decompressed.substring(2); |
||||
return decompressed; |
||||
} |
||||
|
||||
/** |
||||
* generates the ethereum-adress of the publicKey |
||||
* We create the checksum-adress which is case-sensitive |
||||
* @returns {string} address |
||||
*/ |
||||
function toAddress(publicKey) { |
||||
|
||||
// normalize key
|
||||
publicKey = decompress(publicKey); |
||||
|
||||
var addressBuffer = (0, _ethereumjsUtil.pubToAddress)((0, _ethereumjsUtil.toBuffer)((0, _util.addLeading0x)(publicKey))); |
||||
var checkSumAdress = (0, _ethereumjsUtil.toChecksumAddress)((0, _util.addLeading0x)(addressBuffer.toString('hex'))); |
||||
return checkSumAdress; |
||||
function toAddress(publicKey) { |
||||
// normalize key
|
||||
publicKey = decompress(publicKey); |
||||
var addressBuffer = (0, _ethereumjsUtil.pubToAddress)((0, _ethereumjsUtil.toBuffer)((0, _util.addLeading0x)(publicKey))); |
||||
var checkSumAdress = (0, _ethereumjsUtil.toChecksumAddress)((0, _util.addLeading0x)(addressBuffer.toString('hex'))); |
||||
return checkSumAdress; |
||||
} |
@ -1,33 +1,29 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports['default'] = signTransaction; |
||||
exports["default"] = signTransaction; |
||||
|
||||
var _tx = require('@ethereumjs/tx'); |
||||
var _tx = require("@ethereumjs/tx"); |
||||
|
||||
var _publicKeyByPrivateKey = require('./public-key-by-private-key'); |
||||
var _publicKeyByPrivateKey = _interopRequireDefault(require("./public-key-by-private-key")); |
||||
|
||||
var _publicKeyByPrivateKey2 = _interopRequireDefault(_publicKeyByPrivateKey); |
||||
var _publicKey = require("./public-key"); |
||||
|
||||
var _publicKey = require('./public-key'); |
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } |
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
||||
|
||||
function signTransaction(rawTx, privateKey) { |
||||
var txOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
||||
|
||||
|
||||
// check if privateKey->address matches rawTx.from
|
||||
var publicKey = (0, _publicKeyByPrivateKey2['default'])(privateKey); |
||||
var address = (0, _publicKey.toAddress)(publicKey); |
||||
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey'); |
||||
|
||||
var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); |
||||
|
||||
var tx = _tx.Transaction.fromTxData(rawTx, txOptions); |
||||
var signedTx = tx.sign(privateKeyBuffer); |
||||
var serializedTx = signedTx.serialize().toString('hex'); |
||||
return serializedTx; |
||||
var txOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
||||
// check if privateKey->address matches rawTx.from
|
||||
var publicKey = (0, _publicKeyByPrivateKey["default"])(privateKey); |
||||
var address = (0, _publicKey.toAddress)(publicKey); |
||||
if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey'); |
||||
var privateKeyBuffer = Buffer.from(privateKey.replace(/^.{2}/g, ''), 'hex'); |
||||
|
||||
var tx = _tx.Transaction.fromTxData(rawTx, txOptions); |
||||
|
||||
var signedTx = tx.sign(privateKeyBuffer); |
||||
var serializedTx = signedTx.serialize().toString('hex'); |
||||
return serializedTx; |
||||
} |
@ -1,27 +1,29 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports["default"] = txDataByCompiled; |
||||
|
||||
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); |
||||
var _ethers = require("ethers"); |
||||
|
||||
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); |
||||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } |
||||
|
||||
exports['default'] = txDataByCompiled; |
||||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } |
||||
|
||||
var _ethers = require('ethers'); |
||||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } |
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } |
||||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } |
||||
|
||||
function txDataByCompiled(abi, bytecode, args) { |
||||
// solc returns a string which is often passed instead of the json
|
||||
if (typeof abi === 'string') abi = JSON.parse(abi); |
||||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } |
||||
|
||||
// Construct a Contract Factory
|
||||
var factory = new _ethers.ContractFactory(abi, '0x' + bytecode); |
||||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } |
||||
|
||||
var deployTransaction = factory.getDeployTransaction.apply(factory, (0, _toConsumableArray3['default'])(args)); |
||||
function txDataByCompiled(abi, bytecode, args) { |
||||
// solc returns a string which is often passed instead of the json
|
||||
if (typeof abi === 'string') abi = JSON.parse(abi); // Construct a Contract Factory
|
||||
|
||||
return deployTransaction.data; |
||||
var factory = new _ethers.ContractFactory(abi, '0x' + bytecode); |
||||
var deployTransaction = factory.getDeployTransaction.apply(factory, _toConsumableArray(args)); |
||||
return deployTransaction.data; |
||||
} |
@ -1,24 +1,25 @@ |
||||
'use strict'; |
||||
"use strict"; |
||||
|
||||
Object.defineProperty(exports, "__esModule", { |
||||
value: true |
||||
value: true |
||||
}); |
||||
exports.removeLeading0x = removeLeading0x; |
||||
exports.addLeading0x = addLeading0x; |
||||
exports.uint8ArrayToHex = uint8ArrayToHex; |
||||
exports.hexToUnit8Array = hexToUnit8Array; |
||||
|
||||
function removeLeading0x(str) { |
||||
if (str.startsWith('0x')) return str.substring(2);else return str; |
||||
if (str.startsWith('0x')) return str.substring(2);else return str; |
||||
} |
||||
|
||||
function addLeading0x(str) { |
||||
if (!str.startsWith('0x')) return '0x' + str;else return str; |
||||
if (!str.startsWith('0x')) return '0x' + str;else return str; |
||||
} |
||||
|
||||
function uint8ArrayToHex(arr) { |
||||
return Buffer.from(arr).toString('hex'); |
||||
return Buffer.from(arr).toString('hex'); |
||||
} |
||||
|
||||
function hexToUnit8Array(str) { |
||||
return new Uint8Array(Buffer.from(str, 'hex')); |
||||
return new Uint8Array(Buffer.from(str, 'hex')); |
||||
} |
Loading…
Reference in new issue