UNPKG

@tgrospic/rnode-http-js

Version:
63 lines 2.77 kB
var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; import { ec } from 'elliptic'; import * as ethUtil from 'ethereumjs-util'; import { decodeAscii } from '../codecs'; import { deployDataProtobufSerialize } from '../rnode-sign'; /** * Recover public key from Ethereum signed data and signature. * * @param data - Signed message bytes * @param sigHex - Signature base 16 * @returns Public key base 16 */ export var recoverPublicKeyEth = function (data, sigHex) { // Ethereum lib to recover public key from massage and signature var hashed = ethUtil.hashPersonalMessage(ethUtil.toBuffer(__spread(data))); var _a = ethUtil.fromRpcSig(sigHex), v = _a.v, r = _a.r, s = _a.s; // Public key without prefix var pubkeyRecover = ethUtil.ecrecover(hashed, v, r, s); return ethUtil.bufferToHex(Buffer.from(__spread([4], pubkeyRecover))); }; /** * Verify deploy signed with Ethereum compatible signature. */ export var verifyDeployEth = function (deploySigned) { var term = deploySigned.term, timestamp = deploySigned.timestamp, phloPrice = deploySigned.phloPrice, phloLimit = deploySigned.phloLimit, validAfterBlockNumber = deploySigned.validAfterBlockNumber, deployer = deploySigned.deployer, sig = deploySigned.sig; // Serialize deploy data for signing var deploySerialized = deployDataProtobufSerialize({ term: term, timestamp: timestamp, phloPrice: phloPrice, phloLimit: phloLimit, validAfterBlockNumber: validAfterBlockNumber, }); // Create a hash of message with prefix // https://github.com/ethereumjs/ethereumjs-util/blob/4a8001c/src/signature.ts#L136 var deployLen = deploySerialized.length; var msgPrefix = "\u0019Ethereum Signed Message:\n" + deployLen; var prefixBin = decodeAscii(msgPrefix); var msg = __spread(prefixBin, deploySerialized); var hashed = ethUtil.keccak256(Buffer.from(msg)); // Check deployer's signature var crypt = new ec('secp256k1'); var key = crypt.keyFromPublic(deployer); var sigRS = { r: sig.slice(0, 32), s: sig.slice(32, 64) }; var isValid = key.verify(hashed, sigRS); return isValid; }; //# sourceMappingURL=eth-sign.js.map