UNPKG

@gnolang/tm2-js-client

Version:

Tendermint2 JS / TS Client

118 lines (117 loc) 5.69 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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["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 }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringToUTF8 = exports.defaultAddressPrefix = exports.generateKeyPair = exports.generateEntropy = exports.generateHDPath = void 0; exports.encodeCharacterSet = encodeCharacterSet; var crypto_1 = require("@cosmjs/crypto"); var crypto_2 = __importDefault(require("crypto")); /** * Generates the HD path, for the specified index, in the form 'm/44'/118'/0'/0/i', * where 'i' is the account index * @param {number} [index=0] the account index */ var generateHDPath = function (index) { return [ crypto_1.Slip10RawIndex.hardened(44), crypto_1.Slip10RawIndex.hardened(118), crypto_1.Slip10RawIndex.hardened(0), crypto_1.Slip10RawIndex.normal(0), crypto_1.Slip10RawIndex.normal(index ? index : 0), ]; }; exports.generateHDPath = generateHDPath; /** * Generates random entropy of the specified size (in B) * @param {number} [size=32] the entropy size in bytes */ var generateEntropy = function (size) { var array = new Uint8Array(size ? size : 32); // Generate random data crypto_2.default.randomFillSync(array); return array; }; exports.generateEntropy = generateEntropy; /** * Generates a new Secp256k1 key-pair using * the provided English mnemonic and account index * @param {string} mnemonic the English mnemonic * @param {number} [accountIndex=0] the account index */ var generateKeyPair = function (mnemonic, accountIndex) { return __awaiter(void 0, void 0, void 0, function () { var seed, privateKey, publicKey; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, crypto_1.Bip39.mnemonicToSeed(new crypto_1.EnglishMnemonic(mnemonic))]; case 1: seed = _a.sent(); privateKey = crypto_1.Slip10.derivePath(crypto_1.Slip10Curve.Secp256k1, seed, (0, exports.generateHDPath)(accountIndex)).privkey; return [4 /*yield*/, crypto_1.Secp256k1.makeKeypair(privateKey)]; case 2: publicKey = (_a.sent()).pubkey; return [2 /*return*/, { publicKey: publicKey, privateKey: privateKey, }]; } }); }); }; exports.generateKeyPair = generateKeyPair; // Address prefix for TM2 networks exports.defaultAddressPrefix = 'g'; /** * Encodes a string into a Uint8Array * @param {string} str the string to be encoded */ var stringToUTF8 = function (str) { return new TextEncoder().encode(str); }; exports.stringToUTF8 = stringToUTF8; /** * Escapes <,>,& in string. * Golang's json marshaller escapes <,>,& by default. * https://cs.opensource.google/go/go/+/refs/tags/go1.20.6:src/encoding/json/encode.go;l=46-53 */ function encodeCharacterSet(data) { return data .replace(/</g, '\\u003c') .replace(/>/g, '\\u003e') .replace(/&/g, '\\u0026'); }