ton3-core
Version:
TON low-level API tools
108 lines • 3.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageExternalIn = exports.MessageInternal = void 0;
const tweetnacl_1 = __importDefault(require("tweetnacl"));
const coins_1 = require("../coins");
const address_1 = require("../address");
const helpers_1 = require("../utils/helpers");
const boc_1 = require("../boc");
class Message {
constructor(header, body = null, state = null) {
this.header = header;
this.body = body;
this.state = state;
}
parse(key) {
const message = new boc_1.Builder();
const body = this.body !== null && key !== undefined
? Message.signed(this.body, key)
: this.body;
message.storeSlice(this.header.slice());
if (this.state !== null) {
message.storeBit(1);
if (message.remainder >= (this.state.bits.length + 1)
&& (message.refs.length + this.state.refs.length) <= 4) {
message.storeBit(0)
.storeSlice(this.state.slice());
}
else {
message.storeBit(1)
.storeRef(this.state);
}
}
else {
message.storeBit(0);
}
if (body) {
if (message.remainder >= body.bits.length
&& message.refs.length + body.refs.length <= 4) {
message.storeBit(0)
.storeSlice(body.slice());
}
else {
message.storeBit(1)
.storeRef(body);
}
}
else {
message.storeBit(0);
}
return message.cell();
}
static signed(data, key) {
const hash = (0, helpers_1.hexToBytes)(data.hash());
const signature = tweetnacl_1.default.sign.detached(hash, key);
return new boc_1.Builder()
.storeBytes(signature)
.storeSlice(data.slice())
.cell();
}
sign(key) {
return this.parse(key);
}
cell() {
return this.parse();
}
}
class MessageInternal extends Message {
constructor(options, data) {
const builder = new boc_1.Builder();
const { ihrDisabled = true, bounce, bounced = false, src, dest, value, ihrFee = new coins_1.Coins(0), fwdFee = new coins_1.Coins(0), createdLt = 0, createdAt = 0 } = options;
const { body = null, state = null } = data;
const header = builder
.storeBit(0)
.storeInt(ihrDisabled ? -1 : 0, 1)
.storeInt(bounce ? -1 : 0, 1)
.storeInt(bounced ? -1 : 0, 1)
.storeAddress(src)
.storeAddress(dest)
.storeCoins(value)
.storeBit(0)
.storeCoins(ihrFee)
.storeCoins(fwdFee)
.storeUint(createdLt, 64)
.storeUint(createdAt, 32)
.cell();
super(header, body, state);
}
}
exports.MessageInternal = MessageInternal;
class MessageExternalIn extends Message {
constructor(options, data) {
const builder = new boc_1.Builder();
const { src = address_1.Address.NONE, dest = address_1.Address.NONE, importFee = new coins_1.Coins(0) } = options;
const { body = null, state = null } = data;
const header = builder
.storeBits([1, 0])
.storeAddress(src)
.storeAddress(dest)
.storeCoins(importFee)
.cell();
super(header, body, state);
}
}
exports.MessageExternalIn = MessageExternalIn;
//# sourceMappingURL=message.js.map