@desig/web3
Version:
Desig: The Blockchain-Agnostic Multisig Solution
80 lines • 3.68 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connection = exports.EventStreaming = void 0;
const ed25519_1 = require("@noble/ed25519");
const core_1 = require("@desig/core");
const axios_1 = __importDefault(require("axios"));
const bs58_1 = require("bs58");
const utils_1 = require("@noble/hashes/utils");
const sha3_1 = require("@noble/hashes/sha3");
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
var EventStreaming;
(function (EventStreaming) {
EventStreaming["multisig"] = "multisig";
EventStreaming["signer"] = "signer";
EventStreaming["approval"] = "approval";
EventStreaming["signature"] = "signature";
})(EventStreaming = exports.EventStreaming || (exports.EventStreaming = {}));
class Connection {
constructor(cluster, privkey, keypair) {
this.cluster = cluster;
this.privkey = privkey;
this.keypair = keypair;
this.sign = (signerId, message) => {
const msg = (0, utils_1.concatBytes)(signerId, message);
const sig = ed25519_1.sync.sign(msg, this.privkey);
return sig;
};
this.getAuthorization = (data) => __awaiter(this, void 0, void 0, function* () {
const hash = (0, sha3_1.keccak_256)(JSON.stringify({ signer: this.owner, verifier: this.cluster, data }));
const sig = ed25519_1.sync.sign(hash, this.privkey);
const credential = `${this.owner}/${(0, bs58_1.encode)(sig)}`;
return `Bearer ${credential}`;
});
this.health = () => __awaiter(this, void 0, void 0, function* () {
try {
yield axios_1.default.get(`${this.cluster}/health`);
return true;
}
catch (er) {
return false;
}
});
this.on = (event, param, callback) => {
const socket = new isomorphic_ws_1.default(`${this.connection.getUri()}/ws/${event}/${param}`.replace('http', 'ws'));
socket.onmessage = ({ data }) => callback(data.toString());
socket.onerror = ({ message }) => callback('', message);
return () => {
if (socket.close)
socket.close(1000, 'Session ended.');
if (socket.terminate)
socket.terminate();
};
};
this.connection = axios_1.default.create({ baseURL: this.cluster });
}
get owner() {
const pubkey = core_1.EdCurve.getPublicKey(this.privkey);
return (0, bs58_1.encode)(pubkey);
}
get index() {
if (!this.keypair)
throw new Error('Cannot run this function without the keypair');
const { index } = this.keypair.getThreshold();
return index;
}
}
exports.Connection = Connection;
//# sourceMappingURL=connection.js.map