Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static create(db, passphrase, seed) {
const keyContainer = new KeyContainer(db);
keyContainer.unlock(passphrase);
if (seed === undefined) {
seed = bip39.generateMnemonic();
}
keyContainer.setMasterSeed(seed);
// seed = keyContainer.getMasterSeed();
const keys = keyContainer.createKeys();
const keyOperationalPub = keys.kOp;
db.insert('keyOperationalPub', keyOperationalPub);
const keyDisablePub = keys.kDis;
const keyDisable = `0x${ethUtil.pubToAddress(keyDisablePub, true).toString('hex')}`;
db.insert('keyDisable', keyDisable);
const keyReenablePub = keys.kReen;
const keyReenable = `0x${ethUtil.pubToAddress(keyReenablePub, true).toString('hex')}`;
db.insert('keyReenable', keyReenable);
const keyUpdateRootPub = keys.kUpdateRoot;
const keyUpdateRoot = `0x${ethUtil.pubToAddress(keyUpdateRootPub, true).toString('hex')}`;
db.insert('keyUpdateRoot', keyUpdateRoot);
const {
id, proofClaimKeyOperationalPub, proofClaimKeyDisable, proofClaimKeyReenable, proofClaimKeyUpdateRoot,
} = identityUtils.calculateIdGenesis(keyOperationalPub, keyReenable, keyDisable, keyUpdateRoot);
db.insert('id', id);
db.insert(id, true);
db.insert('proofClaimKeyOperationalPub', JSON.stringify(proofClaimKeyOperationalPub));
db.insert('proofClaimKeyDisable', JSON.stringify(proofClaimKeyDisable));
this.signMessage(index, message, (err, signed) => {
if (err) return cb(err)
// Verify
const signature = Buffer.from(signed.replace('0x', ''), 'hex')
if (signature.length !== 65) cb(new Error(`Frame verifyAddress signature has incorrect length`))
let v = signature[64]
v = v === 0 || v === 1 ? v + 27 : v
let r = toBuffer(signature.slice(0, 32))
let s = toBuffer(signature.slice(32, 64))
const hash = hashPersonalMessage(toBuffer(message))
const verifiedAddress = '0x' + pubToAddress(ecrecover(hash, v, r, s)).toString('hex')
cb(null, verifiedAddress.toLowerCase() === address.toLowerCase())
})
}
if (seed === undefined) {
seed = bip39.generateMnemonic();
}
keyContainer.setMasterSeed(seed);
// seed = keyContainer.getMasterSeed();
const keys = keyContainer.createKeys();
const keyOperationalPub = keys.kOp;
db.insert('keyOperationalPub', keyOperationalPub);
const keyDisablePub = keys.kDis;
const keyDisable = `0x${ethUtil.pubToAddress(keyDisablePub, true).toString('hex')}`;
db.insert('keyDisable', keyDisable);
const keyReenablePub = keys.kReen;
const keyReenable = `0x${ethUtil.pubToAddress(keyReenablePub, true).toString('hex')}`;
db.insert('keyReenable', keyReenable);
const keyUpdateRootPub = keys.kUpdateRoot;
const keyUpdateRoot = `0x${ethUtil.pubToAddress(keyUpdateRootPub, true).toString('hex')}`;
db.insert('keyUpdateRoot', keyUpdateRoot);
const {
id, proofClaimKeyOperationalPub, proofClaimKeyDisable, proofClaimKeyReenable, proofClaimKeyUpdateRoot,
} = identityUtils.calculateIdGenesis(keyOperationalPub, keyReenable, keyDisable, keyUpdateRoot);
db.insert('id', id);
db.insert(id, true);
db.insert('proofClaimKeyOperationalPub', JSON.stringify(proofClaimKeyOperationalPub));
db.insert('proofClaimKeyDisable', JSON.stringify(proofClaimKeyDisable));
db.insert('proofClaimKeyReenable', JSON.stringify(proofClaimKeyReenable));
db.insert('proofClaimKeyUpdateRoot', JSON.stringify(proofClaimKeyUpdateRoot));
return new Promise((resolve, reject) => {
let address = Utils.pubToAddress('0x' + pubKey);
resolve(address.toString('hex'));
});
}
getSignedAddress (signed, message, cb) {
const signature = Buffer.from(signed.replace('0x', ''), 'hex')
if (signature.length !== 65) cb(new Error(`Frame verifySignature: Signature has incorrect length`))
let v = signature[64]
v = v === 0 || v === 1 ? v + 27 : v
const r = toBuffer(signature.slice(0, 32))
const s = toBuffer(signature.slice(32, 64))
const hash = hashPersonalMessage(toBuffer(message))
const verifiedAddress = '0x' + pubToAddress(ecrecover(hash, v, r, s)).toString('hex')
cb(null, verifiedAddress)
}
const getSignatureOwner = async (transaction) => {
let tokenOwner = ''
try {
let sig = ethUtil.fromRpcSig(ethUtil.
addHexPrefix(transaction.signature.toString('hex')))
let msgHash = ethUtil.hashPersonalMessage(transaction.getHash(true))
let pubKey = ethUtil.ecrecover(msgHash, sig.v, sig.r, sig.s)
tokenOwner = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey))
} catch (error) {
throw new Error(rejectCauses.invalidSignature)
}
return tokenOwner
}
export function verifySignedMessage({ address, msg, sig, version }: ISignedMessage) {
const sigb = new Buffer(stripHexPrefixAndLower(sig), 'hex');
if (sigb.length !== 65) {
return false;
}
//TODO: explain what's going on here
sigb[64] = sigb[64] === 0 || sigb[64] === 1 ? sigb[64] + 27 : sigb[64];
const hash = version === '2' ? hashPersonalMessage(toBuffer(msg)) : sha3(msg);
const pubKey = ecrecover(hash, sigb[64], sigb.slice(0, 32), sigb.slice(32, 64));
return stripHexPrefixAndLower(address) === pubToAddress(pubKey).toString('hex');
}
function publicKeyToAddress(publicKey) {
const buf = secp256k1.publicKeyConvert(publicKey, false).slice(1);
return '0x' + ethjsUtil.pubToAddress(buf).toString('hex');
}
export function verifySignedMessage({ address, msg, sig, version }: ISignedMessage) {
const sigb = new Buffer(stripHexPrefixAndLower(sig), 'hex');
if (sigb.length !== 65) {
return false;
}
//TODO: explain what's going on here
sigb[64] = sigb[64] === 0 || sigb[64] === 1 ? sigb[64] + 27 : sigb[64];
const hash = version === '2' ? hashPersonalMessage(toBuffer(msg)) : sha3(msg);
const pubKey = ecrecover(hash, sigb[64], sigb.slice(0, 32), sigb.slice(32, 64));
return stripHexPrefixAndLower(address) === pubToAddress(pubKey).toString('hex');
}