UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

102 lines (101 loc) 3.94 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var ed25519_hd_key_exports = {}; __export(ed25519_hd_key_exports, { derivePath: () => derivePath, getMasterKeyFromSeed: () => getMasterKeyFromSeed, getPublicKey: () => getPublicKey, isValidPath: () => isValidPath, pathRegex: () => pathRegex, replaceDerive: () => replaceDerive }); module.exports = __toCommonJS(ed25519_hd_key_exports); var import_bcs = require("@mysten/bcs"); var import_hmac = require("@noble/hashes/hmac"); var import_sha512 = require("@noble/hashes/sha512"); var import_tweetnacl = __toESM(require("tweetnacl")); const ED25519_CURVE = "ed25519 seed"; const HARDENED_OFFSET = 2147483648; const pathRegex = new RegExp("^m(\\/[0-9]+')+$"); const replaceDerive = (val) => val.replace("'", ""); const getMasterKeyFromSeed = (seed) => { const h = import_hmac.hmac.create(import_sha512.sha512, ED25519_CURVE); const I = h.update((0, import_bcs.fromHEX)(seed)).digest(); const IL = I.slice(0, 32); const IR = I.slice(32); return { key: IL, chainCode: IR }; }; const CKDPriv = ({ key, chainCode }, index) => { const indexBuffer = new ArrayBuffer(4); const cv = new DataView(indexBuffer); cv.setUint32(0, index); const data = new Uint8Array(1 + key.length + indexBuffer.byteLength); data.set(new Uint8Array(1).fill(0)); data.set(key, 1); data.set(new Uint8Array(indexBuffer, 0, indexBuffer.byteLength), key.length + 1); const I = import_hmac.hmac.create(import_sha512.sha512, chainCode).update(data).digest(); const IL = I.slice(0, 32); const IR = I.slice(32); return { key: IL, chainCode: IR }; }; const getPublicKey = (privateKey, withZeroByte = true) => { const keyPair = import_tweetnacl.default.sign.keyPair.fromSeed(privateKey); const signPk = keyPair.secretKey.subarray(32); const newArr = new Uint8Array(signPk.length + 1); newArr.set([0]); newArr.set(signPk, 1); return withZeroByte ? newArr : signPk; }; const isValidPath = (path) => { if (!pathRegex.test(path)) { return false; } return !path.split("/").slice(1).map(replaceDerive).some( isNaN /* ts T_T*/ ); }; const derivePath = (path, seed, offset = HARDENED_OFFSET) => { if (!isValidPath(path)) { throw new Error("Invalid derivation path"); } const { key, chainCode } = getMasterKeyFromSeed(seed); const segments = path.split("/").slice(1).map(replaceDerive).map((el) => parseInt(el, 10)); return segments.reduce((parentKeys, segment) => CKDPriv(parentKeys, segment + offset), { key, chainCode }); }; //# sourceMappingURL=ed25519-hd-key.js.map