@keyper/container
Version:
keyper container
258 lines (257 loc) • 12.4 kB
JavaScript
"use strict";
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Container = void 0;
var utils = require("@nervosnetwork/ckb-sdk-utils");
var Container = /** @class */ (function () {
function Container(algorithms) {
var _this = this;
this.algorithms = [];
this.providers = [];
this.lockScripts = [];
this.publicKeys = [];
this.holders = {};
algorithms.forEach(function (item) {
_this.algorithms.push(item.algorithm);
_this.providers.push(item.provider);
});
}
Container.prototype.lockScriptSize = function () {
return this.lockScripts.length;
};
Container.prototype.publicKeySize = function () {
return this.publicKeys.length;
};
Container.prototype.addLockScript = function (lockScript) {
var matched = false;
var index = 0;
for (var i = 0; i < this.algorithms.length; i++) {
if (this.algorithms[i] === lockScript.signatureAlgorithm()) {
matched = true;
index = i;
break;
}
}
if (!matched) {
throw Error("container not support " + lockScript.signatureAlgorithm() + " signature algorithm.");
}
for (var i = 0; i < this.lockScripts.length; i++) {
if (lockScript.codeHash === this.lockScripts[i].codeHash && lockScript.hashType === this.lockScripts[i].hashType) {
return;
}
}
lockScript.setProvider(this.providers[index]);
this.initLockScriptHolders(lockScript);
this.lockScripts.push(lockScript);
};
Container.prototype.addPublicKey = function (publicKey) {
if (publicKey.payload === "") {
throw Error("public key is empty.");
}
for (var i = 0; i < this.publicKeys.length; i++) {
if (publicKey.payload === this.publicKeys[i].payload
&& publicKey.algorithm == this.publicKeys[i].algorithm) {
return;
}
}
this.initPublicKeyHolders(publicKey);
this.publicKeys.push(publicKey);
};
Container.prototype.getScriptsByPublicKey = function (publicKey) {
var _this = this;
var result = [];
Object.keys(this.holders).forEach(function (lockHash) {
var holder = _this.holders[lockHash];
if (holder.publicKey.algorithm === publicKey.algorithm
&& holder.publicKey.payload === publicKey.payload) {
result.push(holder.script);
}
});
return result;
};
Container.prototype.removePublicKey = function (publicKey) {
var _this = this;
if (publicKey.payload === "") {
throw Error("public key is empty.");
}
this.publicKeys.forEach(function (item, index) {
if (publicKey.payload === item.payload
&& publicKey.algorithm == item.algorithm) {
_this.publicKeys.splice(index, 1);
Object.keys(_this.holders).map(function (lockHash) {
var holder = _this.holders[lockHash];
if (holder.publicKey.algorithm === publicKey.algorithm
&& holder.publicKey.payload === publicKey.payload) {
delete _this.holders[lockHash];
}
});
return;
}
});
};
Container.prototype.initLockScriptHolders = function (lockScript) {
var _this = this;
this.publicKeys.forEach(function (publicKey) {
if (publicKey.algorithm === lockScript.signatureAlgorithm()) {
var script = lockScript.script(publicKey.payload);
var hash = utils.scriptToHash(script);
_this.holders[hash] = {
publicKey: publicKey,
script: script,
lockScript: lockScript
};
}
});
};
Container.prototype.initPublicKeyHolders = function (publicKey) {
var _this = this;
this.lockScripts.forEach(function (lockScript) {
if (publicKey.algorithm === lockScript.signatureAlgorithm()) {
var script = lockScript.script(publicKey.payload);
var hash = utils.scriptToHash(script);
_this.holders[hash] = {
publicKey: publicKey,
script: script,
lockScript: lockScript
};
}
});
};
Container.prototype.getAllLockHashesAndMeta = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, Object.keys(this.holders).map(function (lockHash) {
return {
hash: lockHash,
meta: {
name: _this.holders[lockHash].lockScript.name,
script: _this.holders[lockHash].script,
deps: _this.holders[lockHash].lockScript.deps(),
headers: _this.holders[lockHash].lockScript.headers ? _this.holders[lockHash].lockScript.headers() : undefined,
},
};
})];
});
});
};
Container.prototype.getLockHashesAndMetaByPublicKey = function (publicKey) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, Object.keys(this.holders).filter(function (lockHash) {
var target = _this.holders[lockHash].publicKey;
return publicKey.algorithm === target.algorithm && publicKey.payload === target.payload;
}).map(function (lockHash) {
return {
hash: lockHash,
meta: {
name: _this.holders[lockHash].lockScript.name,
script: _this.holders[lockHash].script,
deps: _this.holders[lockHash].lockScript.deps(),
headers: _this.holders[lockHash].lockScript.headers ? _this.holders[lockHash].lockScript.headers() : undefined,
},
};
})];
});
});
};
Container.prototype.sign = function (context, rawTx, config) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function () {
var holder, deps, i, dep, found, j, txDep, headers, i, header, found, j, txHeader, result;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
if (!context.lockHash || !this.holders[context.lockHash]) {
throw Error("context hash or holder not exists");
}
holder = this.holders[context.lockHash];
context["publicKey"] = holder.publicKey.payload;
deps = holder.lockScript.deps();
for (i = 0; i < deps.length; i++) {
dep = deps[i];
found = false;
for (j = 0; j < rawTx.cellDeps.length; j++) {
txDep = rawTx.cellDeps[j];
if (txDep.depType === dep.depType
&& ((_a = txDep.outPoint) === null || _a === void 0 ? void 0 : _a.txHash) === ((_b = dep.outPoint) === null || _b === void 0 ? void 0 : _b.txHash)
&& ((_c = txDep.outPoint) === null || _c === void 0 ? void 0 : _c.index) == ((_d = dep.outPoint) === null || _d === void 0 ? void 0 : _d.index)) {
found = true;
break;
}
}
if (!found) {
rawTx.cellDeps.push(dep);
}
}
if (holder.lockScript.headers) {
headers = holder.lockScript.headers();
for (i = 0; i < headers.length; i++) {
header = headers[i];
found = false;
for (j = 0; j < rawTx.headerDeps.length; j++) {
txHeader = rawTx.headerDeps[j];
if (txHeader === header) {
found = true;
break;
}
}
if (!found) {
rawTx.headerDeps.push(header);
}
}
}
return [4 /*yield*/, holder.lockScript.sign(context, rawTx, config)];
case 1:
result = _e.sent();
return [2 /*return*/, result];
}
});
});
};
Container.prototype.send = function (_tx) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw new Error("unsupport for default container");
});
});
};
return Container;
}());
exports.Container = Container;