UNPKG

@roochnetwork/rooch-sdk

Version:
95 lines (94 loc) 3.51 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 publickey_exports = {}; __export(publickey_exports, { Ed25519PublicKey: () => Ed25519PublicKey }); module.exports = __toCommonJS(publickey_exports); var import_tweetnacl = __toESM(require("tweetnacl"), 1); var import_address = require("../../address/index.js"); var import_crypto = require("../../crypto/index.js"); var import_utils = require("../../utils/index.js"); const PUBLIC_KEY_SIZE = 32; class Ed25519PublicKey extends import_crypto.PublicKey { /** * Create a new Ed25519PublicKey object * @param value ed25519 public key as buffer or base-64 encoded string */ constructor(value) { super(); if (typeof value === "string") { this.data = (0, import_utils.fromB64)(value); } else if (value instanceof Uint8Array) { this.data = value; } else { this.data = Uint8Array.from(value); } if (this.data.length !== PUBLIC_KEY_SIZE) { throw new Error( `Invalid public key input. Expected ${PUBLIC_KEY_SIZE} bytes, got ${this.data.length}` ); } } /** * Checks if two Ed25519 public keys are equal */ equals(publicKey) { return super.equals(publicKey); } /** * Return the byte array representation of the Ed25519 public key */ toBytes() { return this.data; } /** * Return the Rooch address associated with this Ed25519 public key */ flag() { return import_crypto.SIGNATURE_SCHEME_TO_FLAG.ED25519; } /** * Verifies that the signature is valid for the provided message */ async verify(message, signature) { return import_tweetnacl.default.sign.detached.verify(message, signature, this.toBytes()); } /** * Return the Rooch address associated with this Ed25519 public key */ toAddress() { const tmp = new Uint8Array(PUBLIC_KEY_SIZE + 1); tmp.set([import_crypto.SIGNATURE_SCHEME_TO_FLAG.ED25519]); tmp.set(this.toBytes(), 1); return new import_address.RoochAddress((0, import_utils.blake2b)(tmp, { dkLen: 32 }).slice(0, import_address.ROOCH_ADDRESS_LENGTH * 2)); } } Ed25519PublicKey.SIZE = PUBLIC_KEY_SIZE; //# sourceMappingURL=publickey.js.map