@thru/thru-sdk 0.1.19 → 0.1.21
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 +19 -0
- package/dist/{chunk-PH7P5EEU.js → chunk-SHMREHP5.js} +13 -9
- package/dist/chunk-SHMREHP5.js.map +1 -0
- package/dist/client.d.ts +2 -1
- package/dist/client.js +3 -2
- package/dist/client.js.map +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/sdk.d.ts +3 -3
- package/dist/sdk.js +1 -1
- package/dist/{transactions-BzD9hYlc.d.ts → transactions-CLezIeXO.d.ts} +20 -18
- package/package.json +7 -3
- package/buf.gen.yaml +0 -12
- package/buf.lock +0 -9
- package/buf.yaml +0 -15
- package/dist/chunk-PH7P5EEU.js.map +0 -1
- package/proto/thru/common/v1/consensus.proto +0 -73
- package/proto/thru/common/v1/errors.proto +0 -65
- package/proto/thru/common/v1/filters.proto +0 -60
- package/proto/thru/common/v1/pagination.proto +0 -45
- package/proto/thru/core/v1/account.proto +0 -137
- package/proto/thru/core/v1/block.proto +0 -80
- package/proto/thru/core/v1/state.proto +0 -35
- package/proto/thru/core/v1/transaction.proto +0 -136
- package/proto/thru/core/v1/types.proto +0 -50
- package/proto/thru/services/v1/command_service.proto +0 -70
- package/proto/thru/services/v1/query_service.proto +0 -344
- package/proto/thru/services/v1/streaming_service.proto +0 -126
- package/thru-ts-client-sdk/__tests__/helpers/test-utils.ts +0 -228
- package/thru-ts-client-sdk/client.ts +0 -10
- package/thru-ts-client-sdk/core/__tests__/bound-client.test.ts +0 -354
- package/thru-ts-client-sdk/core/__tests__/client.test.ts +0 -53
- package/thru-ts-client-sdk/core/bound-client.ts +0 -156
- package/thru-ts-client-sdk/core/client.ts +0 -38
- package/thru-ts-client-sdk/defaults.ts +0 -26
- package/thru-ts-client-sdk/modules/__tests__/accounts.test.ts +0 -406
- package/thru-ts-client-sdk/modules/__tests__/blocks.test.ts +0 -199
- package/thru-ts-client-sdk/modules/__tests__/events.test.ts +0 -74
- package/thru-ts-client-sdk/modules/__tests__/height.test.ts +0 -39
- package/thru-ts-client-sdk/modules/__tests__/helpers.test.ts +0 -288
- package/thru-ts-client-sdk/modules/__tests__/keys.test.ts +0 -55
- package/thru-ts-client-sdk/modules/__tests__/proofs.test.ts +0 -119
- package/thru-ts-client-sdk/modules/__tests__/streaming.test.ts +0 -152
- package/thru-ts-client-sdk/modules/__tests__/transactions.test.ts +0 -730
- package/thru-ts-client-sdk/modules/__tests__/version.test.ts +0 -40
- package/thru-ts-client-sdk/modules/accounts.ts +0 -141
- package/thru-ts-client-sdk/modules/blocks.ts +0 -75
- package/thru-ts-client-sdk/modules/events.ts +0 -20
- package/thru-ts-client-sdk/modules/height.ts +0 -9
- package/thru-ts-client-sdk/modules/helpers.ts +0 -131
- package/thru-ts-client-sdk/modules/keys.ts +0 -29
- package/thru-ts-client-sdk/modules/proofs.ts +0 -20
- package/thru-ts-client-sdk/modules/streaming.ts +0 -133
- package/thru-ts-client-sdk/modules/transactions.ts +0 -374
- package/thru-ts-client-sdk/modules/version.ts +0 -10
- package/thru-ts-client-sdk/proto/buf/validate/validate_pb.ts +0 -4761
- package/thru-ts-client-sdk/proto/google/api/annotations_pb.ts +0 -39
- package/thru-ts-client-sdk/proto/google/api/client_pb.ts +0 -953
- package/thru-ts-client-sdk/proto/google/api/field_behavior_pb.ts +0 -157
- package/thru-ts-client-sdk/proto/google/api/http_pb.ts +0 -474
- package/thru-ts-client-sdk/proto/google/api/launch_stage_pb.ts +0 -118
- package/thru-ts-client-sdk/proto/thru/common/v1/consensus_pb.ts +0 -162
- package/thru-ts-client-sdk/proto/thru/common/v1/errors_pb.ts +0 -129
- package/thru-ts-client-sdk/proto/thru/common/v1/filters_pb.ts +0 -129
- package/thru-ts-client-sdk/proto/thru/common/v1/pagination_pb.ts +0 -79
- package/thru-ts-client-sdk/proto/thru/core/v1/account_pb.ts +0 -359
- package/thru-ts-client-sdk/proto/thru/core/v1/block_pb.ts +0 -259
- package/thru-ts-client-sdk/proto/thru/core/v1/state_pb.ts +0 -103
- package/thru-ts-client-sdk/proto/thru/core/v1/transaction_pb.ts +0 -528
- package/thru-ts-client-sdk/proto/thru/core/v1/types_pb.ts +0 -100
- package/thru-ts-client-sdk/proto/thru/services/v1/command_service_pb.ts +0 -146
- package/thru-ts-client-sdk/proto/thru/services/v1/query_service_pb.ts +0 -819
- package/thru-ts-client-sdk/proto/thru/services/v1/streaming_service_pb.ts +0 -390
- package/thru-ts-client-sdk/sdk.ts +0 -42
- package/thru-ts-client-sdk/test-scripts/counter.ts +0 -469
- package/thru-ts-client-sdk/test-scripts/create-account.ts +0 -74
- package/thru-ts-client-sdk/test-scripts/get-height.ts +0 -52
- package/thru-ts-client-sdk/transactions/Transaction.ts +0 -240
- package/thru-ts-client-sdk/transactions/TransactionBuilder.ts +0 -53
- package/thru-ts-client-sdk/transactions/__tests__/TransactionBuilder.test.ts +0 -411
- package/thru-ts-client-sdk/transactions/__tests__/utils.test.ts +0 -214
- package/thru-ts-client-sdk/transactions/index.ts +0 -3
- package/thru-ts-client-sdk/transactions/types.ts +0 -74
- package/thru-ts-client-sdk/transactions/utils.ts +0 -132
- package/thru-ts-client-sdk/types/types.ts +0 -8
- package/thru-ts-client-sdk/utils/utils.ts +0 -27
- package/tsconfig.json +0 -9
- package/tsup.config.ts +0 -14
- package/vitest.config.ts +0 -31
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { createMockContext } from "../../__tests__/helpers/test-utils";
|
|
3
|
-
import { getVersion } from "../version";
|
|
4
|
-
|
|
5
|
-
describe("version", () => {
|
|
6
|
-
describe("getVersion", () => {
|
|
7
|
-
it("should return version response", async () => {
|
|
8
|
-
const ctx = createMockContext();
|
|
9
|
-
const mockResponse = {
|
|
10
|
-
versions: {
|
|
11
|
-
"component1": "1.0.0",
|
|
12
|
-
"component2": "2.1.0",
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
vi.spyOn(ctx.query, "getVersion").mockResolvedValue(mockResponse as any);
|
|
16
|
-
|
|
17
|
-
const result = await getVersion(ctx);
|
|
18
|
-
|
|
19
|
-
expect(result).toBe(mockResponse);
|
|
20
|
-
expect(result.versions).toEqual({
|
|
21
|
-
"component1": "1.0.0",
|
|
22
|
-
"component2": "2.1.0",
|
|
23
|
-
});
|
|
24
|
-
expect(ctx.query.getVersion).toHaveBeenCalledTimes(1);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("should create empty request", async () => {
|
|
28
|
-
const ctx = createMockContext();
|
|
29
|
-
const mockResponse = { versions: {} };
|
|
30
|
-
vi.spyOn(ctx.query, "getVersion").mockResolvedValue(mockResponse as any);
|
|
31
|
-
|
|
32
|
-
await getVersion(ctx);
|
|
33
|
-
|
|
34
|
-
const callArgs = (ctx.query.getVersion as any).mock.calls[0][0];
|
|
35
|
-
// Request should be created (empty GetVersionRequest)
|
|
36
|
-
expect(callArgs).toBeDefined();
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import { BytesLike } from "@thru/helpers";
|
|
4
|
-
import type { ThruClientContext } from "../core/client";
|
|
5
|
-
import { DEFAULT_ACCOUNT_VIEW, DEFAULT_MIN_CONSENSUS, DEFAULT_VERSION_CONTEXT } from "../defaults";
|
|
6
|
-
import { ConsensusStatus, VersionContext } from "../proto/thru/common/v1/consensus_pb";
|
|
7
|
-
import { type Filter } from "../proto/thru/common/v1/filters_pb";
|
|
8
|
-
import type { PageRequest } from "../proto/thru/common/v1/pagination_pb";
|
|
9
|
-
import { Account, AccountView, DataSlice, RawAccount } from "../proto/thru/core/v1/account_pb";
|
|
10
|
-
import { StateProofType } from "../proto/thru/core/v1/state_pb";
|
|
11
|
-
import {
|
|
12
|
-
GetAccountRequestSchema,
|
|
13
|
-
GetRawAccountRequestSchema,
|
|
14
|
-
ListAccountsRequestSchema,
|
|
15
|
-
type ListAccountsResponse,
|
|
16
|
-
} from "../proto/thru/services/v1/query_service_pb";
|
|
17
|
-
import type { Transaction } from "../transactions/Transaction";
|
|
18
|
-
import { TransactionBuilder } from "../transactions/TransactionBuilder";
|
|
19
|
-
import type { TransactionHeaderInput } from "../transactions/types";
|
|
20
|
-
import { mergeTransactionHeader } from "../utils/utils";
|
|
21
|
-
import { getBlockHeight } from "./height";
|
|
22
|
-
import { toPubkey } from "./helpers";
|
|
23
|
-
import { generateStateProof } from "./proofs";
|
|
24
|
-
|
|
25
|
-
export interface CreateAccountOptions {
|
|
26
|
-
/** The new account's public key (fee payer). */
|
|
27
|
-
publicKey: BytesLike;
|
|
28
|
-
/** Optional overrides for the transaction header. */
|
|
29
|
-
header?: Partial<TransactionHeaderInput>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
export interface AccountQueryOptions {
|
|
34
|
-
view?: AccountView;
|
|
35
|
-
versionContext?: VersionContext;
|
|
36
|
-
minConsensus?: ConsensusStatus;
|
|
37
|
-
dataSlice?: DataSlice;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface RawAccountQueryOptions {
|
|
41
|
-
view?: AccountView;
|
|
42
|
-
versionContext?: VersionContext;
|
|
43
|
-
minConsensus?: ConsensusStatus;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface ListAccountsOptions {
|
|
47
|
-
view?: AccountView;
|
|
48
|
-
versionContext?: VersionContext;
|
|
49
|
-
filter?: Filter;
|
|
50
|
-
page?: PageRequest;
|
|
51
|
-
minConsensus?: ConsensusStatus;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function getAccount(
|
|
55
|
-
ctx: ThruClientContext,
|
|
56
|
-
address: BytesLike,
|
|
57
|
-
options: AccountQueryOptions = {},
|
|
58
|
-
): Promise<Account> {
|
|
59
|
-
const request = create(GetAccountRequestSchema, {
|
|
60
|
-
address: toPubkey(address, "address"),
|
|
61
|
-
view: options.view ?? DEFAULT_ACCOUNT_VIEW,
|
|
62
|
-
versionContext: options.versionContext ?? DEFAULT_VERSION_CONTEXT,
|
|
63
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
64
|
-
dataSlice: options.dataSlice,
|
|
65
|
-
});
|
|
66
|
-
return ctx.query.getAccount(request);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function getRawAccount(
|
|
70
|
-
ctx: ThruClientContext,
|
|
71
|
-
address: BytesLike,
|
|
72
|
-
options: RawAccountQueryOptions = {},
|
|
73
|
-
): Promise<RawAccount> {
|
|
74
|
-
const request = create(GetRawAccountRequestSchema, {
|
|
75
|
-
address: toPubkey(address, "address"),
|
|
76
|
-
view: options.view ?? DEFAULT_ACCOUNT_VIEW,
|
|
77
|
-
versionContext: options.versionContext ?? DEFAULT_VERSION_CONTEXT,
|
|
78
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
79
|
-
});
|
|
80
|
-
return ctx.query.getRawAccount(request);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function listAccounts(
|
|
84
|
-
ctx: ThruClientContext,
|
|
85
|
-
options: ListAccountsOptions,
|
|
86
|
-
): Promise<ListAccountsResponse> {
|
|
87
|
-
const request = create(ListAccountsRequestSchema, {
|
|
88
|
-
view: options.view ?? DEFAULT_ACCOUNT_VIEW,
|
|
89
|
-
versionContext: options.versionContext ?? DEFAULT_VERSION_CONTEXT,
|
|
90
|
-
filter: options.filter,
|
|
91
|
-
page: options.page,
|
|
92
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
93
|
-
});
|
|
94
|
-
return ctx.query.listAccounts(request);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export async function createAccount(
|
|
98
|
-
ctx: ThruClientContext,
|
|
99
|
-
options: CreateAccountOptions,
|
|
100
|
-
): Promise<Transaction> {
|
|
101
|
-
const feePayer = toPubkey(options.publicKey, "publicKey").value;
|
|
102
|
-
|
|
103
|
-
const height = await getBlockHeight(ctx);
|
|
104
|
-
const startSlot = height.finalized;
|
|
105
|
-
|
|
106
|
-
const proofResponse = await generateStateProof(ctx, {
|
|
107
|
-
address: options.publicKey,
|
|
108
|
-
proofType: StateProofType.CREATING,
|
|
109
|
-
targetSlot: startSlot,
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
const proofBytes = proofResponse.proof?.proof;
|
|
113
|
-
if (!proofBytes || proofBytes.length === 0) {
|
|
114
|
-
throw new Error("State proof generation returned empty proof");
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const program = new Uint8Array(32);
|
|
118
|
-
program[31] = 0x02;
|
|
119
|
-
|
|
120
|
-
const builder = new TransactionBuilder();
|
|
121
|
-
const headerDefaults: TransactionHeaderInput = {
|
|
122
|
-
fee: 0n,
|
|
123
|
-
nonce: 0n,
|
|
124
|
-
startSlot,
|
|
125
|
-
expiryAfter: 100,
|
|
126
|
-
computeUnits: 10_000,
|
|
127
|
-
memoryUnits: 10_000,
|
|
128
|
-
stateUnits: 10_000,
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const header = mergeTransactionHeader(headerDefaults, options.header);
|
|
132
|
-
|
|
133
|
-
const transaction = builder.build({
|
|
134
|
-
feePayer: { publicKey: feePayer },
|
|
135
|
-
program,
|
|
136
|
-
header,
|
|
137
|
-
proofs: { feePayerStateProof: proofBytes }
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
return transaction;
|
|
141
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import type { ThruClientContext } from "../core/client";
|
|
4
|
-
import { DEFAULT_BLOCK_VIEW, DEFAULT_MIN_CONSENSUS } from "../defaults";
|
|
5
|
-
import { ConsensusStatus } from "../proto/thru/common/v1/consensus_pb";
|
|
6
|
-
import type { Filter } from "../proto/thru/common/v1/filters_pb";
|
|
7
|
-
import type { PageRequest } from "../proto/thru/common/v1/pagination_pb";
|
|
8
|
-
import { Block, BlockView, RawBlock } from "../proto/thru/core/v1/block_pb";
|
|
9
|
-
import {
|
|
10
|
-
GetBlockRequestSchema,
|
|
11
|
-
GetRawBlockRequestSchema,
|
|
12
|
-
ListBlocksRequestSchema,
|
|
13
|
-
ListBlocksResponse,
|
|
14
|
-
} from "../proto/thru/services/v1/query_service_pb";
|
|
15
|
-
import { isSlotSelector } from "../utils/utils";
|
|
16
|
-
import type { BlockSelector } from "./helpers";
|
|
17
|
-
import { toBlockHash } from "./helpers";
|
|
18
|
-
|
|
19
|
-
export interface BlockQueryOptions {
|
|
20
|
-
view?: BlockView;
|
|
21
|
-
minConsensus?: ConsensusStatus;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface RawBlockQueryOptions {
|
|
25
|
-
minConsensus?: ConsensusStatus;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface ListBlocksOptions {
|
|
29
|
-
filter?: Filter;
|
|
30
|
-
page?: PageRequest;
|
|
31
|
-
view?: BlockView;
|
|
32
|
-
minConsensus?: ConsensusStatus;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function getBlock(
|
|
36
|
-
ctx: ThruClientContext,
|
|
37
|
-
selector: BlockSelector,
|
|
38
|
-
options: BlockQueryOptions = {},
|
|
39
|
-
): Promise<Block> {
|
|
40
|
-
const request = create(GetBlockRequestSchema, {
|
|
41
|
-
selector: isSlotSelector(selector)
|
|
42
|
-
? { case: "slot", value: typeof selector.slot === "bigint" ? selector.slot : BigInt(selector.slot) }
|
|
43
|
-
: { case: "blockHash", value: toBlockHash(selector.blockHash) },
|
|
44
|
-
view: options.view ?? DEFAULT_BLOCK_VIEW,
|
|
45
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
46
|
-
});
|
|
47
|
-
return ctx.query.getBlock(request);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function getRawBlock(
|
|
51
|
-
ctx: ThruClientContext,
|
|
52
|
-
selector: BlockSelector,
|
|
53
|
-
options: RawBlockQueryOptions = {},
|
|
54
|
-
): Promise<RawBlock> {
|
|
55
|
-
const request = create(GetRawBlockRequestSchema, {
|
|
56
|
-
selector: isSlotSelector(selector)
|
|
57
|
-
? { case: "slot", value: typeof selector.slot === "bigint" ? selector.slot : BigInt(selector.slot) }
|
|
58
|
-
: { case: "blockHash", value: toBlockHash(selector.blockHash) },
|
|
59
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
60
|
-
});
|
|
61
|
-
return ctx.query.getRawBlock(request);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function listBlocks(
|
|
65
|
-
ctx: ThruClientContext,
|
|
66
|
-
options: ListBlocksOptions = {},
|
|
67
|
-
): Promise<ListBlocksResponse> {
|
|
68
|
-
const request = create(ListBlocksRequestSchema, {
|
|
69
|
-
filter: options.filter,
|
|
70
|
-
page: options.page,
|
|
71
|
-
view: options.view ?? DEFAULT_BLOCK_VIEW,
|
|
72
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
73
|
-
});
|
|
74
|
-
return ctx.query.listBlocks(request);
|
|
75
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import type { ThruClientContext } from "../core/client";
|
|
4
|
-
import type { VersionContext } from "../proto/thru/common/v1/consensus_pb";
|
|
5
|
-
import { Event, GetEventRequestSchema } from "../proto/thru/services/v1/query_service_pb";
|
|
6
|
-
|
|
7
|
-
export interface GetEventOptions {
|
|
8
|
-
versionContext?: VersionContext;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getEvent(ctx: ThruClientContext, eventId: string, options: GetEventOptions = {}): Promise<Event> {
|
|
12
|
-
if (!eventId) {
|
|
13
|
-
throw new Error("eventId is required");
|
|
14
|
-
}
|
|
15
|
-
const request = create(GetEventRequestSchema, {
|
|
16
|
-
eventId,
|
|
17
|
-
versionContext: options.versionContext,
|
|
18
|
-
});
|
|
19
|
-
return ctx.query.getEvent(request);
|
|
20
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import type { ThruClientContext } from "../core/client";
|
|
4
|
-
import { GetHeightRequestSchema, GetHeightResponse } from "../proto/thru/services/v1/query_service_pb";
|
|
5
|
-
|
|
6
|
-
export function getBlockHeight(ctx: ThruClientContext): Promise<GetHeightResponse> {
|
|
7
|
-
const request = create(GetHeightRequestSchema);
|
|
8
|
-
return ctx.query.getHeight(request);
|
|
9
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
import { sha256 } from "@noble/hashes/sha2";
|
|
3
|
-
import { BytesLike, decodeAddress, decodeBase64, decodeSignature, encodeAddress, ensureBytes, hexToBytes, isHexString } from "@thru/helpers";
|
|
4
|
-
|
|
5
|
-
import { BlockHash, BlockHashSchema, Pubkey, PubkeySchema, Signature, SignatureSchema } from "../proto/thru/core/v1/types_pb";
|
|
6
|
-
|
|
7
|
-
export type BlockSelector = { slot: number | bigint } | { blockHash: BytesLike };
|
|
8
|
-
|
|
9
|
-
export function toSignature(value: BytesLike): Signature {
|
|
10
|
-
let bytes: Uint8Array;
|
|
11
|
-
if (value instanceof Uint8Array) {
|
|
12
|
-
if (value.length !== 64) {
|
|
13
|
-
throw new Error("signature must contain 64 bytes");
|
|
14
|
-
}
|
|
15
|
-
bytes = value;
|
|
16
|
-
} else if (typeof value === "string") {
|
|
17
|
-
bytes = value.startsWith("ts") ? decodeSignature(value) : decodeBase64(value);
|
|
18
|
-
} else {
|
|
19
|
-
throw new Error("signature is required");
|
|
20
|
-
}
|
|
21
|
-
if (bytes.length !== 64) {
|
|
22
|
-
throw new Error("signature must contain 64 bytes");
|
|
23
|
-
}
|
|
24
|
-
return create(SignatureSchema, { value: bytes });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function toPubkey(value: BytesLike, field: string): Pubkey {
|
|
28
|
-
let bytes: Uint8Array;
|
|
29
|
-
if (value instanceof Uint8Array) {
|
|
30
|
-
bytes = value;
|
|
31
|
-
} else if (typeof value === "string") {
|
|
32
|
-
bytes = value.startsWith("ta") ? decodeAddress(value) : decodeBase64(value);
|
|
33
|
-
} else {
|
|
34
|
-
throw new Error(`${field} is required`);
|
|
35
|
-
}
|
|
36
|
-
if (bytes.length !== 32) {
|
|
37
|
-
throw new Error(`${field} must contain 32 bytes`);
|
|
38
|
-
}
|
|
39
|
-
return create(PubkeySchema, { value: bytes });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function toBlockHash(value: BytesLike): BlockHash {
|
|
43
|
-
return create(BlockHashSchema, { value: ensureBytes(value, "blockHash") });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface DeriveProgramAddressOptions {
|
|
47
|
-
programAddress: BytesLike;
|
|
48
|
-
seed: BytesLike;
|
|
49
|
-
ephemeral?: boolean;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface DeriveProgramAddressResult {
|
|
53
|
-
bytes: Uint8Array;
|
|
54
|
-
address: string;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function deriveProgramAddress(options: DeriveProgramAddressOptions): DeriveProgramAddressResult {
|
|
58
|
-
const programAddress = normalizeProgramAddress(options.programAddress);
|
|
59
|
-
const seed = normalizeSeed(options.seed);
|
|
60
|
-
const ephemeral = options.ephemeral === true;
|
|
61
|
-
|
|
62
|
-
const derivationInput = new Uint8Array(programAddress.length + 1 + seed.length);
|
|
63
|
-
derivationInput.set(programAddress, 0);
|
|
64
|
-
derivationInput[programAddress.length] = ephemeral ? 1 : 0;
|
|
65
|
-
derivationInput.set(seed, programAddress.length + 1);
|
|
66
|
-
|
|
67
|
-
const hash = sha256(derivationInput);
|
|
68
|
-
const derivedBytes = new Uint8Array(hash.slice(0, 32));
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
bytes: derivedBytes,
|
|
72
|
-
address: encodeAddress(derivedBytes),
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function normalizeProgramAddress(value: BytesLike): Uint8Array {
|
|
77
|
-
if (value instanceof Uint8Array) {
|
|
78
|
-
if (value.length !== 32) {
|
|
79
|
-
throw new Error("Program address must contain 32 bytes");
|
|
80
|
-
}
|
|
81
|
-
return new Uint8Array(value);
|
|
82
|
-
}
|
|
83
|
-
if (typeof value === "string") {
|
|
84
|
-
if (value.startsWith("ta") && value.length === 46) {
|
|
85
|
-
return decodeAddress(value);
|
|
86
|
-
}
|
|
87
|
-
if (isHexString(value)) {
|
|
88
|
-
const bytes = hexToBytes(value);
|
|
89
|
-
if (bytes.length !== 32) {
|
|
90
|
-
throw new Error("Program address hex string must decode to 32 bytes");
|
|
91
|
-
}
|
|
92
|
-
return bytes;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
throw new Error("Program address must be a 32-byte value, ta-address, or 64-character hex string");
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function normalizeSeed(value: BytesLike | string): Uint8Array {
|
|
99
|
-
if (value instanceof Uint8Array) {
|
|
100
|
-
if (value.length === 0) {
|
|
101
|
-
throw new Error("Seed cannot be empty");
|
|
102
|
-
}
|
|
103
|
-
if (value.length > 32) {
|
|
104
|
-
throw new Error("Seed cannot exceed 32 bytes");
|
|
105
|
-
}
|
|
106
|
-
const seed = new Uint8Array(32);
|
|
107
|
-
seed.set(value);
|
|
108
|
-
return seed;
|
|
109
|
-
}
|
|
110
|
-
if (typeof value === "string") {
|
|
111
|
-
if (value.length === 0) {
|
|
112
|
-
throw new Error("Seed cannot be empty");
|
|
113
|
-
}
|
|
114
|
-
if (isHexString(value)) {
|
|
115
|
-
const bytes = hexToBytes(value);
|
|
116
|
-
if (bytes.length !== 32) {
|
|
117
|
-
throw new Error(`Hex seed must decode to 32 bytes, got ${bytes.length}`);
|
|
118
|
-
}
|
|
119
|
-
return bytes;
|
|
120
|
-
}
|
|
121
|
-
const encoder = new TextEncoder();
|
|
122
|
-
const utf8 = encoder.encode(value);
|
|
123
|
-
if (utf8.length > 32) {
|
|
124
|
-
throw new Error(`UTF-8 seed too long: ${utf8.length} bytes (max 32)`);
|
|
125
|
-
}
|
|
126
|
-
const seed = new Uint8Array(32);
|
|
127
|
-
seed.set(utf8);
|
|
128
|
-
return seed;
|
|
129
|
-
}
|
|
130
|
-
throw new Error("Seed must be provided as Uint8Array or string");
|
|
131
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { getWebCrypto, ThruHDWallet } from "@thru/crypto";
|
|
2
|
-
|
|
3
|
-
export interface GeneratedKeyPair {
|
|
4
|
-
address: string;
|
|
5
|
-
publicKey: Uint8Array;
|
|
6
|
-
privateKey: Uint8Array;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Generates a new Ed25519 keypair using the same HD wallet pipeline as the Thru wallet.
|
|
11
|
-
*/
|
|
12
|
-
export async function generateKeyPair(): Promise<GeneratedKeyPair> {
|
|
13
|
-
const seed = generateSeed();
|
|
14
|
-
const account = await ThruHDWallet.getAccount(seed, 0);
|
|
15
|
-
seed.fill(0);
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
address: account.address,
|
|
19
|
-
publicKey: account.publicKey,
|
|
20
|
-
privateKey: account.privateKey,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function generateSeed(): Uint8Array {
|
|
25
|
-
const cryptoObj = getWebCrypto();
|
|
26
|
-
const bytes = new Uint8Array(64);
|
|
27
|
-
cryptoObj.getRandomValues(bytes);
|
|
28
|
-
return bytes;
|
|
29
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import type { ThruClientContext } from "../core/client";
|
|
4
|
-
import { StateProofRequestSchema } from "../proto/thru/core/v1/state_pb";
|
|
5
|
-
import { GenerateStateProofRequestSchema, GenerateStateProofResponse } from "../proto/thru/services/v1/query_service_pb";
|
|
6
|
-
import { GenerateStateProofOptions } from "../types/types";
|
|
7
|
-
import { toPubkey } from "./helpers";
|
|
8
|
-
|
|
9
|
-
export function generateStateProof(
|
|
10
|
-
ctx: ThruClientContext,
|
|
11
|
-
options: GenerateStateProofOptions,
|
|
12
|
-
): Promise<GenerateStateProofResponse> {
|
|
13
|
-
const request = create(StateProofRequestSchema, {
|
|
14
|
-
address: options.address ? toPubkey(options.address, "address") : undefined,
|
|
15
|
-
proofType: options.proofType,
|
|
16
|
-
targetSlot: options.targetSlot,
|
|
17
|
-
});
|
|
18
|
-
const schemaRequest = create(GenerateStateProofRequestSchema, { request });
|
|
19
|
-
return ctx.query.generateStateProof(schemaRequest);
|
|
20
|
-
}
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import { BytesLike } from "@thru/helpers";
|
|
4
|
-
import type { ThruClientContext } from "../core/client";
|
|
5
|
-
import { DEFAULT_ACCOUNT_VIEW, DEFAULT_BLOCK_VIEW, DEFAULT_MIN_CONSENSUS } from "../defaults";
|
|
6
|
-
import type { ConsensusStatus } from "../proto/thru/common/v1/consensus_pb";
|
|
7
|
-
import type { Filter } from "../proto/thru/common/v1/filters_pb";
|
|
8
|
-
import type { AccountView } from "../proto/thru/core/v1/account_pb";
|
|
9
|
-
import type { BlockView } from "../proto/thru/core/v1/block_pb";
|
|
10
|
-
import {
|
|
11
|
-
StreamAccountUpdatesRequestSchema,
|
|
12
|
-
type StreamAccountUpdatesResponse,
|
|
13
|
-
StreamBlocksRequestSchema,
|
|
14
|
-
type StreamBlocksResponse,
|
|
15
|
-
StreamEventsRequestSchema,
|
|
16
|
-
type StreamEventsResponse,
|
|
17
|
-
StreamTransactionsRequestSchema,
|
|
18
|
-
type StreamTransactionsResponse,
|
|
19
|
-
TrackTransactionRequestSchema,
|
|
20
|
-
type TrackTransactionResponse,
|
|
21
|
-
} from "../proto/thru/services/v1/streaming_service_pb";
|
|
22
|
-
import { toPubkey, toSignature as toSignatureMessage } from "./helpers";
|
|
23
|
-
|
|
24
|
-
export interface TrackTransactionOptions {
|
|
25
|
-
timeoutMs?: number;
|
|
26
|
-
signal?: AbortSignal;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface StreamBlocksOptions {
|
|
30
|
-
startSlot?: bigint;
|
|
31
|
-
filter?: Filter;
|
|
32
|
-
view?: BlockView;
|
|
33
|
-
minConsensus?: ConsensusStatus;
|
|
34
|
-
signal?: AbortSignal;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function streamBlocks(
|
|
38
|
-
ctx: ThruClientContext,
|
|
39
|
-
options: StreamBlocksOptions = {},
|
|
40
|
-
): AsyncIterable<StreamBlocksResponse> {
|
|
41
|
-
const request = create(StreamBlocksRequestSchema, {
|
|
42
|
-
startSlot: options.startSlot,
|
|
43
|
-
filter: options.filter,
|
|
44
|
-
view: options.view ?? DEFAULT_BLOCK_VIEW,
|
|
45
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
return ctx.streaming.streamBlocks(request, {
|
|
49
|
-
signal: options.signal,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface StreamAccountUpdatesOptions {
|
|
54
|
-
view?: AccountView;
|
|
55
|
-
filter?: Filter;
|
|
56
|
-
signal?: AbortSignal;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function streamAccountUpdates(
|
|
60
|
-
ctx: ThruClientContext,
|
|
61
|
-
address: BytesLike,
|
|
62
|
-
options: StreamAccountUpdatesOptions = {},
|
|
63
|
-
): AsyncIterable<StreamAccountUpdatesResponse> {
|
|
64
|
-
const request = create(StreamAccountUpdatesRequestSchema, {
|
|
65
|
-
address: toPubkey(address, "address"),
|
|
66
|
-
view: options.view ?? DEFAULT_ACCOUNT_VIEW,
|
|
67
|
-
filter: options.filter,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return ctx.streaming.streamAccountUpdates(request, {
|
|
71
|
-
signal: options.signal,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface StreamTransactionsOptions {
|
|
76
|
-
filter?: Filter;
|
|
77
|
-
minConsensus?: ConsensusStatus;
|
|
78
|
-
signal?: AbortSignal;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function streamTransactions(
|
|
82
|
-
ctx: ThruClientContext,
|
|
83
|
-
options: StreamTransactionsOptions = {},
|
|
84
|
-
): AsyncIterable<StreamTransactionsResponse> {
|
|
85
|
-
const request = create(StreamTransactionsRequestSchema, {
|
|
86
|
-
filter: options.filter,
|
|
87
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
return ctx.streaming.streamTransactions(request, {
|
|
91
|
-
signal: options.signal,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export interface StreamEventsOptions {
|
|
96
|
-
filter?: Filter;
|
|
97
|
-
signal?: AbortSignal;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function streamEvents(
|
|
101
|
-
ctx: ThruClientContext,
|
|
102
|
-
options: StreamEventsOptions = {},
|
|
103
|
-
): AsyncIterable<StreamEventsResponse> {
|
|
104
|
-
const request = create(StreamEventsRequestSchema, {
|
|
105
|
-
filter: options.filter,
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
return ctx.streaming.streamEvents(request, {
|
|
109
|
-
signal: options.signal,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function trackTransaction(
|
|
114
|
-
ctx: ThruClientContext,
|
|
115
|
-
signature: BytesLike,
|
|
116
|
-
options: TrackTransactionOptions = {},
|
|
117
|
-
): AsyncIterable<TrackTransactionResponse> {
|
|
118
|
-
const timeoutMs = options.timeoutMs;
|
|
119
|
-
const request = create(TrackTransactionRequestSchema, {
|
|
120
|
-
signature: toSignatureMessage(signature),
|
|
121
|
-
timeout:
|
|
122
|
-
timeoutMs != null
|
|
123
|
-
? {
|
|
124
|
-
seconds: BigInt(Math.floor(timeoutMs / 1000)),
|
|
125
|
-
nanos: (timeoutMs % 1000) * 1_000_000,
|
|
126
|
-
}
|
|
127
|
-
: undefined,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
return ctx.streaming.trackTransaction(request, {
|
|
131
|
-
signal: options.signal,
|
|
132
|
-
});
|
|
133
|
-
}
|