@thru/thru-sdk 0.1.16 → 0.1.20
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 +96 -0
- package/dist/{chunk-FDZQR6ZZ.js → chunk-4JWHIX3Q.js} +168 -65
- package/dist/chunk-4JWHIX3Q.js.map +1 -0
- package/dist/client.d.ts +16 -3
- package/dist/client.js +19 -3
- package/dist/client.js.map +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/sdk.d.ts +5 -4
- package/dist/sdk.js +1 -1
- package/dist/{transactions-X2KKrGw6.d.ts → transactions-CJsRFL_T.d.ts} +562 -98
- package/package.json +15 -4
- package/buf.gen.yaml +0 -12
- package/buf.lock +0 -9
- package/buf.yaml +0 -15
- package/dist/chunk-FDZQR6ZZ.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/client.ts +0 -10
- package/thru-ts-client-sdk/core/bound-client.ts +0 -135
- package/thru-ts-client-sdk/core/client.ts +0 -38
- package/thru-ts-client-sdk/defaults.ts +0 -17
- package/thru-ts-client-sdk/modules/accounts.ts +0 -145
- 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 -34
- package/thru-ts-client-sdk/modules/transactions.ts +0 -274
- 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 -163
- package/thru-ts-client-sdk/proto/thru/common/v1/errors_pb.ts +0 -130
- package/thru-ts-client-sdk/proto/thru/common/v1/filters_pb.ts +0 -81
- package/thru-ts-client-sdk/proto/thru/common/v1/pagination_pb.ts +0 -80
- package/thru-ts-client-sdk/proto/thru/core/v1/account_pb.ts +0 -358
- package/thru-ts-client-sdk/proto/thru/core/v1/block_pb.ts +0 -260
- package/thru-ts-client-sdk/proto/thru/core/v1/state_pb.ts +0 -104
- package/thru-ts-client-sdk/proto/thru/core/v1/transaction_pb.ts +0 -327
- package/thru-ts-client-sdk/proto/thru/core/v1/types_pb.ts +0 -101
- package/thru-ts-client-sdk/proto/thru/services/v1/command_service_pb.ts +0 -81
- package/thru-ts-client-sdk/proto/thru/services/v1/query_service_pb.ts +0 -813
- package/thru-ts-client-sdk/proto/thru/services/v1/streaming_service_pb.ts +0 -391
- package/thru-ts-client-sdk/sdk.ts +0 -40
- package/thru-ts-client-sdk/test-scripts/counter.ts +0 -216
- 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 -48
- package/thru-ts-client-sdk/transactions/__tests__/transaction.test.ts +0 -95
- package/thru-ts-client-sdk/transactions/index.ts +0 -3
- package/thru-ts-client-sdk/transactions/types.ts +0 -64
- 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
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { createClient } from "@connectrpc/connect";
|
|
2
|
-
import { createGrpcWebTransport } from "@connectrpc/connect-web";
|
|
3
|
-
|
|
4
|
-
import { DEFAULT_HOST } from "../defaults";
|
|
5
|
-
import { CommandService } from "../proto/thru/services/v1/command_service_pb";
|
|
6
|
-
import { QueryService } from "../proto/thru/services/v1/query_service_pb";
|
|
7
|
-
import { StreamingService } from "../proto/thru/services/v1/streaming_service_pb";
|
|
8
|
-
|
|
9
|
-
export interface ThruClientConfig {
|
|
10
|
-
baseUrl?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type QueryClient = ReturnType<typeof createClient<typeof QueryService>>;
|
|
14
|
-
type CommandClient = ReturnType<typeof createClient<typeof CommandService>>;
|
|
15
|
-
type StreamingClient = ReturnType<typeof createClient<typeof StreamingService>>;
|
|
16
|
-
|
|
17
|
-
export interface ThruClientContext {
|
|
18
|
-
baseUrl: string;
|
|
19
|
-
transport: ReturnType<typeof createGrpcWebTransport>;
|
|
20
|
-
query: QueryClient;
|
|
21
|
-
command: CommandClient;
|
|
22
|
-
streaming: StreamingClient;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function createThruClientContext(config: ThruClientConfig = {}): ThruClientContext {
|
|
26
|
-
const baseUrl = config.baseUrl ?? DEFAULT_HOST;
|
|
27
|
-
const transport = createGrpcWebTransport({
|
|
28
|
-
baseUrl,
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
baseUrl,
|
|
33
|
-
transport,
|
|
34
|
-
query: createClient(QueryService, transport),
|
|
35
|
-
command: createClient(CommandService, transport),
|
|
36
|
-
streaming: createClient(StreamingService, transport),
|
|
37
|
-
};
|
|
38
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ConsensusStatus } from "./proto/thru/common/v1/consensus_pb";
|
|
2
|
-
import { AccountView } from "./proto/thru/core/v1/account_pb";
|
|
3
|
-
import { BlockView } from "./proto/thru/core/v1/block_pb";
|
|
4
|
-
import { TransactionView } from "./proto/thru/core/v1/transaction_pb";
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_HOST = "https://grpc-web.alphanet.thruput.org";
|
|
7
|
-
|
|
8
|
-
export const DEFAULT_ACCOUNT_VIEW = AccountView.FULL;
|
|
9
|
-
export const DEFAULT_BLOCK_VIEW = BlockView.FULL;
|
|
10
|
-
export const DEFAULT_TRANSACTION_VIEW = TransactionView.FULL;
|
|
11
|
-
export const DEFAULT_MIN_CONSENSUS = ConsensusStatus.UNSPECIFIED;
|
|
12
|
-
|
|
13
|
-
export const DEFAULT_FEE = 1n;
|
|
14
|
-
export const DEFAULT_COMPUTE_UNITS = 300_000_000;
|
|
15
|
-
export const DEFAULT_STATE_UNITS = 10_000;
|
|
16
|
-
export const DEFAULT_MEMORY_UNITS = 10_000;
|
|
17
|
-
export const DEFAULT_EXPIRY_AFTER = 100;
|
|
@@ -1,145 +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 } 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
|
-
ListOwnedAccountsRequestSchema,
|
|
15
|
-
ListOwnedAccountsResponse,
|
|
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 ListOwnedAccountsOptions {
|
|
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,
|
|
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,
|
|
78
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
79
|
-
});
|
|
80
|
-
return ctx.query.getRawAccount(request);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function listOwnedAccounts(
|
|
84
|
-
ctx: ThruClientContext,
|
|
85
|
-
owner: BytesLike,
|
|
86
|
-
options: ListOwnedAccountsOptions = {},
|
|
87
|
-
): Promise<ListOwnedAccountsResponse> {
|
|
88
|
-
const request = create(ListOwnedAccountsRequestSchema, {
|
|
89
|
-
owner: toPubkey(owner, "owner"),
|
|
90
|
-
view: options.view ?? DEFAULT_ACCOUNT_VIEW,
|
|
91
|
-
versionContext: options.versionContext,
|
|
92
|
-
filter: options.filter,
|
|
93
|
-
page: options.page,
|
|
94
|
-
minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
|
|
95
|
-
});
|
|
96
|
-
return ctx.query.listOwnedAccounts(request);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export async function createAccount(
|
|
100
|
-
ctx: ThruClientContext,
|
|
101
|
-
options: CreateAccountOptions,
|
|
102
|
-
): Promise<Transaction> {
|
|
103
|
-
const feePayer = toPubkey(options.publicKey, "publicKey").value;
|
|
104
|
-
|
|
105
|
-
const height = await getBlockHeight(ctx);
|
|
106
|
-
const startSlot = height.finalized;
|
|
107
|
-
|
|
108
|
-
const proofResponse = await generateStateProof(ctx, {
|
|
109
|
-
address: options.publicKey,
|
|
110
|
-
proofType: StateProofType.CREATING,
|
|
111
|
-
targetSlot: startSlot,
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
const proofBytes = proofResponse.proof?.proof;
|
|
115
|
-
if (!proofBytes || proofBytes.length === 0) {
|
|
116
|
-
throw new Error("State proof generation returned empty proof");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const program = new Uint8Array(32);
|
|
120
|
-
program[31] = 0x02;
|
|
121
|
-
|
|
122
|
-
const builder = new TransactionBuilder();
|
|
123
|
-
const headerDefaults: TransactionHeaderInput = {
|
|
124
|
-
fee: 0n,
|
|
125
|
-
nonce: 0n,
|
|
126
|
-
startSlot,
|
|
127
|
-
expiryAfter: 100,
|
|
128
|
-
computeUnits: 10_000,
|
|
129
|
-
memoryUnits: 10_000,
|
|
130
|
-
stateUnits: 10_000,
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const header = mergeTransactionHeader(headerDefaults, options.header);
|
|
134
|
-
|
|
135
|
-
const transaction = builder.build({
|
|
136
|
-
feePayer: { publicKey: feePayer },
|
|
137
|
-
program,
|
|
138
|
-
header,
|
|
139
|
-
content: {
|
|
140
|
-
proofs: { feePayerStateProof: proofBytes }
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
return transaction;
|
|
145
|
-
}
|
|
@@ -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,34 +0,0 @@
|
|
|
1
|
-
import { create } from "@bufbuild/protobuf";
|
|
2
|
-
|
|
3
|
-
import { BytesLike } from "@thru/helpers";
|
|
4
|
-
import type { ThruClientContext } from "../core/client";
|
|
5
|
-
import type { TrackTransactionResponse } from "../proto/thru/services/v1/streaming_service_pb";
|
|
6
|
-
import { TrackTransactionRequestSchema } from "../proto/thru/services/v1/streaming_service_pb";
|
|
7
|
-
import { toSignature as toSignatureMessage } from "./helpers";
|
|
8
|
-
|
|
9
|
-
export interface TrackTransactionOptions {
|
|
10
|
-
timeoutMs?: number;
|
|
11
|
-
signal?: AbortSignal;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function trackTransaction(
|
|
15
|
-
ctx: ThruClientContext,
|
|
16
|
-
signature: BytesLike,
|
|
17
|
-
options: TrackTransactionOptions = {},
|
|
18
|
-
): AsyncIterable<TrackTransactionResponse> {
|
|
19
|
-
const timeoutMs = options.timeoutMs;
|
|
20
|
-
const request = create(TrackTransactionRequestSchema, {
|
|
21
|
-
signature: toSignatureMessage(signature),
|
|
22
|
-
timeout:
|
|
23
|
-
timeoutMs != null
|
|
24
|
-
? {
|
|
25
|
-
seconds: BigInt(Math.floor(timeoutMs / 1000)),
|
|
26
|
-
nanos: (timeoutMs % 1000) * 1_000_000,
|
|
27
|
-
}
|
|
28
|
-
: undefined,
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
return ctx.streaming.trackTransaction(request, {
|
|
32
|
-
signal: options.signal,
|
|
33
|
-
});
|
|
34
|
-
}
|