@ton.js/core
Version:
TonWeb - JavaScript API for TON blockchain
142 lines • 5.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppTon = void 0;
const cell_1 = require("../boc/cell");
const contract_1 = require("../contract/contract");
const address_1 = require("../utils/address");
const common_1 = require("../utils/common");
class AppTon {
constructor(
/**
* @ledgerhq compatible transport.
*/
transport, ton) {
// @todo: find out why decorateAppAPIMethods is called
// const scrambleKey = "w0w";
// transport.decorateAppAPIMethods(
// this,
// [
// "getAppConfiguration",
// "getAddress",
// "sign",
// "signTransfer",
// ],
// scrambleKey
// );
this.transport = transport;
this.ton = ton;
// @todo: use enum for this
// @todo: these should be static
this.ADDRESS_FORMAT_HEX = 0;
this.ADDRESS_FORMAT_USER_FRIENDLY = 1;
this.ADDRESS_FORMAT_URL_SAFE = 2;
this.ADDRESS_FORMAT_BOUNCEABLE = 4;
this.ADDRESS_FORMAT_TEST_ONLY = 8;
}
/***
* Returns application configuration that includes version.
*/
async getAppConfiguration() {
const [major, minor, patch] = (await this.transport.send(0xe0, 0x01, 0x00, 0x00));
return {
version: [major, minor, patch].join('.'),
};
}
/**
* Returns public key for the specified account number.
* If `isDisplay` is set then it displays the public key
* and confirms before returning.
*/
async getPublicKey(accountNumber, isDisplay) {
const buffer = Buffer.alloc(4);
buffer.writeInt32BE(accountNumber);
const response = await this.transport
.send(0xe0, 0x02, (isDisplay ? 0x01 : 0x00), 0x00, buffer);
const len = response[0];
const publicKey = new Uint8Array(response.slice(1, 1 + len));
return { publicKey };
}
/**
* Returns wallet v3R1 address for the specified account number.
* If `isDisplay` is set, then it displays address and
* confirms before returning. `addressFormat` is a sum
* of `ADDRESS_FORMAT_*` instance property constants.
*/
async getAddress(accountNumber, isDisplay, addressFormat) {
const buffer = Buffer.alloc(4);
buffer.writeInt32BE(accountNumber);
const response = await this.transport
.send(0xe0, 0x05, (isDisplay ? 0x01 : 0x00), addressFormat, buffer);
const len = response[0];
const addressHex = new Uint8Array(response.slice(1, 1 + len));
const address = new address_1.Address('0:' + (0, common_1.bytesToHex)(addressHex));
return { address };
}
/**
* Signs the specified buffer of bytes using the
* specified account number.
*/
async sign(accountNumber, buffer) {
const accountNumberBuffer = Buffer.alloc(4);
accountNumberBuffer.writeInt32BE(accountNumber);
const signBuffer = Buffer.concat([
accountNumberBuffer,
Buffer.from(buffer),
]);
const response = await this.transport
.send(0xe0, 0x03, 0x00, 0x00, signBuffer);
const len = response[0];
const signature = response.slice(1, 1 + len);
return { signature };
}
/**
* Signs the transfer coins message
* (same with TonWeb.WalletContract.createTransferMessage).
* If `seqno` is zero, then it will be "deploy wallet + transfer coins" message.
* `addressFormat` is a sum of `ADDRESS_FORMAT_*` instance property constants.
*/
async transfer(accountNumber, wallet, toAddress, nanograms, seqno, addressFormat) {
// @todo: don't use magic numbers
const sendMode = 3;
const query = await wallet.createTransferMessage(null, toAddress, nanograms, seqno, null, sendMode, true);
const accountNumberBuffer = Buffer.alloc(4);
accountNumberBuffer.writeInt32BE(accountNumber);
const msgBuffer = Buffer.concat([
accountNumberBuffer,
Buffer.from(await query.signingMessage.toBoc())
]);
const response = (await this.transport
.send(0xe0, 0x04, addressFormat, 0x00, msgBuffer));
const len = response[0];
const signatureBuffer = response.slice(1, 1 + len);
const signature = new Uint8Array(signatureBuffer);
const body = new cell_1.Cell();
body.bits.writeBytes(signature);
body.writeCell(query.signingMessage);
let stateInit = null, code = null, data = null;
if (seqno === 0) {
const deploy = await wallet.createStateInit();
stateInit = deploy.stateInit;
code = deploy.code;
data = deploy.data;
}
const selfAddress = await wallet.getAddress();
const header = contract_1.Contract.createExternalMessageHeader(selfAddress);
const resultMessage = contract_1.Contract.createCommonMsgInfo(header, stateInit, body);
const resultPromise = new Promise(resolve => {
resolve({
address: selfAddress,
message: resultMessage,
body,
signature,
signingMessage: query.signingMessage,
stateInit,
code,
data,
});
});
return contract_1.Contract.createMethod(this.ton.provider, resultPromise);
}
}
exports.AppTon = AppTon;
//# sourceMappingURL=app-ton.js.map