UNPKG

@tonkite/highload-wallet-v3

Version:

The wrapper for highload-wallet-v3.

110 lines 4.83 kB
"use strict"; /** * Copyright 2024 Scaleton Labs LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.HighloadWalletV3 = void 0; exports.highloadWalletV3ConfigToCell = highloadWalletV3ConfigToCell; const core_1 = require("@ton/core"); const crypto_1 = require("@ton/crypto"); const highload_wallet_v3_query_id_sequence_1 = require("./highload-wallet-v3-query-id-sequence"); const highload_wallet_v3_code_1 = require("./highload-wallet-v3-code"); const internal_message_1 = require("./messages/internal-message"); const highload_wallet_v3_reader_1 = require("./highload-wallet-v3-reader"); const TIMESTAMP_SIZE = 64; const TIMEOUT_SIZE = 22; function highloadWalletV3ConfigToCell({ publicKey, subwalletId, timeout, }) { return (0, core_1.beginCell)() .storeBuffer(publicKey) .storeUint(subwalletId, 32) .storeDict(null) // old queries .storeDict(null) // queries .storeUint(0, TIMESTAMP_SIZE) .storeUint(timeout, TIMEOUT_SIZE) .endCell(); } class HighloadWalletV3 extends highload_wallet_v3_reader_1.HighloadWalletV3Reader { sequence; publicKey; timeout; subwalletId; static DEFAULT_SUBWALLET_ID = 0x10ad; static DEFAULT_TIMEOUT = 60 * 60 * 24; // 24 hours init; constructor(sequence, publicKey, timeout = HighloadWalletV3.DEFAULT_TIMEOUT, subwalletId = HighloadWalletV3.DEFAULT_SUBWALLET_ID, workchain = 0, code) { const init = { code: code ?? core_1.Cell.fromBoc(Buffer.from(highload_wallet_v3_code_1.HIGHLOAD_WALLET_V3_CODE_HEX, 'hex'))[0], data: highloadWalletV3ConfigToCell({ publicKey, timeout, subwalletId, }), }; super((0, core_1.contractAddress)(workchain, init)); this.sequence = sequence; this.publicKey = publicKey; this.timeout = timeout; this.subwalletId = subwalletId; this.init = init; } static newSequence() { return new highload_wallet_v3_query_id_sequence_1.HighloadWalletV3QueryIdSequence(0, 0); } static restoreSequence(lastQueryId) { return highload_wallet_v3_query_id_sequence_1.HighloadWalletV3QueryIdSequence.restore(lastQueryId); } static emergencySequence() { return highload_wallet_v3_query_id_sequence_1.HighloadWalletV3QueryIdSequence.emergency(); } async sendBatch(provider, secretKey, { messages, createdAt, valuePerBatch, }) { const actions = messages.map(({ message, mode }) => ({ type: 'sendMsg', mode, outMsg: message, })); const queryId = this.sequence.current(); return this.sendExternal(provider, secretKey, { message: (0, core_1.internal)({ to: this.address, value: valuePerBatch ?? 0n, body: new internal_message_1.InternalMessage(queryId, actions).toCell(), }), mode: valuePerBatch && valuePerBatch > 0n ? core_1.SendMode.PAY_GAS_SEPARATELY : core_1.SendMode.CARRY_ALL_REMAINING_BALANCE, queryId, createdAt, }); } async sendExternal(provider, secretKey, { message, mode, queryId, createdAt, }) { const signingMessage = (0, core_1.beginCell)() .storeUint(this.subwalletId, 32) .storeRef((0, core_1.beginCell)().store((0, core_1.storeMessageRelaxed)(message)).endCell()) .storeUint(mode, 8) .storeUint(queryId, highload_wallet_v3_query_id_sequence_1.HighloadWalletV3QueryIdSequence.SHIFT_SIZE + highload_wallet_v3_query_id_sequence_1.HighloadWalletV3QueryIdSequence.BIT_NUMBER_SIZE) .storeUint(createdAt ?? Math.floor(Date.now() / 1000), TIMESTAMP_SIZE) .storeUint(this.timeout, TIMEOUT_SIZE) .endCell(); const externalMessage = (0, core_1.beginCell)() .storeBuffer((0, crypto_1.sign)(signingMessage.hash(), secretKey)) .storeRef(signingMessage) .endCell(); await provider.external(externalMessage); return externalMessage; } } exports.HighloadWalletV3 = HighloadWalletV3; //# sourceMappingURL=highload-wallet-v3.js.map