@planetarium/tx
Version:
Creating Libplanet transactions from JavaScript/TypeScript
225 lines (213 loc) • 7.09 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// 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
import { encode, RecordView } from "@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 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 RecordView(serialized, "text");
}
__name(encodeCurrencyForHash, "encodeCurrencyForHash");
async function getCurrencyHash(currency) {
const encoded = 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
import {
BencodexDictionary
} from "@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 BencodexDictionary(pairs);
}
__name(encodeTxMetadata, "encodeTxMetadata");
// src/tx/unsigned.ts
import { BencodexDictionary as BencodexDictionary2 } from "@planetarium/bencodex";
var ACTION_KEY = new Uint8Array([97]);
function encodeUnsignedTx(metadata) {
return new BencodexDictionary2([
...encodeTxMetadata(metadata),
[ACTION_KEY, metadata.actions]
]);
}
__name(encodeUnsignedTx, "encodeUnsignedTx");
// src/tx/signed.ts
import { Address as Address3 } from "@planetarium/account";
import { BencodexDictionary as BencodexDictionary3, encode as encode2 } from "@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,
Address3.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(encode2(payload));
return {
...tx,
signature
};
}
__name(signTx, "signTx");
function encodeSignedTx(tx) {
const dict = encodeUnsignedTx(tx);
const sig = tx.signature.toBytes();
return new BencodexDictionary3([...dict, [SIGNATURE_KEY, sig]]);
}
__name(encodeSignedTx, "encodeSignedTx");
export {
encodeAddress,
encodeCurrency,
encodeFungibleAssetValue,
encodeSignedTx,
encodeTxMetadata,
encodeUnsignedTx,
getCurrencyHash,
getMajorUnit,
getMinorUnit,
getSign,
signTx
};
/*! For license information please see index.js.LEGAL.txt */
//# sourceMappingURL=index.js.map