@bsv/wallet-toolbox-client
Version:
Client only Wallet Storage
67 lines • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyOffsetToHashedSecret = keyOffsetToHashedSecret;
exports.offsetPrivKey = offsetPrivKey;
exports.offsetPubKey = offsetPubKey;
exports.lockScriptWithKeyOffsetFromPubKey = lockScriptWithKeyOffsetFromPubKey;
exports.createStorageServiceChargeScript = createStorageServiceChargeScript;
exports.redeemServiceCharges = redeemServiceCharges;
const sdk_1 = require("@bsv/sdk");
const utilityHelpers_1 = require("../../utility/utilityHelpers");
function keyOffsetToHashedSecret(pub, keyOffset) {
let offset;
if (keyOffset !== undefined && typeof keyOffset === 'string') {
if (keyOffset.length === 64)
offset = sdk_1.PrivateKey.fromString(keyOffset, 'hex');
else
offset = sdk_1.PrivateKey.fromWif(keyOffset);
}
else {
offset = sdk_1.PrivateKey.fromRandom();
keyOffset = offset.toWif();
}
const sharedSecret = pub.mul(offset).encode(true, undefined);
const hashedSecret = (0, utilityHelpers_1.sha256Hash)(sharedSecret);
return { hashedSecret: new sdk_1.BigNumber(hashedSecret), keyOffset };
}
function offsetPrivKey(privKey, keyOffset) {
const priv = sdk_1.PrivateKey.fromWif(privKey);
const pub = priv.toPublicKey();
const r = keyOffsetToHashedSecret(pub, keyOffset);
const bn = priv.add(r.hashedSecret).mod(new sdk_1.Curve().n);
const offsetPrivKey = new sdk_1.PrivateKey(bn).toWif();
return { offsetPrivKey, keyOffset: r.keyOffset };
}
function offsetPubKey(pubKey, keyOffset) {
const pub = sdk_1.PublicKey.fromString(pubKey);
const r = keyOffsetToHashedSecret(pub, keyOffset);
// The hashed secret is multiplied by the generator point.
const point = new sdk_1.Curve().g.mul(r.hashedSecret);
// The resulting point is added to the recipient public key.
const offsetPubKey = new sdk_1.PublicKey(pub.add(point));
return { offsetPubKey: offsetPubKey.toString(), keyOffset: r.keyOffset };
}
function lockScriptWithKeyOffsetFromPubKey(pubKey, keyOffset) {
const r = offsetPubKey(pubKey, keyOffset);
const offsetPub = sdk_1.PublicKey.fromString(r.offsetPubKey);
const hash = offsetPub.toHash();
const script = new sdk_1.P2PKH().lock(hash).toHex();
return { script, keyOffset: r.keyOffset };
}
function createStorageServiceChargeScript(pubKeyHex) {
return lockScriptWithKeyOffsetFromPubKey(pubKeyHex);
}
function redeemServiceCharges(privateKeyWif, charges) {
const priv = sdk_1.PrivateKey.fromWif(privateKeyWif);
const pub = priv.toPublicKey();
const p2pkh = new sdk_1.P2PKH();
const inputs = [];
for (const c of charges) {
const { hashedSecret } = keyOffsetToHashedSecret(pub, c.keyOffset);
const bn = priv.add(hashedSecret).mod(new sdk_1.Curve().n);
const offsetPrivKey = new sdk_1.PrivateKey(bn);
//const unlock = p2pkh.unlock(offsetPrivKey, signOutputs, anyoneCanPay)
}
return [];
}
//# sourceMappingURL=offsetKey.js.map