PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@stacks/transactions/dist
Просмотр файла: transaction.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeTransaction = exports.StacksTransaction = void 0;
const common_1 = require("@stacks/common");
const constants_1 = require("./constants");
const authorization_1 = require("./authorization");
const utils_1 = require("./utils");
const payload_1 = require("./payload");
const types_1 = require("./types");
const keys_1 = require("./keys");
const bufferReader_1 = require("./bufferReader");
const errors_1 = require("./errors");
class StacksTransaction {
constructor(version, auth, payload, postConditions, postConditionMode, anchorMode, chainId) {
this.version = version;
this.auth = auth;
if ('amount' in payload) {
this.payload = Object.assign(Object.assign({}, payload), { amount: common_1.intToBigInt(payload.amount, false) });
}
else {
this.payload = payload;
}
this.chainId = chainId !== null && chainId !== void 0 ? chainId : constants_1.DEFAULT_CHAIN_ID;
this.postConditionMode = postConditionMode !== null && postConditionMode !== void 0 ? postConditionMode : constants_1.PostConditionMode.Deny;
this.postConditions = postConditions !== null && postConditions !== void 0 ? postConditions : types_1.createLPList([]);
if (anchorMode) {
this.anchorMode = anchorMode;
}
else {
switch (payload.payloadType) {
case constants_1.PayloadType.Coinbase:
case constants_1.PayloadType.PoisonMicroblock: {
this.anchorMode = constants_1.AnchorMode.OnChainOnly;
break;
}
case constants_1.PayloadType.ContractCall:
case constants_1.PayloadType.SmartContract:
case constants_1.PayloadType.TokenTransfer: {
this.anchorMode = constants_1.AnchorMode.Any;
break;
}
}
}
}
signBegin() {
const tx = utils_1.cloneDeep(this);
tx.auth = tx.auth.intoInitialSighashAuth();
return tx.txid();
}
verifyBegin() {
const tx = utils_1.cloneDeep(this);
tx.auth = tx.auth.intoInitialSighashAuth();
return tx.txid();
}
createTxWithSignature(signature) {
const parsedSig = typeof signature === 'string' ? signature : signature.toString('hex');
const tx = utils_1.cloneDeep(this);
if (!tx.auth.spendingCondition) {
throw new Error('Cannot set signature on transaction without spending condition');
}
tx.auth.spendingCondition.signature =
authorization_1.createMessageSignature(parsedSig);
return tx;
}
verifyOrigin() {
return this.auth.verifyOrigin(this.verifyBegin());
}
signNextOrigin(sigHash, privateKey) {
if (this.auth.spendingCondition === undefined) {
throw new Error('"auth.spendingCondition" is undefined');
}
if (this.auth.authType === undefined) {
throw new Error('"auth.authType" is undefined');
}
return this.signAndAppend(this.auth.spendingCondition, sigHash, constants_1.AuthType.Standard, privateKey);
}
signNextSponsor(sigHash, privateKey) {
if (this.auth.sponsorSpendingCondition === undefined) {
throw new Error('"auth.spendingCondition" is undefined');
}
if (this.auth.authType === undefined) {
throw new Error('"auth.authType" is undefined');
}
return this.signAndAppend(this.auth.sponsorSpendingCondition, sigHash, constants_1.AuthType.Sponsored, privateKey);
}
appendPubkey(publicKey) {
const cond = this.auth.spendingCondition;
if (cond && !authorization_1.isSingleSig(cond)) {
const compressed = keys_1.isCompressed(publicKey);
cond.fields.push(authorization_1.createTransactionAuthField(compressed ? constants_1.PubKeyEncoding.Compressed : constants_1.PubKeyEncoding.Uncompressed, publicKey));
}
else {
throw new Error(`Can't append public key to a singlesig condition`);
}
}
signAndAppend(condition, curSigHash, authType, privateKey) {
const { nextSig, nextSigHash } = authorization_1.nextSignature(curSigHash, authType, condition.fee, condition.nonce, privateKey);
if (authorization_1.isSingleSig(condition)) {
condition.signature = nextSig;
}
else {
const compressed = privateKey.data.toString('hex').endsWith('01');
condition.fields.push(authorization_1.createTransactionAuthField(compressed ? constants_1.PubKeyEncoding.Compressed : constants_1.PubKeyEncoding.Uncompressed, nextSig));
}
return nextSigHash;
}
txid() {
const serialized = this.serialize();
return utils_1.txidFromData(serialized);
}
setSponsor(sponsorSpendingCondition) {
if (this.auth.authType != constants_1.AuthType.Sponsored) {
throw new errors_1.SigningError('Cannot sponsor sign a non-sponsored transaction');
}
this.auth.setSponsor(sponsorSpendingCondition);
}
setFee(amount) {
this.auth.setFee(amount);
}
setNonce(nonce) {
this.auth.setNonce(nonce);
}
setSponsorNonce(nonce) {
this.auth.setSponsorNonce(nonce);
}
serialize() {
if (this.version === undefined) {
throw new errors_1.SerializationError('"version" is undefined');
}
if (this.chainId === undefined) {
throw new errors_1.SerializationError('"chainId" is undefined');
}
if (this.auth === undefined) {
throw new errors_1.SerializationError('"auth" is undefined');
}
if (this.anchorMode === undefined) {
throw new errors_1.SerializationError('"anchorMode" is undefined');
}
if (this.payload === undefined) {
throw new errors_1.SerializationError('"payload" is undefined');
}
const bufferArray = new utils_1.BufferArray();
bufferArray.appendByte(this.version);
const chainIdBuffer = common_1.Buffer.alloc(4);
chainIdBuffer.writeUInt32BE(this.chainId, 0);
bufferArray.push(chainIdBuffer);
bufferArray.push(this.auth.serialize());
bufferArray.appendByte(this.anchorMode);
bufferArray.appendByte(this.postConditionMode);
bufferArray.push(types_1.serializeLPList(this.postConditions));
bufferArray.push(payload_1.serializePayload(this.payload));
return bufferArray.concatBuffer();
}
}
exports.StacksTransaction = StacksTransaction;
function deserializeTransaction(data) {
let bufferReader;
if (typeof data === 'string') {
if (data.slice(0, 2).toLowerCase() === '0x') {
bufferReader = new bufferReader_1.BufferReader(common_1.Buffer.from(data.slice(2), 'hex'));
}
else {
bufferReader = new bufferReader_1.BufferReader(common_1.Buffer.from(data, 'hex'));
}
}
else if (common_1.Buffer.isBuffer(data)) {
bufferReader = new bufferReader_1.BufferReader(data);
}
else {
bufferReader = data;
}
const version = bufferReader.readUInt8Enum(constants_1.TransactionVersion, n => {
throw new Error(`Could not parse ${n} as TransactionVersion`);
});
const chainId = bufferReader.readUInt32BE();
const auth = authorization_1.Authorization.deserialize(bufferReader);
const anchorMode = bufferReader.readUInt8Enum(constants_1.AnchorMode, n => {
throw new Error(`Could not parse ${n} as AnchorMode`);
});
const postConditionMode = bufferReader.readUInt8Enum(constants_1.PostConditionMode, n => {
throw new Error(`Could not parse ${n} as PostConditionMode`);
});
const postConditions = types_1.deserializeLPList(bufferReader, constants_1.StacksMessageType.PostCondition);
const payload = payload_1.deserializePayload(bufferReader);
return new StacksTransaction(version, auth, payload, postConditions, postConditionMode, anchorMode, chainId);
}
exports.deserializeTransaction = deserializeTransaction;
//# sourceMappingURL=transaction.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!