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.

62 lines
1.6 KiB

import { Sha256 } from "@aws-crypto/sha256-js";
import { beginCell, Cell } from "ton";
import { Dictionary } from "ton-core";
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;
}
const CELL_MAX_SIZE_BYTES = Math.floor((1023 - 8) / 8);
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();
}
2 years ago
2 years ago
const ONCHAIN_CONTENT_PREFIX = 0x00;
const SNAKE_PREFIX = 0x00;
2 years ago
2 years ago
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")}`);
};
2 years ago
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")));
2 years ago
});
return beginCell()
.storeInt(ONCHAIN_CONTENT_PREFIX, 8)
.storeDict(dict)
.endCell();
2 years ago
}