-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcontract-verifier.ts
More file actions
188 lines (163 loc) · 4.92 KB
/
contract-verifier.ts
File metadata and controls
188 lines (163 loc) · 4.92 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { TonClient4, Address, TupleReader, TupleBuilder } from "ton";
import { getHttpV4Endpoint } from "@orbs-network/ton-access";
import { Sha256 } from "@aws-crypto/sha256-js";
interface GetSourcesOptions {
verifier?: string;
httpApiEndpointV4?: string;
testnet?: boolean;
}
export declare type FuncCompilerVersion = string;
export declare type TactVersion = string;
export declare type FiftVersion = FuncCompilerVersion; // Fift is tied to a FunC version
export declare type TolkVersion = string;
export declare type FuncCompilerSettings = {
funcVersion: FuncCompilerVersion;
commandLine: string;
};
export type FiftCliCompileSettings = {
fiftVersion: FiftVersion;
commandLine: string;
};
export type TactCliCompileSettings = {
tactVersion: TactVersion;
};
export type TolkCliCompileSettings = {
tolkVersion: TolkVersion;
};
export type FuncSource = {
name: string;
content: string;
isEntrypoint: boolean;
};
export type TolkSource = {
name: string;
content: string;
isEntrypoint: boolean;
}
export type TactSource = {
name: string;
content: string;
};
export interface SourcesData {
files: (TactSource | FuncSource | TolkSource)[];
compiler: "func" | "tact" | "fift" | "tolk";
compilerSettings:
| FuncCompilerSettings
| FiftCliCompileSettings
| TolkCliCompileSettings
| TactCliCompileSettings;
verificationDate: Date;
ipfsHttpLink: string;
}
type IpfsUrlConverterFunc = (ipfsUrl: string, testnet: boolean) => string;
const SOURCES_REGISTRY = Address.parse(
"EQD-BJSVUJviud_Qv7Ymfd3qzXdrmV525e3YDzWQoHIAiInL",
);
const SOURCES_REGISTRY_TESTNET = Address.parse(
"EQCsdKYwUaXkgJkz2l0ol6qT_WxeRbE_wBCwnEybmR0u5TO8",
);
function toSha256Buffer(s: string) {
const sha = new Sha256();
sha.update(s);
return Buffer.from(sha.digestSync());
}
function defaultIpfsConverter(ipfs: string, testnet: boolean) {
let endpoint: string;
if (testnet) {
endpoint = "https://tonsource-testnet.infura-ipfs.io/ipfs/";
} else {
endpoint = "https://ipfs.toncenter.com/ipfs/";
}
return ipfs.replace("ipfs://", endpoint);
}
function bigIntFromBuffer(buffer: Buffer) {
return BigInt(`0x${buffer.toString("hex")}`);
}
export const ContractVerifier = {
async getSourcesJsonUrl(
codeCellHash: string,
options?: GetSourcesOptions,
): Promise<string | null> {
const tc = new TonClient4({
endpoint:
options?.httpApiEndpointV4 ??
(await getHttpV4Endpoint({
network: options.testnet ? "testnet" : "mainnet",
})),
});
const {
last: { seqno },
} = await tc.getLastBlock();
const args = new TupleBuilder();
args.writeNumber(
bigIntFromBuffer(toSha256Buffer(options?.verifier ?? "orbs.com")),
);
args.writeNumber(bigIntFromBuffer(Buffer.from(codeCellHash, "base64")));
const { result: itemAddRes } = await tc.runMethod(
seqno,
options.testnet ? SOURCES_REGISTRY_TESTNET : SOURCES_REGISTRY,
"get_source_item_address",
args.build(),
);
let reader = new TupleReader(itemAddRes);
const sourceItemAddr = reader.readAddress();
const isDeployed = await tc.isContractDeployed(seqno, sourceItemAddr);
if (isDeployed) {
const { result: sourceItemDataRes } = await tc.runMethod(
seqno,
sourceItemAddr,
"get_source_item_data",
);
reader = new TupleReader(sourceItemDataRes);
const contentCell = reader.skip(3).readCell().beginParse();
const version = contentCell.loadUint(8);
if (version !== 1) throw new Error("Unsupported version");
const ipfsLink = contentCell.loadStringTail();
return ipfsLink;
}
return null;
},
async getSourcesData(
sourcesJsonUrl: string,
options?: {
ipfsConverter?: IpfsUrlConverterFunc;
testnet?: boolean;
},
): Promise<SourcesData> {
const ipfsConverter = options.ipfsConverter ?? defaultIpfsConverter;
const ipfsHttpLink = ipfsConverter(sourcesJsonUrl, !!options.testnet);
const verifiedContract = await (
await fetch(ipfsConverter(sourcesJsonUrl, !!options.testnet))
).json();
const files = (
await Promise.all(
verifiedContract.sources.map(
async (source: {
url: string;
filename: string;
isEntrypoint?: boolean;
}) => {
const url = ipfsConverter(source.url, !!options.testnet);
const content = await fetch(url).then((u) => u.text());
return {
name: source.filename,
content,
isEntrypoint: source.isEntrypoint,
};
},
),
)
)
.reverse()
.sort((a, b) => {
return Number(b.isEntrypoint) - Number(a.isEntrypoint);
});
return {
files,
verificationDate: new Date(verifiedContract.verificationDate),
compilerSettings: verifiedContract.compilerSettings,
compiler: verifiedContract.compiler,
ipfsHttpLink,
};
},
};