@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
131 lines • 5.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transaction = void 0;
const constants_1 = require("../../common/constants.js");
const address_1 = require("../../common/types/address.js");
const crypto_1 = require("../../crypto/crypto.js");
const bignumber_1 = require("@ethersproject/bignumber");
const bytes_1 = require("@ethersproject/bytes");
const rlp_1 = require("@ethersproject/rlp");
const neverthrow_1 = require("neverthrow");
class Transaction {
constructor(number, type, parentHash, hub, daemonHash, codeHash, owner, linker, amount, joule, difficulty, pow, proofOfWork, payload, timestamp, code, sign) {
this.number = number;
this.type = type;
this.parentHash = parentHash;
this.hub = hub;
this.daemonHash = daemonHash;
this.codeHash = codeHash;
this.owner = owner;
this.linker = linker;
this.amount = amount;
this.joule = joule;
this.difficulty = difficulty;
this.pow = pow;
this.proofOfWork = proofOfWork;
this.payload = payload;
this.timestamp = timestamp;
this.code = code;
this.sign = sign;
}
getTypeCode() {
return constants_1.TransactionTypeCodeRecord[this.type];
}
static default() {
return new Transaction(0, constants_1.TransactionTypes.Send, "", [], "", constants_1.ZERO_HASH, // codeHash
constants_1.ZERO_ADDRESS, // owner
constants_1.ZERO_ADDRESS, // linker
0, 0, 0, 0, "0x", "", Date.now(), "", "");
}
/**
* Validate the transaction
* @returns The error or null
*/
validate() {
if (this.number === undefined) {
return (0, neverthrow_1.err)(new Error("number is undefined"));
}
if (this.type === undefined) {
return (0, neverthrow_1.err)(new Error("type is undefined"));
}
return (0, neverthrow_1.ok)(null);
}
/**
* Sign the transaction
* @param chainId - The chain id of the transaction
* @param curve - The curve of the transaction
* @param privateKey - The private key of the transaction
* @returns The transaction signature
*/
signTx(chainId, curve, privateKey) {
const hash = this.rlpEncodeHash(chainId, curve);
return this.signHash(hash, curve, privateKey);
}
/**
* Sign the hash of the transaction
* @param hash - The hash of the transaction
* @param curve - The curve of the transaction
* @param privateKey - The private key of the transaction
* @returns The transaction signature
*/
signHash(hash, curve, privateKey) {
const result = this.doSign(curve, hash, privateKey);
if (result.isOk()) {
this.sign = `0x${result.value.toString("hex")}`;
return (0, neverthrow_1.ok)(this.sign);
}
return (0, neverthrow_1.err)(result.error);
}
/**
* Sign the hash of the transaction
* @param curve - The curve of the transaction
* @param hash - The hash of the transaction
* @param privateKey - The private key of the transaction
* @returns The transaction signature(buffer)
*/
doSign(curve, hash, privateKey) {
try {
const cryptoService = (0, crypto_1.createCrypto)(curve);
let _privateKey = privateKey;
if (_privateKey.startsWith("0x")) {
_privateKey = _privateKey.slice(2);
}
const signature = cryptoService.sign(hash, _privateKey);
return (0, neverthrow_1.ok)(Buffer.from(signature, "hex"));
}
catch (error) {
return (0, neverthrow_1.err)(error);
}
}
handleNumber(value) {
return (0, bytes_1.stripZeros)((0, bytes_1.arrayify)(bignumber_1.BigNumber.from(value)));
}
rlpEncodeHash(chainId, curve) {
const cryptoService = (0, crypto_1.createCrypto)(curve);
const encoded = cryptoService.encodeHash(() => {
const encoded = (0, rlp_1.encode)([
this.handleNumber(this.number ?? 0),
(0, bytes_1.hexlify)(this.getTypeCode()),
this.parentHash,
this.hub,
this.daemonHash,
this.codeHash || constants_1.ZERO_HASH,
new address_1.Address(this.owner).toETH(),
new address_1.Address(this.linker).toETH(),
this.handleNumber(this.amount ?? 0),
this.handleNumber(this.joule ?? 0),
this.handleNumber(0), // difficulty
this.handleNumber(0), // proofOfWork
this.payload,
this.handleNumber(this.timestamp ?? 0),
(0, bytes_1.arrayify)(chainId),
"0x",
"0x"
]);
return Buffer.from(encoded.substring(2), "hex");
});
return encoded;
}
}
exports.Transaction = Transaction;
//# sourceMappingURL=transaction.js.map