Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const tape = require('tape')
const bls = require('bls-wasm')
const threshold = 4
const dkg = require('../')
bls.init().then(() => {
tape('dkg', t => {
// create the ids
const ids = [0, 1, 2, 3, 4, 5, 6].map(id => {
const sk = new bls.SecretKey()
sk.setHashOf(Buffer.from([id]))
return sk
})
// this stores an array of verifcation vectors. One for each ID
const vvecs = []
// this stores an array of secertKey contrubutions. One for each ID
const skContributions = []
// setup
ids.forEach(id => {
const {verificationVector, secretKeyContribution} = dkg.generateContribution(bls, ids, threshold)
const groupsVvec = dkg.addVerificationVectors(vvecs)
// derive the public key for each id from there secert key share
const groupsPks = []
ids.forEach((id, i) => {
const pk = new bls.PublicKey()
pk.share(groupsVvec, id)
groupsPks.push(pk)
const sk = groupsSks[i]
const pk2 = sk.getPublicKey()
const equal = pk.isEqual(pk2)
t.true(equal, 'public key derived from groups vvec should equal pk from secert share')
})
const groupsPk = new bls.PublicKey()
groupsPk.recover(groupsPks.slice(0, threshold), ids.slice(0, threshold))
const equal = groupsPk.isEqual(groupsVvec[0])
t.true(equal, 'groups public key should equal pk derived from pk shares')
t.end()
})
})
public GenerateSecretDerivedKey(sec: bls.SecretKey, idx: number) : bls.SecretKey {
// todo: implement key deriviation
const sec1 = bls.SecretKey();
sec1.setByCPRNG();
return sec1;
}
ids.forEach((id, i) => {
const pk = new bls.PublicKey()
pk.share(groupsVvec, id)
groupsPks.push(pk)
const sk = groupsSks[i]
const pk2 = sk.getPublicKey()
const equal = pk.isEqual(pk2)
t.true(equal, 'public key derived from groups vvec should equal pk from secert share')
})
const ids = [0, 1, 2, 3, 4, 5, 6].map(id => {
const sk = new bls.SecretKey()
sk.setHashOf(Buffer.from([id]))
return sk
})
public async Init() : Promise {
await bls.init(bls.BLS12_381);
return Promise.resolve();
}
public NewSecretKeyFromSeed(hexStr: string) : bls.SecretKey {
const sec = bls.SecretKey();
const a = bls.fromHexStr(hexStr);
sec.setLittleEndian(a);
return sec;
}
public NewSecretKeyFromHex(hexStr: string) : bls.SecretKey {
const sec = bls.SecretKey();
sec.deserialize(hexStr);
return sec;
}
public constructor(seed: string) {
if (seed.length != 128) throw new Error('Unexpected input length. Expected 128 chars hex string.');
const rndSeed = seed.substring(0,96);
const chainCode = seed.substring(96,128);
this.s = new bls.SecretKey();
this.rndSeed = rndSeed;
this.chainCode = chainCode;
const a = bls.fromHexStr(rndSeed);
this.s.setLittleEndian(a);
}
public NewSecretKeyFromSeed(hexStr: string) : bls.SecretKey {
const sec = bls.SecretKey();
const a = bls.fromHexStr(hexStr);
sec.setLittleEndian(a);
return sec;
}