UNPKG

@terra-money/ledger-terra-js

Version:
124 lines 5.04 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); const crypto = __importStar(require("crypto")); const ripemd160_1 = __importDefault(require("ripemd160")); const bech32_1 = require("bech32"); const device_1 = require("./device"); const APP_NAME_TERRA = "Terra"; const APP_NAME_COSMOS = "Cosmos"; class TerraApp { constructor(transport) { if (!transport) { throw new Error("Transport has not been defined"); } this.transport = transport; } static serializeHRP(hrp) { if (hrp == null || hrp.length < 3 || hrp.length > 83) { throw new Error("Invalid HRP"); } const buf = Buffer.alloc(1 + hrp.length); buf.writeUInt8(hrp.length, 0); buf.write(hrp, 1); return buf; } static getBech32FromPK(hrp, pk) { if (pk.length !== 33) { throw new Error("expected compressed public key [31 bytes]"); } const hashSha256 = crypto.createHash("sha256").update(pk).digest(); const hashRip = new ripemd160_1.default().update(hashSha256).digest(); return bech32_1.bech32.encode(hrp, bech32_1.bech32.toWords(hashRip)); } validateCompatibility() { if (this.info && this.version) { if (this.info.return_code !== 0x9000) { return this.info; } if (this.version.return_code !== 0x9000) { return this.version; } if ((this.info.app_name === APP_NAME_TERRA && this.version.major === 1) || (this.info.app_name === APP_NAME_COSMOS && this.version.major === 2)) { return null; } } return { return_code: 0x6400, error_message: "App Version is not supported", }; } initialize() { return __awaiter(this, void 0, void 0, function* () { return (0, device_1.getAppInfo)(this.transport) .then((appInfo) => { this.info = appInfo; return (0, device_1.getVersion)(this.transport); }) .then((version) => { this.version = version; return this.validateCompatibility(); }); }); } getInfo() { return this.info; } getVersion() { return this.version; } getDeviceInfo() { return (0, device_1.getDeviceInfo)(this.transport); } getPublicKey(path) { const result = (0, device_1.serializePath)(path); const data = Buffer.concat([TerraApp.serializeHRP("terra"), result]); return (0, device_1.publicKey)(this.transport, data); } getAddressAndPubKey(path, hrp) { const result = (0, device_1.serializePath)(path); const data = Buffer.concat([TerraApp.serializeHRP(hrp), result]); return (0, device_1.getAddressAndPubKey)(this.transport, data); } showAddressAndPubKey(path, hrp) { const result = (0, device_1.serializePath)(path); const data = Buffer.concat([TerraApp.serializeHRP(hrp), result]); return (0, device_1.showAddressAndPubKey)(this.transport, data); } sign(path, message) { return (0, device_1.sign)(this.transport, path, message); } } exports.default = TerraApp; //# sourceMappingURL=app.js.map