You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

170 lines
5.0 KiB

import { Sha256 } from "@aws-crypto/sha256-js";
import { beginCell, Cell, Address } from "ton";
import { BitString, Dictionary } from "ton-core";
import Prando from "prando";
import { TrackedMessage } from "ton-emulator/dist/events/message";
import { TrackedBody } from "ton-emulator/dist/events/message";
import { types } from '../output/types.json';
const ONCHAIN_CONTENT_PREFIX = 0x00;
const SNAKE_PREFIX = 0x00;
const CELL_MAX_SIZE_BYTES = Math.floor((1023 - 8) / 8);
function bufferToChunks(buff: Buffer, chunkSize: number) {
let chunks: Buffer[] = [];
while (buff.byteLength > 0) {
chunks.push(buff.slice(0, chunkSize));
buff = buff.slice(chunkSize);
}
return chunks;
}
export function makeSnakeCell(data: Buffer) {
let chunks = bufferToChunks(data, CELL_MAX_SIZE_BYTES);
const b = chunks.reduceRight((curCell, chunk, index) => {
if (index === 0) {
curCell.storeInt(SNAKE_PREFIX, 8);
}
curCell.storeBuffer(chunk);
if (index > 0) {
const cell = curCell.endCell();
return beginCell().storeRef(cell);
} else {
return curCell;
}
}, beginCell());
return b.endCell();
}
const sha256 = (str: string) => {
const sha = new Sha256();
sha.update(str);
return Buffer.from(sha.digestSync());
};
const toKey = (key: string) => {
return BigInt(`0x${sha256(key).toString("hex")}`);
};
export function buildOnchainMetadata(data: {
name: string;
description: string;
image: string;
}): Cell {
let dict = Dictionary.empty(
Dictionary.Keys.BigUint(256),
Dictionary.Values.Cell()
);
Object.entries(data).forEach(([key, value]) => {
dict.set(toKey(key), makeSnakeCell(Buffer.from(value, "utf8")));
});
return beginCell()
.storeInt(ONCHAIN_CONTENT_PREFIX, 8)
.storeDict(dict)
.endCell();
}
export function TON(): number {
return 1000000000;
}
export function randomAddress(seed: string, workchain?: number) {
const random = new Prando(seed);
const hash = Buffer.alloc(32);
for (let i = 0; i < hash.length; i++) {
hash[i] = random.nextInt(0, 255);
}
return new Address(workchain ?? 0, hash);
}
export function fmtMessage(message: TrackedMessage, addressBook: any) {
let from = (message as any).from;
let to = message.to;
let value_bi: BigInt = (message as any).value;
let value = 0;
if (value_bi) {
// convert to number
value = Number(value_bi) / TON();
}
let body_t: TrackedBody | undefined | null = (message as any).body;
if (body_t === undefined) {
body_t = null;
}
let body = "<boc>";
if (body_t?.type == 'cell') {
// let c = Cell.fromBoc(Buffer.from(body_t.cell, 'base64'))[0];
// body_t.cell is like x{<boc>} ...
// we need to get everything up to } and after {
let boc = body_t.cell.slice(2, body_t.cell.indexOf('}')).slice(0, 120);
let c = new Cell({bits: new BitString(Buffer.from(boc, 'base64'), 0, boc.length * 8)});
// let c = new Cell({bits: BitString(boc)});
let op = c.beginParse().loadUint(32);
let msg_type = 'unknown';
for (let t of types) {
if (t.header == op) {
msg_type = t.name;
break;
}
}
body = msg_type;
}
if (body.length % 2 == 1) {
body += ' ';
}
while (body.length < 30) {
body = ' ' + body + ' ';
}
if (addressBook[from]) {
from = addressBook[from];
// add spaces so that length is 11
while (from.length < 11) {
from += ' ';
}
} else {
// slice first 4 symbols, ..., and last 4 symbols
from = from.slice(0, 4) + '...' + from.slice(-4);
}
if (to && addressBook[to]) {
to = addressBook[to];
// add spaces so that length is 11
while (to?.length as number < 11) {
to += ' ';
}
} else {
// slice first 4 symbols, ..., and last 4 symbols
to = to?.slice(0, 4) + '...' + to?.slice(-4);
}
return `${from} --> ${to} ${value.toFixed(2)} ${body}`;
}
export function fmtEvent(ev: any, addressBook: any) {
if (ev.type == 'received') {
return fmtMessage(ev.message, addressBook);
}
if (ev.type == 'sent-bounced') {
return `Bounced ${fmtMessage(ev.message, addressBook)}`;
}
if (ev.type == 'sent') {
let res = '';
for (let m of ev.messages) {
res += fmtMessage(m, addressBook) + '\n';
}
return res.trim();
}
if (ev.type == 'failed') {
return `Failed ${ev.errorCode}`;
}
return '';
}
export function logEvents(events: any[], addressBook: any) {
let addressBookRev = Object.fromEntries(Object.entries(addressBook).map(([key, value]) => [value, key]));
let res = '';
// write events and their indices
for (let i = 0; i < events.length; i++) {
res += `#${i} ${fmtEvent(events[i], addressBookRev)}\n`;
}
console.log(res)
}