UNPKG

@planetarium/tx

Version:

Creating Libplanet transactions from JavaScript/TypeScript

258 lines (245 loc) • 8.53 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { encodeAddress: () => encodeAddress, encodeCurrency: () => encodeCurrency, encodeFungibleAssetValue: () => encodeFungibleAssetValue, encodeSignedTx: () => encodeSignedTx, encodeTxMetadata: () => encodeTxMetadata, encodeUnsignedTx: () => encodeUnsignedTx, getCurrencyHash: () => getCurrencyHash, getMajorUnit: () => getMajorUnit, getMinorUnit: () => getMinorUnit, getSign: () => getSign, signTx: () => signTx }); module.exports = __toCommonJS(src_exports); // src/binary.ts function compareUint8Array(a, b) { const length = Math.min(a.length, b.length); for (let i = 0; i < length; i++) { if (a[i] !== b[i]) return a[i] - b[i]; } return a.length - b.length; } __name(compareUint8Array, "compareUint8Array"); // src/address.ts function encodeAddress(address) { if (address.length !== 20) { throw new TypeError( `Address must be 20 bytes long, but got ${address.length} bytes.` ); } return address; } __name(encodeAddress, "encodeAddress"); function encodeAddressSet(addresses) { const array = []; addresses.forEach((addr) => array.push(addr)); array.sort(compareUint8Array); return array.map(encodeAddress); } __name(encodeAddressSet, "encodeAddressSet"); // src/assets.ts var import_bencodex = require("@planetarium/bencodex"); function encodeCurrency(currency) { const minters = currency.minters === null ? null : encodeAddressSet(currency.minters); const serialized = { ticker: currency.ticker, decimalPlaces: new Uint8Array([currency.decimalPlaces]), minters }; if (currency.maximumSupply !== null) { if (!currency.totalSupplyTrackable) { throw new TypeError("maximumSupply implies totalSupplyTrackable"); } serialized.maximumSupplyMajor = currency.maximumSupply.major; serialized.maximumSupplyMinor = currency.maximumSupply.minor; } if (currency.totalSupplyTrackable) { serialized.totalSupplyTrackable = true; } return new import_bencodex.RecordView(serialized, "text"); } __name(encodeCurrency, "encodeCurrency"); function encodeCurrencyForHash(currency) { const minters = currency.minters === null ? null : encodeAddressSet(currency.minters); const serialized = { ticker: currency.ticker, decimals: BigInt(currency.decimalPlaces), minters }; if (currency.maximumSupply !== null) { if (!currency.totalSupplyTrackable) { throw new TypeError("maximumSupply implies totalSupplyTrackable"); } serialized.maximumSupplyMajor = currency.maximumSupply.major; serialized.maximumSupplyMinor = currency.maximumSupply.minor; } if (currency.totalSupplyTrackable) { serialized.totalSupplyTrackable = true; } return new import_bencodex.RecordView(serialized, "text"); } __name(encodeCurrencyForHash, "encodeCurrencyForHash"); async function getCurrencyHash(currency) { const encoded = (0, import_bencodex.encode)(encodeCurrencyForHash(currency)); const buffer = await crypto.subtle.digest("SHA-1", encoded); return new Uint8Array(buffer); } __name(getCurrencyHash, "getCurrencyHash"); function encodeFungibleAssetValue(value) { return [encodeCurrency(value.currency), value.rawValue]; } __name(encodeFungibleAssetValue, "encodeFungibleAssetValue"); function abs(value) { return value < 0n ? -value : value; } __name(abs, "abs"); function getSign(value) { return value.rawValue < 0n ? -1 : value.rawValue > 0n ? 1 : 0; } __name(getSign, "getSign"); function getMajorUnit(value) { return abs(value.rawValue) / 10n ** BigInt(value.currency.decimalPlaces); } __name(getMajorUnit, "getMajorUnit"); function getMinorUnit(value) { return abs(value.rawValue) % 10n ** BigInt(value.currency.decimalPlaces); } __name(getMinorUnit, "getMinorUnit"); // src/tx/metadata.ts var import_bencodex2 = require("@planetarium/bencodex"); // src/blockhash.ts function encodeBlockHash(blockHash) { if (blockHash.length !== 32) { throw new TypeError( `BlockHash must be 32 bytes long, but got ${blockHash.length} bytes.` ); } return blockHash; } __name(encodeBlockHash, "encodeBlockHash"); // src/key.ts function encodePublicKey(publicKey) { if (publicKey.length < 1) { throw new TypeError("Public key must not be empty."); } return publicKey; } __name(encodePublicKey, "encodePublicKey"); // src/tx/metadata.ts var NONCE_KEY = new Uint8Array([110]); var SIGNER_KEY = new Uint8Array([115]); var GENESIS_HASH_KEY = new Uint8Array([103]); var UPDATED_ADDRESSES_KEY = new Uint8Array([117]); var PUBLIC_KEY_KEY = new Uint8Array([112]); var TIMESTAMP_KEY = new Uint8Array([116]); var GAS_LIMIT_KEY = new Uint8Array([108]); var MAX_GAS_PRICE_KEY = new Uint8Array([109]); function encodeTxMetadata(metadata) { const updatedAddresses = encodeAddressSet(metadata.updatedAddresses); const timestamp = metadata.timestamp.toISOString().replace(/Z$/, "000Z"); const pairs = [ [NONCE_KEY, metadata.nonce], [SIGNER_KEY, encodeAddress(metadata.signer)], [UPDATED_ADDRESSES_KEY, updatedAddresses], [PUBLIC_KEY_KEY, encodePublicKey(metadata.publicKey)], [TIMESTAMP_KEY, timestamp] ]; if (metadata.genesisHash !== null) { pairs.push([GENESIS_HASH_KEY, encodeBlockHash(metadata.genesisHash)]); } if (metadata.gasLimit !== null) { pairs.push([GAS_LIMIT_KEY, metadata.gasLimit]); } if (metadata.maxGasPrice !== null) { pairs.push([MAX_GAS_PRICE_KEY, encodeFungibleAssetValue(metadata.maxGasPrice)]); } return new import_bencodex2.BencodexDictionary(pairs); } __name(encodeTxMetadata, "encodeTxMetadata"); // src/tx/unsigned.ts var import_bencodex3 = require("@planetarium/bencodex"); var ACTION_KEY = new Uint8Array([97]); function encodeUnsignedTx(metadata) { return new import_bencodex3.BencodexDictionary([ ...encodeTxMetadata(metadata), [ACTION_KEY, metadata.actions] ]); } __name(encodeUnsignedTx, "encodeUnsignedTx"); // src/tx/signed.ts var import_account = require("@planetarium/account"); var import_bencodex4 = require("@planetarium/bencodex"); // src/bytes.ts function bytesEqual(a, b) { const x = a instanceof ArrayBuffer ? new Uint8Array(a) : a; const y = b instanceof ArrayBuffer ? new Uint8Array(b) : b; return x.length === y.length && x.every((v, i) => v === y[i]); } __name(bytesEqual, "bytesEqual"); // src/tx/signed.ts var SIGNATURE_KEY = new Uint8Array([83]); async function signTx(tx, signAccount) { if (!bytesEqual( tx.publicKey, (await signAccount.getPublicKey()).toBytes("uncompressed") )) { throw new Error("Public keys in the tx and the signAccount are mismatched"); } else if (!bytesEqual( tx.signer, import_account.Address.deriveFrom(await signAccount.getPublicKey()).toBytes() )) { throw new Error("The transaction signer does not match to the signAccount"); } const payload = encodeUnsignedTx(tx); const signature = await signAccount.sign((0, import_bencodex4.encode)(payload)); return { ...tx, signature }; } __name(signTx, "signTx"); function encodeSignedTx(tx) { const dict = encodeUnsignedTx(tx); const sig = tx.signature.toBytes(); return new import_bencodex4.BencodexDictionary([...dict, [SIGNATURE_KEY, sig]]); } __name(encodeSignedTx, "encodeSignedTx"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { encodeAddress, encodeCurrency, encodeFungibleAssetValue, encodeSignedTx, encodeTxMetadata, encodeUnsignedTx, getCurrencyHash, getMajorUnit, getMinorUnit, getSign, signTx }); /*! For license information please see index.cjs.LEGAL.txt */ //# sourceMappingURL=index.cjs.map