UNPKG

@siacentral/ledgerjs-sia

Version:
279 lines 13.7 kB
"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 (g && (g = 0, op[0] && (_ = 0)), _) 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 }; } }; exports.__esModule = true; var buffer_1 = require("buffer"); var base64_1 = require("@stablelib/base64"); function uint32ToBuffer(val) { var buf = buffer_1.Buffer.alloc(4); buf.writeUInt32LE(val, 0); return buf; } /** * Sia * * @example * import Sia from '@siacentral/ledgerjs-sia'; * const sia = new Sia(transport) */ var Sia = /** @class */ (function () { function Sia(transport, scrambleKey) { if (scrambleKey === void 0) { scrambleKey = 'Sia'; } this.transport = transport; transport.decorateAppAPIMethods(this, [ 'signTransactionV044', 'signTransaction', 'signV2Transaction', 'verifyPublicKey', 'verifyStandardAddress', 'signHash', ], scrambleKey); } /** * getVersion returns the version of the Sia app * * @returns {string} the current version of the Sia app. */ Sia.prototype.getVersion = function () { return __awaiter(this, void 0, void 0, function () { var resp; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.transport.send(0xe0, 0x01, 0x00, 0x00, buffer_1.Buffer.alloc(0))]; case 1: resp = _a.sent(); return [2 /*return*/, "v".concat(resp[0], ".").concat(resp[1], ".").concat(resp[2])]; } }); }); }; /** * verifyPublicKey returns the public key and standard Sia address for * the provided public key index. The user will be asked to verify the * public key on the display. A standard address is defined as an address * having 1 public key, requiring 1 signature, and no timelock. * @param index {number} the index of the public key * @returns {VerifyResponse} the public key and standard address */ Sia.prototype.verifyPublicKey = function (index) { return __awaiter(this, void 0, void 0, function () { var resp; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.transport.send(0xe0, 0x02, 0x00, 0x01, uint32ToBuffer(index))]; case 1: resp = _a.sent(); // the status code is appended as the last 2 bytes of the response, but // the transport already handles invalid codes. return [2 /*return*/, { publicKey: "ed25519:".concat(resp.slice(0, 32).reduce(function (v, b) { return v + ('0' + b.toString(16)).slice(-2); }, '')), address: resp.slice(32, resp.length - 2).toString() }]; } }); }); }; /** * verifyStandardAddress returns the public key and standard Sia address for * the provided public key index. The user will be asked to verify the * address on the display. A standard address is defined as an address * having 1 public key, requiring 1 signature, and no timelock. * @param index {number} the index of the public key * @returns {VerifyResponse} the public key and standard address */ Sia.prototype.verifyStandardAddress = function (index) { return __awaiter(this, void 0, void 0, function () { var resp; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.transport.send(0xe0, 0x02, 0x00, 0x00, uint32ToBuffer(index))]; case 1: resp = _a.sent(); // the status code is appended as the last 2 bytes of the response, but // the transport already handles invalid codes. return [2 /*return*/, { publicKey: "ed25519:".concat(resp.slice(0, 32).reduce(function (v, b) { return v + ('0' + b.toString(16)).slice(-2); }, '')), address: resp.slice(32, resp.length - 2).toString() }]; } }); }); }; /** * signTransactionV044 signs the transaction with the provided key * @deprecated deprecated in v0.4.5 * @param encodedTxn {Buffer} a sia encoded transaction * @param sigIndex {number} the index of the signature to sign * @param keyIndex {number} the index of the key to sign with * @returns {string} the base64 encoded signature */ Sia.prototype.signTransactionV044 = function (encodedTxn, sigIndex, keyIndex) { return __awaiter(this, void 0, void 0, function () { var buf, resp, i; return __generator(this, function (_a) { switch (_a.label) { case 0: buf = buffer_1.Buffer.alloc(encodedTxn.length + 6); resp = buffer_1.Buffer.alloc(0); if (encodedTxn.length === 0) throw new Error('empty transaction'); buf.writeUInt32LE(keyIndex, 0); buf.writeUInt16LE(sigIndex, 4); buf.set(encodedTxn, 6); i = 0; _a.label = 1; case 1: if (!(i < buf.length)) return [3 /*break*/, 4]; return [4 /*yield*/, this.transport.send(0xe0, 0x08, i === 0 ? 0x00 : 0x80, 0x01, buffer_1.Buffer.from(buf.subarray(i, i + 255)))]; case 2: // INS_GET_TXN_HASH = 0x08 resp = _a.sent(); _a.label = 3; case 3: i += 255; return [3 /*break*/, 1]; case 4: console.log(resp); console.log(resp.length); return [2 /*return*/, (0, base64_1.encode)(resp.slice(0, resp.length - 2))]; } }); }); }; /** * signTransaction signs the transaction with the provided key * @param encodedTxn {Buffer} a sia encoded transaction * @param sigIndex {number} the index of the signature to sign * @param keyIndex {number} the index of the key to sign with * @param changeIndex {number} the index of the key used for the change output * @returns {string} the base64 encoded signature */ Sia.prototype.signTransaction = function (encodedTxn, sigIndex, keyIndex, changeIndex) { return __awaiter(this, void 0, void 0, function () { var buf, resp, i; return __generator(this, function (_a) { switch (_a.label) { case 0: buf = buffer_1.Buffer.alloc(encodedTxn.length + 10); resp = buffer_1.Buffer.alloc(0); if (encodedTxn.length === 0) throw new Error('empty transaction'); buf.writeUInt32LE(keyIndex, 0); buf.writeUInt16LE(sigIndex, 4); buf.writeUInt32LE(changeIndex, 6); buf.set(encodedTxn, 10); i = 0; _a.label = 1; case 1: if (!(i < buf.length)) return [3 /*break*/, 4]; return [4 /*yield*/, this.transport.send(0xe0, 0x08, i === 0 ? 0x00 : 0x80, 0x01, buffer_1.Buffer.from(buf.subarray(i, i + 255)))]; case 2: // INS_GET_TXN_HASH = 0x08 resp = _a.sent(); _a.label = 3; case 3: i += 255; return [3 /*break*/, 1]; case 4: return [2 /*return*/, (0, base64_1.encode)(resp)]; } }); }); }; /** * signV2Transaction signs the v2 transaction with the provided key * @param encodedTxn {Buffer} a sia encoded (V2TransactionSemantics) v2 transaction * @param sigIndex {number} the index of the signature to sign * @param keyIndex {number} the index of the key to sign with * @param changeIndex {number} the index of the key used for the change output * @returns {string} the base64 encoded signature */ Sia.prototype.signV2Transaction = function (encodedTxn, sigIndex, keyIndex, changeIndex) { return __awaiter(this, void 0, void 0, function () { var buf, resp, i; return __generator(this, function (_a) { switch (_a.label) { case 0: buf = buffer_1.Buffer.alloc(encodedTxn.length + 10); resp = buffer_1.Buffer.alloc(0); if (encodedTxn.length === 0) throw new Error('empty transaction'); buf.writeUInt32LE(keyIndex, 0); buf.writeUInt16LE(sigIndex, 4); buf.writeUInt32LE(changeIndex, 6); buf.set(encodedTxn, 10); i = 0; _a.label = 1; case 1: if (!(i < buf.length)) return [3 /*break*/, 4]; return [4 /*yield*/, this.transport.send(0xe0, 0x10, i === 0 ? 0x00 : 0x80, 0x01, buffer_1.Buffer.from(buf.subarray(i, i + 255)))]; case 2: // INS_GET_V2TXN_HASH = 0x10 resp = _a.sent(); _a.label = 3; case 3: i += 255; return [3 /*break*/, 1]; case 4: return [2 /*return*/, resp.reduce(function (v, b) { return v + ('0' + b.toString(16)).slice(-2); }, '')]; } }); }); }; Sia.prototype.blindSign = function (sigHash, keyIndex) { return __awaiter(this, void 0, void 0, function () { var buf, resp; return __generator(this, function (_a) { switch (_a.label) { case 0: buf = buffer_1.Buffer.alloc(sigHash.length + 4); resp = buffer_1.Buffer.alloc(0); buf.writeUInt32LE(keyIndex); buf.set(sigHash, 4); return [4 /*yield*/, this.transport.send(0xe0, 0x04, 0x00, 0x00, buf)]; case 1: resp = _a.sent(); return [2 /*return*/, resp.slice(0, resp.length - 2).reduce(function (v, b) { return v + ('0' + b.toString(16)).slice(-2); }, '')]; } }); }); }; Sia.prototype.close = function () { return this.transport.close(); }; return Sia; }()); exports["default"] = Sia; //# sourceMappingURL=sia.js.map