-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
67 lines (56 loc) · 2.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Address, Cell, TonClient } from "ton";
import BN from "bn.js";
import crypto from "crypto";
import { getHttpEndpoint } from "@orbs-network/ton-gateway";
(async () => {
const toSha256Buffer = (s: string) => {
return crypto.createHash("sha256").update(s).digest();
};
const SOURCES_REGISTRY = "EQANJEwItCel0Pwle7fHaL1FRYC2dZyyzKCOqK2yjrMcxN-g";
const VERIFIER_ID = "orbs.com";
const endpoint = await getHttpEndpoint(); // get the decentralized RPC endpoint
console.log(endpoint)
const tc = new TonClient({
endpoint,
});
async function getVerificationDataForContract(
codeCellHash: string
): Promise<string | null> {
const { stack: sourceItemAddressStack } = await tc.callGetMethod(
Address.parse(SOURCES_REGISTRY),
"get_source_item_address",
[
["num", new BN(toSha256Buffer(VERIFIER_ID)).toString()],
["num", new BN(Buffer.from(codeCellHash, "base64")).toString(10)],
]
);
const sourceItemAddr = Cell.fromBoc(
Buffer.from(sourceItemAddressStack[0][1].bytes, "base64")
)[0]
.beginParse()
.readAddress()!;
const isDeployed = await tc.isContractDeployed(sourceItemAddr);
console.log("isDeployed", sourceItemAddr.toFriendly(), isDeployed);
if (isDeployed) {
const { stack: sourceItemDataStack } = await tc.callGetMethod(
sourceItemAddr,
"get_source_item_data"
);
const ipfsLink = Cell.fromBoc(
Buffer.from(sourceItemDataStack[4][1].bytes, "base64")
)[0]
.beginParse()
.readRemainingBytes()
.toString();
console.log("IPFS Link", ipfsLink);
return ipfsLink;
}
return null;
}
const exampleCodeCellHash = "/rX/aCDi/w2Ug+fg1iyBfYRniftK5YDIeIZtlZ2r1cA=";
const nonExistentCodeCellHash = "/rX/aCDi/dddddd";
console.log("💎 Fetching IPFS link for an existing verified code cell hash");
await getVerificationDataForContract(exampleCodeCellHash);
console.log("\n💎 Fetching IPFS link for a nonexistent code cell hash");
await getVerificationDataForContract(nonExistentCodeCellHash);
})();