@desig/web3
Version:
Desig: The Blockchain-Agnostic Multisig Solution
85 lines • 4.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Signer = void 0;
const core_1 = require("@desig/core");
const bs58_1 = require("bs58");
const connection_1 = require("./connection");
const utils_1 = require("@noble/hashes/utils");
const keypair_1 = require("./keypair");
const utils_2 = require("./utils");
const transaction_parser_1 = require("./transaction/transaction.parser");
class Signer extends connection_1.Connection {
constructor(cluster, privkey) {
super(cluster, (0, bs58_1.decode)(privkey));
this.watch = (callback) => {
const unwatch = this.on(connection_1.EventStreaming.signer, this.owner, callback);
return unwatch;
};
this.getAllSigners = (filter = {}) => __awaiter(this, void 0, void 0, function* () {
const { data } = yield this.connection.get('/signer', {
params: { owner: this.owner },
});
return data;
});
this.getSigner = (signerId) => __awaiter(this, void 0, void 0, function* () {
const { data } = yield this.connection.get(`/signer/${signerId}`);
return data;
});
this.activateSigner = (signerId) => __awaiter(this, void 0, void 0, function* () {
let { encryptedShare, genesis, multisig: { id: multisigId, curve }, } = yield this.getSigner(signerId);
if (!encryptedShare) {
const txParser = new transaction_parser_1.TransactionParser();
const extendedElgamal = new core_1.ExtendedElGamal();
const elgamal = new core_1.ElGamal();
const ff = utils_2.ec[curve].ff;
const sss = new core_1.SecretSharing(ff);
const { data: { raw, signatures }, } = yield this.connection.get(`/transaction/${genesis}`);
const tx = (0, bs58_1.decode)(raw);
const { refgid, t, n, kr } = txParser.nExtension.decode(tx);
const _t = ff.decode(ff.numberToRedBN(Number(t)), 8);
const _n = ff.decode(ff.numberToRedBN(Number(n)), 8);
const z = sss.interpolate((0, bs58_1.decode)(signerId), signatures
.filter(({ signature }) => !!signature)
.map(({ signature, signer: { id } }) => [
(0, bs58_1.decode)(id),
(0, bs58_1.decode)(signature),
])
.map(([index, signature]) => {
const commitment = signature.subarray(64);
return (0, utils_1.concatBytes)(index, _t, _n, refgid, commitment);
}));
const r = elgamal.decrypt(kr, this.privkey);
const s = sss.ff.sub(z, r);
const share = (0, utils_1.concatBytes)((0, bs58_1.decode)(signerId), _t, _n, refgid, s);
const secret = `${curve}/${multisigId}/${(0, bs58_1.encode)(share)}`;
encryptedShare = (0, bs58_1.encode)(extendedElgamal.encrypt(new TextEncoder().encode(secret), (0, bs58_1.decode)(this.owner)));
}
const payload = { activated: true, encryptedShare };
const Authorization = yield this.getAuthorization(payload);
const { data } = yield this.connection.patch(`/signer/${signerId}`, payload, {
headers: {
Authorization,
},
});
return data;
});
this.getSignerKeypair = (signerId) => __awaiter(this, void 0, void 0, function* () {
const { encryptedShare } = yield this.getSigner(signerId);
const elgamal = new core_1.ExtendedElGamal();
const buf = elgamal.decrypt((0, bs58_1.decode)(encryptedShare), this.privkey);
const secretKey = new TextDecoder().decode(buf);
return new keypair_1.DesigKeypair(secretKey);
});
}
}
exports.Signer = Signer;
//# sourceMappingURL=signer.js.map