UNPKG

hapi-ton-sdk

Version:

SDK for managing HAPI attestations on TON network

149 lines (144 loc) 6.44 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/contracts/HapiAttestation.ts var HapiAttestation_exports = {}; __export(HapiAttestation_exports, { HapiTonAttestation: () => HapiTonAttestation }); module.exports = __toCommonJS(HapiAttestation_exports); var import_core = require("@ton/core"); // src/config.ts var config = { apiStaging: "https://hapi-one.stage.hapi.farm", apiProduction: "https://score-be.hapi.mobi", ton: { score: "kQC60vGFCtYeQi-S0p6Lhfghd0vYS1YcTiHDWhEmuQ39QpCh", nodeUrl: "https://tonapi.io" }, tonTestnet: { score: "kQC60vGFCtYeQi-S0p6Lhfghd0vYS1YcTiHDWhEmuQ39QpCh", nodeUrl: "https://testnet.tonapi.io" }, tonApiPath: (hash) => `/v2/blockchain/messages/${hash}/transaction` }; // src/utils/crc32.ts function crc32(str) { const table = new Int32Array(256); for (let i = 0; i < 256; i++) { let c = i; for (let j = 0; j < 8; j++) { c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1; } table[i] = c; } let crc = -1; for (let i = 0; i < str.length; i++) { crc = crc >>> 8 ^ table[(crc ^ str.charCodeAt(i)) & 255]; } return (crc ^ -1) >>> 0; } // src/types/index.ts var OpCode = { createAttestation: crc32("create_attestation"), updateAttestation: crc32("update_attestation") }; // src/contracts/HapiAttestation.ts var HapiTonAttestation = class _HapiTonAttestation { constructor(address, init) { this.address = address; this.init = init; } static createFromAddress(address = config.ton.score, contractAdapter) { return contractAdapter.open(new _HapiTonAttestation((0, import_core.address)(address))); } async getCreateAttestationFee(provider) { const result = await provider.get("get_create_attestation_fee", []); return result.stack.readBigNumber(); } async getUpdateAttestationFee(provider) { const result = await provider.get("get_update_attestation_fee", []); return result.stack.readBigNumber(); } async getAttestationData(provider) { const res = await provider.get("get_hapi_attestation_data", []); const userCount = res.stack.readBigNumber(); const contractOwner = res.stack.readAddress(); const commissionOwner = res.stack.readAddress(); const createAttestatioFee = res.stack.readBigNumber(); const updateAttestatioFee = res.stack.readBigNumber(); const walletCode = res.stack.readCell(); return { userCount, contractOwner, commissionOwner, createAttestatioFee, updateAttestatioFee, walletCode }; } static async getUserJettonAddress(provider, address) { const result = await provider.get("get_user_jetton_address", [ { type: "slice", cell: (0, import_core.beginCell)().storeAddress((0, import_core.address)(address)).endCell() } ]); return result.stack.readAddress(); } static getStaticUserJettonAddress(address) { const JETTON_WALLET_CODE = import_core.Cell.fromBoc( Buffer.from( "b5ee9c724102140100013b000114ff00f4a413f4bcf2c80b01020120021302014803070202cb040602dfd0ccc7434c0c05c6c2456f80871c02456f83e900c36cf1b088134c7c860842576e74e6ea497c1b81450b1c17cb87d208433e45309eea3ac40b4cfc0407481f4cffe803e900c208203d0901c3ec08076cf08d04d8572140173c584f2c1f2cfc073c5b3327b55007c057817c12103fcbc212050028c8801001cb0558cf1601fa027001cb6ac973fb000049a2e4400800e58280e78b387d013800e5b541086a993b6d80e58f8080e59fe4c080417d80400201480811020120090e0201480a0b0111ae56ed9e08122f8240120201200c0d0110a9d4db3c10345f0412010aa9bfdb3c30120201580f10000fad97fc13b7911840010dacd2ed9e2f824012010fb9996db3c145f04812001aed44d0fa40d207d33ffa40d430000cf230840ff2f0a35e372c", "hex" ) )[0]; const JETTON_MASTER_ADDRESS = import_core.Address.parse(config.ton.score); const USER_ADDRESS = import_core.Address.parse(address); const USER_ADDRESS_CELL = (0, import_core.beginCell)().storeAddress(USER_ADDRESS).endCell(); const JETTON_MASTER_ADDRESS_CELL = (0, import_core.beginCell)().storeAddress(JETTON_MASTER_ADDRESS).endCell(); const userJettonWalletData = (0, import_core.beginCell)().storeSlice(USER_ADDRESS_CELL.asSlice()).storeUint(0, 8).storeUint(0, 64).storeSlice(JETTON_MASTER_ADDRESS_CELL.asSlice()).storeRef(JETTON_WALLET_CODE).endCell(); const jettonWalletStateInit = (0, import_core.beginCell)().storeUint(0, 2).storeMaybeRef(JETTON_WALLET_CODE).storeMaybeRef(userJettonWalletData).storeUint(0, 1).endCell(); return new import_core.Address(0, jettonWalletStateInit.hash()); } prepareCreateAttestation(opts) { return { value: opts.value, sendMode: import_core.SendMode.PAY_GAS_SEPARATELY, body: (0, import_core.beginCell)().storeUint(OpCode.createAttestation, 32).storeUint(opts.queryId, 64).storeUint(opts.referralId ?? 0n, 64).storeUint(opts.trustScore, 8).storeUint(opts.expirationDate, 64).storeBuffer(opts.signature).endCell() }; } async sendCreateAttestation(provider, via, opts) { await provider.internal(via, this.prepareCreateAttestation(opts)); } prepareUpdateAttestation(opts) { return { value: opts.value, sendMode: import_core.SendMode.PAY_GAS_SEPARATELY, body: (0, import_core.beginCell)().storeUint(OpCode.updateAttestation, 32).storeUint(opts.queryId, 64).storeUint(opts.referralId ?? 0n, 64).storeUint(opts.trustScore, 8).storeUint(opts.expirationDate, 64).storeBuffer(opts.signature).endCell() }; } async sendUpdateAttestation(provider, via, opts) { await provider.internal(via, this.prepareUpdateAttestation(opts)); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { HapiTonAttestation }); //# sourceMappingURL=HapiAttestation.js.map