Skip to content

Commit

Permalink
0.0.60 - allow up to 4 transfers at once; transfer.seqno check - it m…
Browse files Browse the repository at this point in the history
…ust be number >= 0;
  • Loading branch information
tolya-yanot committed Feb 16, 2023
1 parent 9761af5 commit 71065b1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist/tonweb.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tonweb",
"version": "0.0.59",
"version": "0.0.60",
"description": "TonWeb - JavaScript API for TON blockchain",
"main": "src/index.js",
"types": "dist/types/index.d.ts",
Expand Down
29 changes: 28 additions & 1 deletion src/contract/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {Cell} = require("../boc");
const {Address, bytesToBase64, bytesToHex} = require("../utils");
const {Address, bytesToBase64, bytesToHex, BN} = require("../utils");

class Contract {
/**
Expand Down Expand Up @@ -171,6 +171,33 @@ class Contract {
return message;
}

/**
* @param address {Address | string}
* @param amount {BN} in nanotons
* @param payload {string | Uint8Array | Cell}
* @param stateInit? {Cell}
* @return {Cell}
*/
static createOutMsg(address, amount, payload, stateInit = null) {
let payloadCell = new Cell();
if (payload) {
if (payload.refs) { // is Cell
payloadCell = payload;
} else if (typeof payload === 'string') {
if (payload.length > 0) {
payloadCell.bits.writeUint(0, 32);
payloadCell.bits.writeString(payload);
}
} else {
payloadCell.bits.writeBytes(payload)
}
}

const orderHeader = Contract.createInternalMessageHeader(new Address(address), new BN(amount));
const order = Contract.createCommonMsgInfo(orderHeader, stateInit, payloadCell);
return order;
}

//tblkch.pdf, page 57
/**
* Create CommonMsgInfo contains header, stateInit, body
Expand Down
64 changes: 46 additions & 18 deletions src/contract/wallet/WalletContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ class WalletContract extends Contract {
*/
transfer: (params) => Contract.createMethod(provider, this.createTransferMessage(params.secretKey, params.toAddress, params.amount, params.seqno, params.payload, params.sendMode, !Boolean(params.secretKey), params.stateInit, params.expireAt)),

/**
* @param params {{secretKey: Uint8Array, seqno: number, expireAt?: number, messages: [{address: Address | string, amount: BN, payload?: string | Uint8Array | Cell, sendMode?: number, stateInit?: Cell }]}}
*/
transfers: (params) => Contract.createMethod(provider, this.createTransferMessages(params.secretKey, params.seqno, params.messages, !Boolean(params.secretKey), params.expireAt)),

seqno: () => {
return {
/**
* @return {Promise<number>}
* @return {Promise<number|null>}
*/
call: async () => {
const address = await this.getAddress();
Expand Down Expand Up @@ -161,7 +166,7 @@ class WalletContract extends Contract {
/**
* @param secretKey {Uint8Array} nacl.KeyPair.secretKey
* @param address {Address | string}
* @param amount {BN | number} in nanograms
* @param amount {BN | number} in nanotons
* @param seqno {number}
* @param payload? {string | Uint8Array | Cell}
* @param sendMode? {number}
Expand All @@ -181,28 +186,51 @@ class WalletContract extends Contract {
stateInit = null,
expireAt = undefined
) {
let payloadCell = new Cell();
if (payload) {
if (payload.refs) { // is Cell
payloadCell = payload;
} else if (typeof payload === 'string') {
if (payload.length > 0) {
payloadCell.bits.writeUint(0, 32);
payloadCell.bits.writeString(payload);
}
} else {
payloadCell.bits.writeBytes(payload)
}
if (seqno === null || seqno === undefined || seqno < 0) {
throw new Error('seqno must be number >= 0')
}

const orderHeader = Contract.createInternalMessageHeader(new Address(address), new BN(amount));
const order = Contract.createCommonMsgInfo(orderHeader, stateInit, payloadCell);
const signingMessage = this.createSigningMessage(seqno, expireAt);
if (sendMode === null || sendMode === undefined) {
sendMode = 3;
}
signingMessage.bits.writeUint8(sendMode);
signingMessage.refs.push(order);
signingMessage.refs.push(Contract.createOutMsg(address, amount, payload, stateInit));

return this.createExternalMessage(signingMessage, secretKey, seqno, dummySignature);
}

/**
* @param secretKey {Uint8Array} nacl.KeyPair.secretKey
* @param seqno {number}
* @param messages {[{toAddress: Address | string, amount: BN, payload?: string | Uint8Array | Cell, sendMode?: number, stateInit?: Cell }]} up to 4 messages
* @param dummySignature? {boolean}
* @param expireAt? {number}
* @return {Promise<{address: Address, signature: Uint8Array, message: Cell, cell: Cell, body: Cell, resultMessage: Cell}>}
*/
async createTransferMessages(
secretKey,
seqno,
messages,
dummySignature = false,
expireAt = undefined,
) {
if (seqno === null || seqno === undefined || seqno < 0) {
throw new Error('seqno must be number >= 0')
}
const signingMessage = this.createSigningMessage(seqno, expireAt);
if (messages.length < 1 || messages.length > 4) {
throw new Error('put 1-4 messages');
}
for (let msg of messages) {
let sendMode = msg.sendMode;
if (sendMode === null || sendMode === undefined) {
sendMode = 3;
}
signingMessage.bits.writeUint8(sendMode);
signingMessage.refs.push(Contract.createOutMsg(msg.toAddress, msg.amount, msg.payload, msg.stateInit));
}
return this.createExternalMessage(signingMessage, secretKey, seqno, dummySignature);
}
}

module.exports = {WalletContract};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TransportWebUSB = require("@ledgerhq/hw-transport-webusb").default;
const TransportWebHID = require("@ledgerhq/hw-transport-webhid").default;
const BluetoothTransport = require("@ledgerhq/hw-transport-web-ble").default;
const {Dns, DnsCollection, DnsItem} = require("./contract/dns").default;
const version = '0.0.59';
const version = '0.0.60';

class TonWeb {
constructor(provider) {
Expand Down

1 comment on commit 71065b1

@adiko02
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.