Skip to content

Instantly share code, notes, and snippets.

@guggero
Created July 27, 2020 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guggero/8f06d1da926e7024220b352184c61933 to your computer and use it in GitHub Desktop.
Save guggero/8f06d1da926e7024220b352184c61933 to your computer and use it in GitHub Desktop.
Shared Key test script
// Some gRPC and TLS setup code here.
var customKeyLoc = {
key_family: 6,
key_index: 0,
};
var customKeyDesc = {
key_loc: customKeyLoc,
};
var ephemeralPrivKey = bufferToInt(Buffer.from(randomBytes(32)));
var ephemeralPubKey = secp256k1.G.multiply(ephemeralPrivKey);
client.getInfo({}, (err, res) => {
if (err != null) {
console.log(err);
}
var pubKeyBuf = Buffer.from(res.identity_pubkey, 'hex');
var pubKey = pubKeyToPoint(pubKeyBuf);
var sharedPubKey = pubKey.multiply(ephemeralPrivKey);
var sharedKey = hash(pointToBuffer(sharedPubKey));
console.log('Our shared key 1 is: ' + sharedKey.toString('hex'));
signer.deriveSharedKey({
ephemeral_pubkey: pointToBuffer(ephemeralPubKey),
}, (err2, res2) => {
if (err2 != null) {
console.log(err2);
}
console.log('Result 1 from node: ' + res2.shared_key.toString('hex'));
});
});
wallet.deriveKey(customKeyLoc, (err, res) => {
if (err != null) {
console.log(err);
}
console.log(res);
var pubKeyBuf = res.raw_key_bytes;
var pubKey = pubKeyToPoint(pubKeyBuf);
var sharedPubKey = pubKey.multiply(ephemeralPrivKey);
var sharedKey = hash(pointToBuffer(sharedPubKey));
console.log('Our shared key 2 is: ' + sharedKey.toString('hex'));
signer.deriveSharedKey({
ephemeral_pubkey: pointToBuffer(ephemeralPubKey),
key_loc: customKeyLoc,
}, (err2, res2) => {
if (err2 != null) {
console.log(err2);
}
console.log('Result 2 from node: ' + res2.shared_key.toString('hex'));
});
});
wallet.deriveKey(customKeyLoc, (err, res) => {
if (err != null) {
console.log(err);
}
console.log(res);
var pubKeyBuf = res.raw_key_bytes;
var pubKey = pubKeyToPoint(pubKeyBuf);
var sharedPubKey = pubKey.multiply(ephemeralPrivKey);
var sharedKey = hash(pointToBuffer(sharedPubKey));
console.log('Our shared key 3 is: ' + sharedKey.toString('hex'));
signer.deriveSharedKey({
ephemeral_pubkey: pointToBuffer(ephemeralPubKey),
key_desc: customKeyDesc,
}, (err2, res2) => {
if (err2 != null) {
console.log(err2);
}
console.log('Result 3 from node: ' + res2.shared_key.toString('hex'));
});
});
wallet.deriveKey(customKeyLoc, (err, res) => {
if (err != null) {
console.log(err);
}
console.log(res);
var pubKeyBuf = res.raw_key_bytes;
var pubKey = pubKeyToPoint(pubKeyBuf);
var sharedPubKey = pubKey.multiply(ephemeralPrivKey);
var sharedKey = hash(pointToBuffer(sharedPubKey));
console.log('Our shared key 4 is: ' + sharedKey.toString('hex'));
signer.deriveSharedKey({
ephemeral_pubkey: pointToBuffer(ephemeralPubKey),
key_desc: {
raw_key_bytes: pubKeyBuf,
key_loc: {
key_family: 2,
key_index: 0,
}
},
}, (err2, res2) => {
if (err2 != null) {
console.log(err2);
}
console.log('Result 4 from node: ' + res2.shared_key.toString('hex'));
});
});
function pubKeyToPoint(pubKey) {
const pubKeyEven = (pubKey[0] - 0x02) === 0;
const x = bufferToInt(pubKey.slice(1, 33));
const P = secp256k1.pointFromX(!pubKeyEven, x);
checkPointExists(pubKeyEven, P);
return P;
}
function bufferToInt(buffer) {
return BigInteger.fromBuffer(buffer);
}
function hash(buffer) {
return Buffer.from(sha256.create().update(buffer).array());
}
function pointToBuffer(point) {
return point.getEncoded(true);
}
function checkPointExists(pubKeyEven, P) {
if (P.curve.isInfinity(P)) {
throw new Error('point is at infinity');
}
const pEven = P.affineY.isEven();
if (pubKeyEven !== pEven) {
throw new Error('point does not exist');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment