Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
const Fabric = require('../');
const assert = require('assert');
const expect = require('chai').expect;
const crypto = require('crypto');
const net = require('net');
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const Peer = require('../lib/peer');
const Message = require('../lib/message');
// const key = '/test';
const data = require('../data/message');
const TEST_PORT = 7775;
const GENESIS_DATA = 'Hello, world!';
const BULK_COUNT = 1000;
describe('Peer', function () {
it('should expose a constructor', function () {
assert.equal(Peer instanceof Function, true);
});
export function verifyWithSha256(msg: any, pubKeyHex: string, signature: string): boolean {
// private key from hex
var ec = new EC('secp256k1');
var key = ec.keyFromPublic(decodePubKey(pubKeyHex), 'hex');
// signmsg
const signByte = shajs('sha256')
.update(msg)
.digest();
// sign to get signaturegit stat
const res = key.verify(signByte, signature);
return res;
}
function isKeyMatch(privKeyHex, pubKeyHex) {
const ec = new elliptic.ec('secp256k1');
var key = ec.keyFromPrivate(decodePrivKey(privKeyHex), 'hex');
return key.getPublic(true, 'hex').toUpperCase() == decodePubKey(pubKeyHex);
}
// deterministically generates new priv-key bytes from provided key.
function ECDH(curve) {
this.curveType = aliases[curve];
if (!this.curveType ) {
this.curveType = {
name: curve
};
}
this.curve = new elliptic.ec(this.curveType.name);
this.keys = void 0;
}
function getDerivedKey (enclavePublicKey, clientPrivateKey) {
let ec = new EC ('secp256k1');
if (enclavePublicKey.slice (0, 2) != '04') {
enclavePublicKey = '04' + enclavePublicKey;
}
let client_key = ec.keyFromPrivate (clientPrivateKey, 'hex');
let enclave_key = ec.keyFromPublic (enclavePublicKey, 'hex');
let shared_points = enclave_key.getPublic ().mul (client_key.getPrivate ());
let y = 0x02 | (shared_points.getY ().isOdd () ? 1 : 0);
let x = shared_points.getX ();
let y_buffer = Buffer.from ([y]);
let x_buffer = Buffer.from (x.toString (16), 'hex');
let sha256 = forge.md.sha256.create ();
sha256.update (y_buffer.toString ('binary'));
sha256.update (x_buffer.toString ('binary'));
export function ECKeySign(hashBytes, priKeyBytes) {
const ec = new EC('secp256k1');
const key = ec.keyFromPrivate(priKeyBytes, 'bytes');
const signature = key.sign(hashBytes);
const r = signature.r;
const s = signature.s;
const id = signature.recoveryParam;
let rHex = r.toString('hex');
while (rHex.length < 64) {
rHex = `0${rHex}`;
}
let sHex = s.toString('hex');
while (sHex.length < 64) {
sHex = `0${sHex}`;
computeEcDSASignature(hash: string): string {
const ec = new elliptic.ec(this.parameters.curve.preset);
const signed = ec.sign(hash, this.key, { canonical: true });
return Buffer.concat([
signed.r.toArrayLike(Buffer, 'be', 32),
signed.s.toArrayLike(Buffer, 'be', 32)
]).toString('hex');
}
getPublic(compressed = true) {
const ec = new EC("secp256k1");
const key = ec.keyFromPrivate(this.body.toHex(0, this.body.capacity()), "hex");
const pub = key.getPublic();
const uncompressed_hex = pub.x.toString("hex", 64) + pub.y.toString("hex", 64);
const buffer = new ByteBuffer.fromHex(uncompressed_hex).prepend("04", "hex", 0);
const uncompressed_pub = new Publickey(buffer);
if (compressed) return new Publickey(uncompressed_pub.compressed);
else return uncompressed_pub;
}
export function ES256KSigner () {
const secp256k1 = new ec('secp256k1')
function hash (payload) {
return Buffer.from(sha256.arrayBuffer(payload))
}
function toJose (signature) {
const jose = Buffer.alloc(64)
Buffer.from(signature.r, 'hex').copy(jose, 0)
Buffer.from(signature.s, 'hex').copy(jose, 32)
return base64url.encode(jose)
}
return async function sign (payload, signer) {
const signature = await signer(hash(payload))
return toJose(signature)
}
function getCurve() {
if (!_curve) {
_curve = new elliptic_1.ec("secp256k1");
}
return _curve;
}
var SigningKey = /** @class */ (function () {