@thru/programs 0.3.4 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +124 -39
- package/dist/abi-manager/index.cjs +9568 -0
- package/dist/abi-manager/index.cjs.map +1 -0
- package/dist/abi-manager/index.d.cts +2169 -0
- package/dist/abi-manager/index.d.ts +2169 -0
- package/dist/abi-manager/index.js +4 -0
- package/dist/abi-manager/index.js.map +1 -0
- package/dist/amm/index.cjs +132 -33
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.d.cts +1 -1
- package/dist/amm/index.d.ts +1 -1
- package/dist/amm/index.js +5 -2
- package/dist/amm/index.js.map +1 -1
- package/dist/bootstrap-addresses/index.cjs +118 -0
- package/dist/bootstrap-addresses/index.cjs.map +1 -0
- package/dist/bootstrap-addresses/index.d.cts +44 -0
- package/dist/bootstrap-addresses/index.d.ts +44 -0
- package/dist/bootstrap-addresses/index.js +5 -0
- package/dist/bootstrap-addresses/index.js.map +1 -0
- package/dist/chunk-6IC6CO5S.js +9443 -0
- package/dist/chunk-6IC6CO5S.js.map +1 -0
- package/dist/chunk-7DHFYBRW.js +51 -0
- package/dist/chunk-7DHFYBRW.js.map +1 -0
- package/dist/chunk-7FBCBZIY.js +41 -0
- package/dist/chunk-7FBCBZIY.js.map +1 -0
- package/dist/chunk-BNYDH4ZP.js +4351 -0
- package/dist/chunk-BNYDH4ZP.js.map +1 -0
- package/dist/{chunk-J7B7XYOI.js → chunk-CYG4PB65.js} +14 -41
- package/dist/chunk-CYG4PB65.js.map +1 -0
- package/dist/chunk-VNTPD4AR.js +37 -0
- package/dist/chunk-VNTPD4AR.js.map +1 -0
- package/dist/chunk-YX7BLFQZ.js +81 -0
- package/dist/chunk-YX7BLFQZ.js.map +1 -0
- package/dist/chunk-ZHF2PH5H.js +6536 -0
- package/dist/chunk-ZHF2PH5H.js.map +1 -0
- package/dist/clob/index.cjs +8461 -4922
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +1115 -53
- package/dist/clob/index.d.ts +1115 -53
- package/dist/clob/index.js +6817 -3395
- package/dist/clob/index.js.map +1 -1
- package/dist/deploy/index.cjs +24751 -0
- package/dist/deploy/index.cjs.map +1 -0
- package/dist/deploy/index.d.cts +180 -0
- package/dist/deploy/index.d.ts +180 -0
- package/dist/deploy/index.js +1849 -0
- package/dist/deploy/index.js.map +1 -0
- package/dist/helpers-DD7q61nr.d.cts +7 -0
- package/dist/helpers-DD7q61nr.d.ts +7 -0
- package/dist/manager/index.cjs +6680 -0
- package/dist/manager/index.cjs.map +1 -0
- package/dist/manager/index.d.cts +1279 -0
- package/dist/manager/index.d.ts +1279 -0
- package/dist/manager/index.js +5 -0
- package/dist/manager/index.js.map +1 -0
- package/dist/multicall/index.cjs +117 -40
- package/dist/multicall/index.cjs.map +1 -1
- package/dist/multicall/index.d.cts +5 -2
- package/dist/multicall/index.d.ts +5 -2
- package/dist/multicall/index.js +4 -1
- package/dist/oracle/index.cjs +122 -14
- package/dist/oracle/index.cjs.map +1 -1
- package/dist/oracle/index.d.cts +3 -1
- package/dist/oracle/index.d.ts +3 -1
- package/dist/oracle/index.js +7 -1
- package/dist/oracle/index.js.map +1 -1
- package/dist/passkey-manager/index.cjs +107 -6
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.d.cts +1 -1
- package/dist/passkey-manager/index.d.ts +1 -1
- package/dist/passkey-manager/index.js +6 -3
- package/dist/passkey-manager/index.js.map +1 -1
- package/dist/uploader/index.cjs +4442 -0
- package/dist/uploader/index.cjs.map +1 -0
- package/dist/uploader/index.d.cts +761 -0
- package/dist/uploader/index.d.ts +761 -0
- package/dist/uploader/index.js +4 -0
- package/dist/uploader/index.js.map +1 -0
- package/package.json +27 -2
- package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
- package/src/abi-manager/accounts.ts +91 -0
- package/src/abi-manager/constants.ts +19 -0
- package/src/abi-manager/derivation.ts +86 -0
- package/src/abi-manager/errors.ts +48 -0
- package/src/abi-manager/index.test.ts +136 -0
- package/src/abi-manager/index.ts +56 -0
- package/src/abi-manager/instructions.ts +573 -0
- package/src/abi-manager/types.ts +150 -0
- package/src/amm/index.test.ts +2 -6
- package/src/amm/index.ts +2 -2
- package/src/bootstrap-addresses/index.test.ts +59 -0
- package/src/bootstrap-addresses/index.ts +58 -0
- package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
- package/src/clob/exchange-client.test.ts +115 -2
- package/src/clob/exchange-client.ts +52 -13
- package/src/clob/exchange.test.ts +18 -16
- package/src/clob/exchange.ts +4 -0
- package/src/clob/index.test.ts +140 -7
- package/src/clob/index.ts +304 -12
- package/src/clob/price.test.ts +32 -0
- package/src/clob/price.ts +77 -0
- package/src/deploy/accounts.ts +208 -0
- package/src/deploy/chain.ts +163 -0
- package/src/deploy/constants.ts +30 -0
- package/src/deploy/deploy.test.ts +349 -0
- package/src/deploy/errors.ts +72 -0
- package/src/deploy/index.ts +44 -0
- package/src/deploy/inspection.ts +146 -0
- package/src/deploy/seeds.ts +29 -0
- package/src/deploy/types.ts +178 -0
- package/src/deploy/uploader.test.ts +326 -0
- package/src/deploy/uploader.ts +558 -0
- package/src/deploy/validation-imports.test.ts +55 -0
- package/src/deploy/validation.ts +183 -0
- package/src/deploy/workflows.test.ts +422 -0
- package/src/deploy/workflows.ts +1088 -0
- package/src/helpers/bytes.test.ts +58 -0
- package/src/helpers/bytes.ts +28 -3
- package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/manager/abi/thru/program/manager/types.ts +3814 -0
- package/src/manager/accounts.ts +44 -0
- package/src/manager/constants.ts +16 -0
- package/src/manager/derivation.ts +32 -0
- package/src/manager/errors.ts +47 -0
- package/src/manager/index.test.ts +108 -0
- package/src/manager/index.ts +68 -0
- package/src/manager/instructions.ts +281 -0
- package/src/manager/types.ts +95 -0
- package/src/manager/validation.ts +35 -0
- package/src/multicall/index.test.ts +11 -2
- package/src/multicall/index.ts +12 -5
- package/src/oracle/index.ts +4 -0
- package/src/passkey-manager/constants.ts +2 -1
- package/src/passkey-manager/validate.test.ts +2 -2
- package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
- package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
- package/src/uploader/accounts.ts +38 -0
- package/src/uploader/constants.ts +14 -0
- package/src/uploader/derivation.ts +34 -0
- package/src/uploader/errors.ts +25 -0
- package/src/uploader/index.test.ts +107 -0
- package/src/uploader/index.ts +34 -0
- package/src/uploader/instructions.ts +163 -0
- package/src/uploader/types.ts +67 -0
- package/src/utils/helpers.ts +121 -0
- package/tsup.config.ts +5 -0
- package/dist/chunk-J7B7XYOI.js.map +0 -1
- package/dist/chunk-P5OABVJI.js +0 -28
- package/dist/chunk-P5OABVJI.js.map +0 -1
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { Account, Pubkey, createThruClient } from "@thru/sdk";
|
|
2
|
+
import { encodeAddress } from "@thru/sdk/helpers";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { deriveProgramABIAddresses } from "../abi-manager";
|
|
5
|
+
import { deriveManagedProgramAddresses } from "../manager";
|
|
6
|
+
import { deriveUploadAddresses } from "../uploader";
|
|
7
|
+
import {
|
|
8
|
+
parseABIAccount,
|
|
9
|
+
parseABIMeta,
|
|
10
|
+
parseManagerMeta,
|
|
11
|
+
parseUploaderMeta,
|
|
12
|
+
} from "./accounts";
|
|
13
|
+
import {
|
|
14
|
+
ABI_MANAGER_PROGRAM_ADDRESS,
|
|
15
|
+
MANAGER_PROGRAM_ADDRESS,
|
|
16
|
+
DEPLOYMENT_MULTICALL_PROGRAM_ADDRESS,
|
|
17
|
+
UPLOADER_PROGRAM_ADDRESS,
|
|
18
|
+
} from "./constants";
|
|
19
|
+
import { DeployError } from "./errors";
|
|
20
|
+
import { inspectProgramDeployment } from "./inspection";
|
|
21
|
+
import { seedBytes, temporarySeed } from "./seeds";
|
|
22
|
+
import {
|
|
23
|
+
resolveSigner,
|
|
24
|
+
validateABI,
|
|
25
|
+
validateProgramImage,
|
|
26
|
+
validateSeedAndChunkSize,
|
|
27
|
+
} from "./validation";
|
|
28
|
+
|
|
29
|
+
vi.mock("@thru/sdk/abi", () => ({
|
|
30
|
+
buildLayoutIrWithManifest: vi.fn(async () => ({ rootTypes: [] })),
|
|
31
|
+
OnchainFetcher: class {},
|
|
32
|
+
resolveImports: vi.fn(async (yaml: string) => ({
|
|
33
|
+
root: { id: { packageName: "test.deploy" } },
|
|
34
|
+
manifest: { "test.deploy": yaml },
|
|
35
|
+
})),
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
function address(lastByte: number): string {
|
|
39
|
+
const bytes = new Uint8Array(32);
|
|
40
|
+
bytes[31] = lastByte;
|
|
41
|
+
return encodeAddress(bytes);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function account(owner: string, data: Uint8Array): Account {
|
|
45
|
+
return {
|
|
46
|
+
meta: { owner: Pubkey.from(owner) },
|
|
47
|
+
data: { data },
|
|
48
|
+
} as Account;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe("deployment address derivation", () => {
|
|
52
|
+
it("matches fixed CLI-compatible program, ABI, and uploader vectors", () => {
|
|
53
|
+
const program = deriveManagedProgramAddresses("nft");
|
|
54
|
+
expect(program).toMatchObject({
|
|
55
|
+
programMetaAccountAddress:
|
|
56
|
+
"ta00Efqv-BVcX3MsYbqO9JN2arQVJEMg3xqQF2iy0H1TGV",
|
|
57
|
+
programAccountAddress: "taAFaJ4ctkbuhYBl2FX6tmXGJZQgShIXt6TPrMw4-GOsv4",
|
|
58
|
+
});
|
|
59
|
+
expect(
|
|
60
|
+
deriveProgramABIAddresses(program.programAccountAddress),
|
|
61
|
+
).toMatchObject({
|
|
62
|
+
abiMetaAccountAddress: "taTRXKKLkeKvK_XMkdqbMzcqR0cUY20PBwaluN8UKvkkGF",
|
|
63
|
+
abiAccountAddress: "takDA1V6UYKs86PsY7tQjlGOeFkQhmaVCehzy1TylM0ufs",
|
|
64
|
+
});
|
|
65
|
+
expect(deriveUploadAddresses("nft_temporary")).toMatchObject({
|
|
66
|
+
metaAccountAddress: "taF70vdExq-vx8VUoZBCcNayNbAsNktsrFd34CrRRa-U4_",
|
|
67
|
+
bufferAccountAddress: "ta3qmTeqrBsvHKCX60S7ZZqfuOi-X59-FTeZ1UXCK3GtdX",
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("uses the CLI suffix and SHA-256 fallback rules", async () => {
|
|
72
|
+
await expect(temporarySeed("nft", "temporary")).resolves.toBe(
|
|
73
|
+
"nft_temporary",
|
|
74
|
+
);
|
|
75
|
+
await expect(
|
|
76
|
+
temporarySeed("12345678901234567890123456789012", "temporary"),
|
|
77
|
+
).resolves.toBe("57154b56ad5b3aa779bd86459729936c");
|
|
78
|
+
expect(seedBytes("é".repeat(16))).toHaveLength(32);
|
|
79
|
+
expect(() => seedBytes("é".repeat(17))).toThrow(DeployError);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("uses canonical built-in addresses", () => {
|
|
83
|
+
expect(UPLOADER_PROGRAM_ADDRESS).toBe(address(2));
|
|
84
|
+
expect(MANAGER_PROGRAM_ADDRESS).toBe(address(4));
|
|
85
|
+
expect(DEPLOYMENT_MULTICALL_PROGRAM_ADDRESS).toBe(address(9));
|
|
86
|
+
expect(ABI_MANAGER_PROGRAM_ADDRESS).toBe(
|
|
87
|
+
"taAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACrG7",
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("deployment validation", () => {
|
|
93
|
+
it("validates managed program image version, size, and trailer", () => {
|
|
94
|
+
const valid = new Uint8Array(20);
|
|
95
|
+
valid[0] = 1;
|
|
96
|
+
expect(() => validateProgramImage(valid)).not.toThrow();
|
|
97
|
+
expect(() => validateProgramImage(valid.slice(0, 19))).toThrow(
|
|
98
|
+
/at least 20/,
|
|
99
|
+
);
|
|
100
|
+
const wrongVersion = valid.slice();
|
|
101
|
+
wrongVersion[0] = 2;
|
|
102
|
+
expect(() => validateProgramImage(wrongVersion)).toThrow(/version/);
|
|
103
|
+
const elf = valid.slice();
|
|
104
|
+
elf.set([0x7f, 0x45, 0x4c, 0x46]);
|
|
105
|
+
expect(() => validateProgramImage(elf)).toThrow(/ELF file/);
|
|
106
|
+
const wrongTrailer = valid.slice();
|
|
107
|
+
wrongTrailer[wrongTrailer.length - 1] = 1;
|
|
108
|
+
expect(() => validateProgramImage(wrongTrailer)).toThrow(/zero/);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("validates chunk bounds and self-contained ABI YAML", async () => {
|
|
112
|
+
expect(validateSeedAndChunkSize("nft")).toBe(30_720);
|
|
113
|
+
expect(() => validateSeedAndChunkSize("nft", 1_023)).toThrow(/1024/);
|
|
114
|
+
await expect(
|
|
115
|
+
validateABI(
|
|
116
|
+
new TextEncoder().encode(`
|
|
117
|
+
abi:
|
|
118
|
+
package: "test.deploy"
|
|
119
|
+
abi-version: 1
|
|
120
|
+
package-version: "1.0.0"
|
|
121
|
+
description: "deploy test"
|
|
122
|
+
imports: []
|
|
123
|
+
types:
|
|
124
|
+
- name: "Value"
|
|
125
|
+
kind:
|
|
126
|
+
struct:
|
|
127
|
+
packed: true
|
|
128
|
+
fields:
|
|
129
|
+
- name: "value"
|
|
130
|
+
field-type:
|
|
131
|
+
primitive: u64
|
|
132
|
+
`),
|
|
133
|
+
),
|
|
134
|
+
).resolves.toContain("test.deploy");
|
|
135
|
+
await expect(
|
|
136
|
+
validateABI(
|
|
137
|
+
new TextEncoder().encode(`
|
|
138
|
+
abi:
|
|
139
|
+
package: test.bad
|
|
140
|
+
abi-version: 1
|
|
141
|
+
package-version: "1.0.0"
|
|
142
|
+
imports:
|
|
143
|
+
- type: path
|
|
144
|
+
path: local.abi.yaml
|
|
145
|
+
types: []
|
|
146
|
+
`),
|
|
147
|
+
),
|
|
148
|
+
).rejects.toMatchObject({ code: "INVALID_INPUT" });
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("preserves canonical ABI YAML after resolving imports", async () => {
|
|
152
|
+
const yaml = `
|
|
153
|
+
abi:
|
|
154
|
+
package: test.deploy
|
|
155
|
+
abi-version: 1
|
|
156
|
+
package-version: "1.0.0"
|
|
157
|
+
imports:
|
|
158
|
+
- type: onchain
|
|
159
|
+
address: ta1blxgaYR0dei5aldWJe1vbUtt-LkVEBOzNJtSRhHQcTG
|
|
160
|
+
target: abi
|
|
161
|
+
network: alphanet
|
|
162
|
+
revision: latest
|
|
163
|
+
types: []
|
|
164
|
+
`;
|
|
165
|
+
|
|
166
|
+
await expect(validateABI(new TextEncoder().encode(yaml))).resolves.toBe(
|
|
167
|
+
yaml,
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("requires the private key to match the supplied address", async () => {
|
|
172
|
+
const client = createThruClient();
|
|
173
|
+
const privateKey = new Uint8Array(32).fill(7);
|
|
174
|
+
const publicKey = await client.keys.fromPrivateKey(privateKey);
|
|
175
|
+
const signerAddress = Pubkey.from(publicKey).toThruFmt();
|
|
176
|
+
await expect(
|
|
177
|
+
resolveSigner(client, signerAddress, privateKey),
|
|
178
|
+
).resolves.toMatchObject({
|
|
179
|
+
address: signerAddress,
|
|
180
|
+
});
|
|
181
|
+
await expect(
|
|
182
|
+
resolveSigner(client, address(31), privateKey),
|
|
183
|
+
).rejects.toMatchObject({
|
|
184
|
+
code: "SIGNER_MISMATCH",
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe("deployment transaction composition", () => {
|
|
190
|
+
it("resolves indices after the SDK sorts account lists", async () => {
|
|
191
|
+
const client = createThruClient();
|
|
192
|
+
const privateKey = new Uint8Array(32).fill(9);
|
|
193
|
+
const publicKey = await client.keys.fromPrivateKey(privateKey);
|
|
194
|
+
const low = address(7);
|
|
195
|
+
const high = address(8);
|
|
196
|
+
let observed: number[] = [];
|
|
197
|
+
await client.transactions.buildAndSign({
|
|
198
|
+
feePayer: { publicKey, privateKey },
|
|
199
|
+
program: DEPLOYMENT_MULTICALL_PROGRAM_ADDRESS,
|
|
200
|
+
header: { fee: 0n, nonce: 0n, startSlot: 1n, chainId: 1 },
|
|
201
|
+
accounts: {
|
|
202
|
+
readWrite: [high, low],
|
|
203
|
+
readOnly: [MANAGER_PROGRAM_ADDRESS],
|
|
204
|
+
},
|
|
205
|
+
instructionData: (context) => {
|
|
206
|
+
observed = [
|
|
207
|
+
context.getAccountIndex(low),
|
|
208
|
+
context.getAccountIndex(high),
|
|
209
|
+
context.getAccountIndex(MANAGER_PROGRAM_ADDRESS),
|
|
210
|
+
];
|
|
211
|
+
return Uint8Array.of(0);
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
expect(observed).toEqual([2, 3, 4]);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe("deployment account parsers", () => {
|
|
219
|
+
it("parses manager, ABI, and uploader account formats", () => {
|
|
220
|
+
const authority = new Uint8Array(32).fill(1);
|
|
221
|
+
const managerData = new Uint8Array(73);
|
|
222
|
+
managerData.set(authority);
|
|
223
|
+
new DataView(managerData.buffer).setBigUint64(64, 5n, true);
|
|
224
|
+
managerData[72] = 0;
|
|
225
|
+
expect(
|
|
226
|
+
parseManagerMeta(account(MANAGER_PROGRAM_ADDRESS, managerData)),
|
|
227
|
+
).toEqual({
|
|
228
|
+
authority,
|
|
229
|
+
version: 5n,
|
|
230
|
+
state: 0,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const program = new Uint8Array(32).fill(2);
|
|
234
|
+
const abiMetaData = new Uint8Array(100);
|
|
235
|
+
abiMetaData[0] = 1;
|
|
236
|
+
abiMetaData.set(program, 4);
|
|
237
|
+
expect(
|
|
238
|
+
parseABIMeta(account(ABI_MANAGER_PROGRAM_ADDRESS, abiMetaData)),
|
|
239
|
+
).toEqual({ program });
|
|
240
|
+
|
|
241
|
+
const abiAddress = new Uint8Array(32).fill(3);
|
|
242
|
+
const content = Uint8Array.of(0x61, 0x62, 0x69);
|
|
243
|
+
const abiData = new Uint8Array(45 + content.length);
|
|
244
|
+
abiData.set(abiAddress);
|
|
245
|
+
new DataView(abiData.buffer).setBigUint64(32, 7n, true);
|
|
246
|
+
abiData[40] = 0;
|
|
247
|
+
new DataView(abiData.buffer).setUint32(41, content.length, true);
|
|
248
|
+
abiData.set(content, 45);
|
|
249
|
+
expect(
|
|
250
|
+
parseABIAccount(account(ABI_MANAGER_PROGRAM_ADDRESS, abiData)),
|
|
251
|
+
).toEqual({
|
|
252
|
+
abiMetaAccount: abiAddress,
|
|
253
|
+
revision: 7n,
|
|
254
|
+
state: 0,
|
|
255
|
+
content,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const hash = new Uint8Array(32).fill(4);
|
|
259
|
+
const uploadData = new Uint8Array(65);
|
|
260
|
+
uploadData.set(authority);
|
|
261
|
+
uploadData.set(hash, 32);
|
|
262
|
+
uploadData[64] = 2;
|
|
263
|
+
expect(
|
|
264
|
+
parseUploaderMeta(account(UPLOADER_PROGRAM_ADDRESS, uploadData)),
|
|
265
|
+
).toEqual({
|
|
266
|
+
authority,
|
|
267
|
+
expectedHash: hash,
|
|
268
|
+
state: 2,
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe("deployment inspection", () => {
|
|
274
|
+
it("reports validated program and ABI state with byte matches", async () => {
|
|
275
|
+
const authority = new Uint8Array(32).fill(7);
|
|
276
|
+
const authorityAddress = Pubkey.from(authority).toThruFmt();
|
|
277
|
+
const addresses = deriveManagedProgramAddresses("inspect");
|
|
278
|
+
const abiAddresses = deriveProgramABIAddresses(
|
|
279
|
+
addresses.programAccountAddress,
|
|
280
|
+
);
|
|
281
|
+
const programBytes = Uint8Array.of(1, 2, 3);
|
|
282
|
+
const abiBytes = Uint8Array.of(4, 5);
|
|
283
|
+
const managerData = new Uint8Array(73);
|
|
284
|
+
managerData.set(authority);
|
|
285
|
+
new DataView(managerData.buffer).setBigUint64(64, 6n, true);
|
|
286
|
+
|
|
287
|
+
const abiMetaData = new Uint8Array(100);
|
|
288
|
+
abiMetaData[0] = 1;
|
|
289
|
+
abiMetaData.set(addresses.programAccountBytes, 4);
|
|
290
|
+
const abiData = new Uint8Array(45 + abiBytes.length);
|
|
291
|
+
abiData.set(abiAddresses.abiMetaAccountBytes);
|
|
292
|
+
new DataView(abiData.buffer).setBigUint64(32, 3n, true);
|
|
293
|
+
new DataView(abiData.buffer).setUint32(41, abiBytes.length, true);
|
|
294
|
+
abiData.set(abiBytes, 45);
|
|
295
|
+
|
|
296
|
+
const accounts = new Map<string, Account>([
|
|
297
|
+
[
|
|
298
|
+
addresses.programMetaAccountAddress,
|
|
299
|
+
account(MANAGER_PROGRAM_ADDRESS, managerData),
|
|
300
|
+
],
|
|
301
|
+
[
|
|
302
|
+
addresses.programAccountAddress,
|
|
303
|
+
{
|
|
304
|
+
meta: {
|
|
305
|
+
owner: Pubkey.from(MANAGER_PROGRAM_ADDRESS),
|
|
306
|
+
flags: { isProgram: true },
|
|
307
|
+
},
|
|
308
|
+
data: { data: programBytes },
|
|
309
|
+
} as Account,
|
|
310
|
+
],
|
|
311
|
+
[
|
|
312
|
+
abiAddresses.abiMetaAccountAddress,
|
|
313
|
+
account(ABI_MANAGER_PROGRAM_ADDRESS, abiMetaData),
|
|
314
|
+
],
|
|
315
|
+
[
|
|
316
|
+
abiAddresses.abiAccountAddress,
|
|
317
|
+
account(ABI_MANAGER_PROGRAM_ADDRESS, abiData),
|
|
318
|
+
],
|
|
319
|
+
]);
|
|
320
|
+
const client = {
|
|
321
|
+
accounts: {
|
|
322
|
+
get: vi.fn(async (accountAddress: string) => {
|
|
323
|
+
const stored = accounts.get(accountAddress);
|
|
324
|
+
if (!stored) throw { code: 5 };
|
|
325
|
+
return stored;
|
|
326
|
+
}),
|
|
327
|
+
},
|
|
328
|
+
} as unknown as Parameters<typeof inspectProgramDeployment>[0]["client"];
|
|
329
|
+
|
|
330
|
+
await expect(
|
|
331
|
+
inspectProgramDeployment({
|
|
332
|
+
client,
|
|
333
|
+
seed: "inspect",
|
|
334
|
+
authorityAddress,
|
|
335
|
+
expectedProgramBytes: programBytes,
|
|
336
|
+
inspectABI: true,
|
|
337
|
+
expectedABIBytes: abiBytes,
|
|
338
|
+
}),
|
|
339
|
+
).resolves.toMatchObject({
|
|
340
|
+
program: { status: "present", version: 6n, bytesMatch: true },
|
|
341
|
+
abi: { status: "present", revision: 3n, bytesMatch: true },
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
accounts.delete(addresses.programAccountAddress);
|
|
345
|
+
await expect(
|
|
346
|
+
inspectProgramDeployment({ client, seed: "inspect" }),
|
|
347
|
+
).resolves.toMatchObject({ program: { status: "partial" } });
|
|
348
|
+
});
|
|
349
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DeployProgressPhase,
|
|
3
|
+
DeploymentOperation,
|
|
4
|
+
UploadCleanupResult,
|
|
5
|
+
} from "./types";
|
|
6
|
+
|
|
7
|
+
export type DeployErrorCode =
|
|
8
|
+
| "INVALID_INPUT"
|
|
9
|
+
| "SIGNER_MISMATCH"
|
|
10
|
+
| "TARGET_EXISTS"
|
|
11
|
+
| "TARGET_NOT_FOUND"
|
|
12
|
+
| "TARGET_FINALIZED"
|
|
13
|
+
| "UPLOAD_CONFLICT"
|
|
14
|
+
| "TRANSACTION_FAILED"
|
|
15
|
+
| "VERIFICATION_FAILED"
|
|
16
|
+
| "OUTCOME_UNKNOWN"
|
|
17
|
+
| "RPC_ERROR";
|
|
18
|
+
|
|
19
|
+
export interface DeployErrorOptions {
|
|
20
|
+
operation?: DeploymentOperation;
|
|
21
|
+
phase?: DeployProgressPhase;
|
|
22
|
+
transactionSignature?: string;
|
|
23
|
+
vmError?: number;
|
|
24
|
+
userErrorCode?: bigint;
|
|
25
|
+
addresses?: Record<string, string>;
|
|
26
|
+
cleanup?: UploadCleanupResult[];
|
|
27
|
+
cause?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class DeployError extends Error {
|
|
31
|
+
readonly code: DeployErrorCode;
|
|
32
|
+
readonly operation?: DeploymentOperation;
|
|
33
|
+
readonly phase?: DeployProgressPhase;
|
|
34
|
+
readonly transactionSignature?: string;
|
|
35
|
+
readonly vmError?: number;
|
|
36
|
+
readonly userErrorCode?: bigint;
|
|
37
|
+
addresses?: Record<string, string>;
|
|
38
|
+
cleanup?: UploadCleanupResult[];
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
code: DeployErrorCode,
|
|
42
|
+
message: string,
|
|
43
|
+
options: DeployErrorOptions = {},
|
|
44
|
+
) {
|
|
45
|
+
super(message);
|
|
46
|
+
if (options.cause !== undefined) {
|
|
47
|
+
Object.defineProperty(this, "cause", {
|
|
48
|
+
configurable: true,
|
|
49
|
+
value: options.cause,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
this.name = "DeployError";
|
|
53
|
+
this.code = code;
|
|
54
|
+
this.operation = options.operation;
|
|
55
|
+
this.phase = options.phase;
|
|
56
|
+
this.transactionSignature = options.transactionSignature;
|
|
57
|
+
this.vmError = options.vmError;
|
|
58
|
+
this.userErrorCode = options.userErrorCode;
|
|
59
|
+
this.addresses = options.addresses;
|
|
60
|
+
this.cleanup = options.cleanup;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function asDeployError(
|
|
65
|
+
error: unknown,
|
|
66
|
+
code: DeployErrorCode,
|
|
67
|
+
message: string,
|
|
68
|
+
options: DeployErrorOptions = {},
|
|
69
|
+
): DeployError {
|
|
70
|
+
if (error instanceof DeployError) return error;
|
|
71
|
+
return new DeployError(code, message, { ...options, cause: error });
|
|
72
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export { DeployError } from "./errors";
|
|
2
|
+
export type { DeployErrorCode, DeployErrorOptions } from "./errors";
|
|
3
|
+
export {
|
|
4
|
+
deployProgram,
|
|
5
|
+
deployProgramABI,
|
|
6
|
+
upgradeProgram,
|
|
7
|
+
upgradeProgramABI,
|
|
8
|
+
} from "./workflows";
|
|
9
|
+
export { inspectProgramDeployment } from "./inspection";
|
|
10
|
+
export type {
|
|
11
|
+
ABIAccountInspection,
|
|
12
|
+
DeployProgramABIRequest,
|
|
13
|
+
DeployProgramABIResult,
|
|
14
|
+
DeployProgramRequest,
|
|
15
|
+
DeployProgramResult,
|
|
16
|
+
DeployProgressEvent,
|
|
17
|
+
DeploymentBytes,
|
|
18
|
+
DeploymentPrivateKey,
|
|
19
|
+
DeploymentRequestBase,
|
|
20
|
+
DeploymentSigner,
|
|
21
|
+
InspectProgramDeploymentRequest,
|
|
22
|
+
ProgramAccountInspection,
|
|
23
|
+
ProgramDeploymentInspection,
|
|
24
|
+
UpgradeProgramABIRequest,
|
|
25
|
+
UpgradeProgramABIResult,
|
|
26
|
+
UpgradeProgramRequest,
|
|
27
|
+
UpgradeProgramResult,
|
|
28
|
+
UploadArtifactResult,
|
|
29
|
+
UploadCleanupResult,
|
|
30
|
+
} from "./types";
|
|
31
|
+
|
|
32
|
+
import {
|
|
33
|
+
deployProgram,
|
|
34
|
+
deployProgramABI,
|
|
35
|
+
upgradeProgram,
|
|
36
|
+
upgradeProgramABI,
|
|
37
|
+
} from "./workflows";
|
|
38
|
+
|
|
39
|
+
export const deploy = Object.freeze({
|
|
40
|
+
deployProgram,
|
|
41
|
+
deployProgramABI,
|
|
42
|
+
upgradeProgram,
|
|
43
|
+
upgradeProgramABI,
|
|
44
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { Pubkey } from "@thru/sdk";
|
|
2
|
+
import { deriveProgramABIAddresses } from "../abi-manager";
|
|
3
|
+
import { bytesEqual } from "../helpers/bytes";
|
|
4
|
+
import { deriveManagedProgramAddresses } from "../manager";
|
|
5
|
+
import {
|
|
6
|
+
assertManagedProgramAccount,
|
|
7
|
+
assertPubkeyBytes,
|
|
8
|
+
getOptionalAccount,
|
|
9
|
+
parseABIAccount,
|
|
10
|
+
parseABIMeta,
|
|
11
|
+
parseManagerMeta,
|
|
12
|
+
requireAccountData,
|
|
13
|
+
} from "./accounts";
|
|
14
|
+
import { DeployError, asDeployError } from "./errors";
|
|
15
|
+
import { seedBytes } from "./seeds";
|
|
16
|
+
import type {
|
|
17
|
+
ABIAccountInspection,
|
|
18
|
+
InspectProgramDeploymentRequest,
|
|
19
|
+
ProgramAccountInspection,
|
|
20
|
+
ProgramDeploymentInspection,
|
|
21
|
+
} from "./types";
|
|
22
|
+
|
|
23
|
+
function absentPair(
|
|
24
|
+
metadataPresent: boolean,
|
|
25
|
+
accountPresent: boolean,
|
|
26
|
+
): Extract<ProgramAccountInspection, { status: "missing" | "partial" }> {
|
|
27
|
+
return metadataPresent || accountPresent
|
|
28
|
+
? { status: "partial", metadataPresent, accountPresent }
|
|
29
|
+
: { status: "missing", metadataPresent: false, accountPresent: false };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function inspectProgramDeployment(
|
|
33
|
+
request: InspectProgramDeploymentRequest,
|
|
34
|
+
): Promise<ProgramDeploymentInspection> {
|
|
35
|
+
seedBytes(request.seed);
|
|
36
|
+
const ephemeral = request.ephemeral ?? false;
|
|
37
|
+
const programAddresses = deriveManagedProgramAddresses(
|
|
38
|
+
request.seed,
|
|
39
|
+
ephemeral,
|
|
40
|
+
);
|
|
41
|
+
const abiAddresses = deriveProgramABIAddresses(
|
|
42
|
+
programAddresses.programAccountAddress,
|
|
43
|
+
ephemeral,
|
|
44
|
+
);
|
|
45
|
+
const inspectABI =
|
|
46
|
+
request.inspectABI || request.expectedABIBytes !== undefined;
|
|
47
|
+
const [metadataAccount, programAccount, abiMetadataAccount, abiAccount] =
|
|
48
|
+
await Promise.all([
|
|
49
|
+
getOptionalAccount(
|
|
50
|
+
request.client,
|
|
51
|
+
programAddresses.programMetaAccountAddress,
|
|
52
|
+
),
|
|
53
|
+
getOptionalAccount(
|
|
54
|
+
request.client,
|
|
55
|
+
programAddresses.programAccountAddress,
|
|
56
|
+
),
|
|
57
|
+
inspectABI
|
|
58
|
+
? getOptionalAccount(request.client, abiAddresses.abiMetaAccountAddress)
|
|
59
|
+
: undefined,
|
|
60
|
+
inspectABI
|
|
61
|
+
? getOptionalAccount(request.client, abiAddresses.abiAccountAddress)
|
|
62
|
+
: undefined,
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
let program: ProgramAccountInspection;
|
|
66
|
+
if (!metadataAccount || !programAccount) {
|
|
67
|
+
program = absentPair(!!metadataAccount, !!programAccount);
|
|
68
|
+
} else {
|
|
69
|
+
const metadata = parseManagerMeta(metadataAccount);
|
|
70
|
+
assertManagedProgramAccount(programAccount);
|
|
71
|
+
const authorityAddress = Pubkey.from(metadata.authority).toThruFmt();
|
|
72
|
+
if (request.authorityAddress !== undefined) {
|
|
73
|
+
let expectedAuthority: Uint8Array;
|
|
74
|
+
try {
|
|
75
|
+
expectedAuthority = Pubkey.from(request.authorityAddress).toBytes();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
throw asDeployError(
|
|
78
|
+
error,
|
|
79
|
+
"INVALID_INPUT",
|
|
80
|
+
"authorityAddress is not a valid Thru address",
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (!bytesEqual(metadata.authority, expectedAuthority)) {
|
|
84
|
+
throw new DeployError(
|
|
85
|
+
"SIGNER_MISMATCH",
|
|
86
|
+
`program authority mismatch: expected ${request.authorityAddress}, found ${authorityAddress}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
program = {
|
|
91
|
+
status: "present",
|
|
92
|
+
metadataPresent: true,
|
|
93
|
+
accountPresent: true,
|
|
94
|
+
authorityAddress,
|
|
95
|
+
version: metadata.version,
|
|
96
|
+
state: metadata.state,
|
|
97
|
+
bytesMatch:
|
|
98
|
+
request.expectedProgramBytes === undefined
|
|
99
|
+
? undefined
|
|
100
|
+
: bytesEqual(
|
|
101
|
+
requireAccountData(programAccount, "managed program account"),
|
|
102
|
+
request.expectedProgramBytes,
|
|
103
|
+
),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let abi: ABIAccountInspection | undefined;
|
|
108
|
+
if (inspectABI) {
|
|
109
|
+
if (!abiMetadataAccount || !abiAccount) {
|
|
110
|
+
abi = absentPair(!!abiMetadataAccount, !!abiAccount);
|
|
111
|
+
} else {
|
|
112
|
+
const metadata = parseABIMeta(abiMetadataAccount);
|
|
113
|
+
const account = parseABIAccount(abiAccount);
|
|
114
|
+
assertPubkeyBytes(
|
|
115
|
+
metadata.program,
|
|
116
|
+
programAddresses.programAccountAddress,
|
|
117
|
+
"ABI metadata points to another program",
|
|
118
|
+
);
|
|
119
|
+
assertPubkeyBytes(
|
|
120
|
+
account.abiMetaAccount,
|
|
121
|
+
abiAddresses.abiMetaAccountAddress,
|
|
122
|
+
"ABI account points to another metadata account",
|
|
123
|
+
);
|
|
124
|
+
abi = {
|
|
125
|
+
status: "present",
|
|
126
|
+
metadataPresent: true,
|
|
127
|
+
accountPresent: true,
|
|
128
|
+
revision: account.revision,
|
|
129
|
+
state: account.state,
|
|
130
|
+
bytesMatch:
|
|
131
|
+
request.expectedABIBytes === undefined
|
|
132
|
+
? undefined
|
|
133
|
+
: bytesEqual(account.content, request.expectedABIBytes),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
programMetaAccountAddress: programAddresses.programMetaAccountAddress,
|
|
140
|
+
programAccountAddress: programAddresses.programAccountAddress,
|
|
141
|
+
abiMetaAccountAddress: abiAddresses.abiMetaAccountAddress,
|
|
142
|
+
abiAccountAddress: abiAddresses.abiAccountAddress,
|
|
143
|
+
program,
|
|
144
|
+
abi,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { bytesToHex, sha256 } from "../helpers/bytes";
|
|
2
|
+
import { normalizeSeed } from "../utils/helpers";
|
|
3
|
+
import { DeployError } from "./errors";
|
|
4
|
+
|
|
5
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
6
|
+
|
|
7
|
+
export function seedBytes(seed: string): Uint8Array {
|
|
8
|
+
const bytes = TEXT_ENCODER.encode(seed);
|
|
9
|
+
try {
|
|
10
|
+
return normalizeSeed(bytes);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
throw new DeployError(
|
|
13
|
+
"INVALID_INPUT",
|
|
14
|
+
`seed must be 1-32 UTF-8 bytes, got ${bytes.length}`,
|
|
15
|
+
{ cause: error },
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function temporarySeed(
|
|
21
|
+
seed: string,
|
|
22
|
+
suffix: string,
|
|
23
|
+
): Promise<string> {
|
|
24
|
+
const combined = `${seed}_${suffix}`;
|
|
25
|
+
const combinedBytes = TEXT_ENCODER.encode(combined);
|
|
26
|
+
if (combinedBytes.length <= 32) return combined;
|
|
27
|
+
const digest = await sha256(combinedBytes);
|
|
28
|
+
return bytesToHex(digest.slice(0, 16));
|
|
29
|
+
}
|