@thru/thru-sdk 0.1.10 → 0.1.12

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.
Files changed (35) hide show
  1. package/dist/{chunk-CSMWBLLI.js → chunk-FDZQR6ZZ.js} +170 -377
  2. package/dist/chunk-FDZQR6ZZ.js.map +1 -0
  3. package/dist/client.d.ts +6 -1
  4. package/dist/client.js +5 -2
  5. package/dist/client.js.map +1 -1
  6. package/dist/metafile-esm.json +1 -1
  7. package/dist/sdk.d.ts +3 -2
  8. package/dist/sdk.js +1 -1
  9. package/dist/{transactions-DAkq8mUc.d.ts → transactions-X2KKrGw6.d.ts} +77 -63
  10. package/package.json +6 -4
  11. package/proto/thru/common/v1/consensus.proto +1 -3
  12. package/proto/thru/common/v1/errors.proto +4 -6
  13. package/proto/thru/common/v1/filters.proto +16 -6
  14. package/proto/thru/common/v1/pagination.proto +6 -8
  15. package/proto/thru/core/v1/account.proto +26 -27
  16. package/proto/thru/core/v1/block.proto +13 -15
  17. package/proto/thru/core/v1/state.proto +2 -4
  18. package/proto/thru/core/v1/transaction.proto +76 -35
  19. package/proto/thru/core/v1/types.proto +1 -3
  20. package/proto/thru/services/v1/command_service.proto +28 -3
  21. package/proto/thru/services/v1/query_service.proto +47 -47
  22. package/proto/thru/services/v1/streaming_service.proto +16 -18
  23. package/thru-ts-client-sdk/core/bound-client.ts +10 -4
  24. package/thru-ts-client-sdk/modules/accounts.ts +19 -11
  25. package/thru-ts-client-sdk/modules/helpers.ts +3 -212
  26. package/thru-ts-client-sdk/modules/keys.ts +29 -0
  27. package/thru-ts-client-sdk/modules/streaming.ts +1 -1
  28. package/thru-ts-client-sdk/modules/transactions.ts +5 -5
  29. package/thru-ts-client-sdk/sdk.ts +2 -5
  30. package/thru-ts-client-sdk/test-scripts/create-account.ts +11 -16
  31. package/thru-ts-client-sdk/transactions/types.ts +1 -1
  32. package/thru-ts-client-sdk/transactions/utils.ts +1 -3
  33. package/thru-ts-client-sdk/types/types.ts +1 -1
  34. package/thru-ts-client-sdk/utils/utils.ts +15 -58
  35. package/dist/chunk-CSMWBLLI.js.map +0 -1
@@ -32,9 +32,9 @@ import {
32
32
  type TransactionHeaderInput,
33
33
  } from "../transactions";
34
34
  import { parseAccountIdentifier, parseInstructionData } from "../transactions/utils";
35
- import type { BytesLike } from "./helpers";
36
- import { encodeSignature, toSignature as toSignatureMessage } from "./helpers";
35
+ import { toSignature } from "./helpers";
37
36
 
37
+ import { BytesLike, encodeSignature } from "@thru/helpers";
38
38
  import { getAccount } from "./accounts";
39
39
  import { getBlockHeight } from "./height";
40
40
 
@@ -94,7 +94,7 @@ export async function getTransaction(
94
94
  options: TransactionQueryOptions = {},
95
95
  ): Promise<CoreTransaction> {
96
96
  const request = create(GetTransactionRequestSchema, {
97
- signature: toSignatureMessage(signature),
97
+ signature: toSignature(signature),
98
98
  view: options.view ?? DEFAULT_TRANSACTION_VIEW,
99
99
  versionContext: options.versionContext,
100
100
  minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
@@ -108,7 +108,7 @@ export async function getRawTransaction(
108
108
  options: RawTransactionQueryOptions = {},
109
109
  ): Promise<RawTransaction> {
110
110
  const request = create(GetRawTransactionRequestSchema, {
111
- signature: toSignatureMessage(signature),
111
+ signature: toSignature(signature),
112
112
  versionContext: options.versionContext,
113
113
  minConsensus: options.minConsensus ?? DEFAULT_MIN_CONSENSUS,
114
114
  });
@@ -117,7 +117,7 @@ export async function getRawTransaction(
117
117
 
118
118
  export async function getTransactionStatus(ctx: ThruClientContext, signature: BytesLike): Promise<TransactionStatus> {
119
119
  const request = create(GetTransactionStatusRequestSchema, {
120
- signature: toSignatureMessage(signature),
120
+ signature: toSignature(signature),
121
121
  });
122
122
  return ctx.query.getTransactionStatus(request);
123
123
  }
@@ -5,6 +5,7 @@ export * as accounts from "./modules/accounts";
5
5
  export * as blocks from "./modules/blocks";
6
6
  export * as events from "./modules/events";
7
7
  export * as height from "./modules/height";
8
+ export * as keys from "./modules/keys";
8
9
  export * as proofs from "./modules/proofs";
9
10
  export * as streaming from "./modules/streaming";
10
11
  export * as transactions from "./modules/transactions";
@@ -18,19 +19,15 @@ export type {
18
19
  export type { BlockQueryOptions, ListBlocksOptions, RawBlockQueryOptions } from "./modules/blocks";
19
20
  export type { GetEventOptions } from "./modules/events";
20
21
  export {
21
- decodeAddress,
22
- decodeSignature,
23
22
  deriveProgramAddress,
24
- encodeAddress,
25
- encodeSignature,
26
23
  toPubkey
27
24
  } from "./modules/helpers";
28
25
  export type {
29
26
  BlockSelector,
30
- BytesLike,
31
27
  DeriveProgramAddressOptions,
32
28
  DeriveProgramAddressResult
33
29
  } from "./modules/helpers";
30
+ export type { GeneratedKeyPair } from "./modules/keys";
34
31
  export type { TrackTransactionOptions } from "./modules/streaming";
35
32
  export type {
36
33
  BuildAndSignTransactionOptions, BuildTransactionOptions, RawTransactionQueryOptions, TransactionAccountsConfig,
@@ -1,4 +1,4 @@
1
- import { getPublicKeyAsync } from "@noble/ed25519";
1
+ import { bytesToHex } from "@noble/hashes/utils";
2
2
  import { createThruClient } from "../client";
3
3
  import { ConsensusStatus } from "../proto/thru/common/v1/consensus_pb";
4
4
 
@@ -8,20 +8,15 @@ const sdk = createThruClient({
8
8
  });
9
9
 
10
10
  // Replace with the account you want to create and its 32-byte private key.
11
- const targetAccountPrivateKeyHex = "da0abd760d32d319ad11069d2d56f8c61b03f28d4096947c5117a54f427e4d60";
12
- const targetAccountAddress = sdk.helpers.encodeAddress(await getPublicKeyAsync(targetAccountPrivateKeyHex))
13
11
 
14
- function hexToBytes(hex: string): Uint8Array {
15
- const normalized = hex.startsWith("0x") ? hex.slice(2) : hex;
16
- if (normalized.length % 2 !== 0) {
17
- throw new Error("Private key hex must contain an even number of characters");
18
- }
19
- const bytes = new Uint8Array(normalized.length / 2);
20
- for (let i = 0; i < normalized.length; i += 2) {
21
- bytes[i / 2] = parseInt(normalized.substring(i, i + 2), 16);
22
- }
23
- return bytes;
24
- }
12
+ const keypair = await sdk.keys.generateKeyPair()
13
+ const targetAccountAddress = keypair.address
14
+ const targetAccountPrivateKey = keypair.privateKey
15
+
16
+ console.log("Target account address:", targetAccountAddress);
17
+ console.log("Target account private key:", targetAccountPrivateKey);
18
+ const targetAccountPrivateKeyHex = bytesToHex(targetAccountPrivateKey);
19
+ console.log("Target account private key hex:", targetAccountPrivateKeyHex);
25
20
 
26
21
  async function fetchAccountSnapshot(label: string, address: string): Promise<void> {
27
22
  try {
@@ -48,10 +43,10 @@ async function run(): Promise<void> {
48
43
  await fetchAccountSnapshot("Pre-create", targetAccountAddress);
49
44
 
50
45
  const feePayerPublicKey = sdk.helpers.decodeAddress(targetAccountAddress);
51
- const feePayerPrivateKey = hexToBytes(targetAccountPrivateKeyHex);
46
+ const feePayerPrivateKey = targetAccountPrivateKey
52
47
 
53
48
  const transaction = await sdk.accounts.create({
54
- publicKey: feePayerPublicKey
49
+ publicKey: feePayerPublicKey,
55
50
  })
56
51
 
57
52
  await transaction.sign(feePayerPrivateKey);
@@ -1,4 +1,4 @@
1
- import type { BytesLike } from "../modules/helpers";
1
+ import { BytesLike } from "@thru/helpers";
2
2
 
3
3
  export type Bytes32 = Uint8Array;
4
4
  export type Bytes64 = Uint8Array;
@@ -1,6 +1,4 @@
1
- import type { BytesLike } from "../modules/helpers";
2
- import { decodeAddress } from "../modules/helpers";
3
- import { hexToBytes, isHexString } from "../utils/utils";
1
+ import { BytesLike, decodeAddress, hexToBytes, isHexString } from "@thru/helpers";
4
2
  import type { AccountAddress, ProgramIdentifier } from "./types";
5
3
 
6
4
  const ACCOUNT_LIMIT = 1024;
@@ -1,4 +1,4 @@
1
- import { BytesLike } from "../modules/helpers";
1
+ import { BytesLike } from "@thru/helpers";
2
2
  import { StateProofType } from "../proto/thru/core/v1/state_pb";
3
3
 
4
4
  export type GenerateStateProofOptions = {
@@ -1,70 +1,27 @@
1
+ import { BytesLike } from "@thru/helpers";
1
2
 
2
- const BASE64_URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
3
- const BASE64_URL_MAP = new Int16Array(256).fill(-1);
4
- for (let i = 0; i < BASE64_URL_ALPHABET.length; i++) {
5
- BASE64_URL_MAP[BASE64_URL_ALPHABET.charCodeAt(i)] = i;
6
- }
3
+ import { TransactionHeaderInput } from "../transactions";
7
4
 
8
- export type BytesLike = string | Uint8Array;
9
5
  export type BlockSelector = { slot: number | bigint } | { blockHash: BytesLike };
10
6
 
11
- export function decodeBase64(value: string): Uint8Array {
12
- if (value.length === 0) {
13
- return new Uint8Array();
14
- }
15
- const atobFn = globalThis.atob;
16
- if (!atobFn) {
17
- throw new Error("Base64 decoding requires window.atob support");
18
- }
19
- const binary = atobFn(value);
20
- const bytes = new Uint8Array(binary.length);
21
- for (let i = 0; i < binary.length; i++) {
22
- bytes[i] = binary.charCodeAt(i);
23
- }
24
- return bytes;
25
- }
26
-
27
7
  export function isSlotSelector(selector: BlockSelector): selector is { slot: number | bigint } {
28
8
  return "slot" in selector;
29
9
  }
30
10
 
31
- export function ensureBytes(value: BytesLike | undefined, field: string): Uint8Array {
32
- if (value instanceof Uint8Array) {
33
- if (value.length === 0) {
34
- throw new Error(`${field} cannot be empty`);
35
- }
36
- return value;
37
- }
38
- if (typeof value === "string") {
39
- if (value.length === 0) {
40
- throw new Error(`${field} cannot be empty`);
41
- }
42
- return decodeBase64(value);
11
+ export function mergeTransactionHeader(
12
+ defaults: TransactionHeaderInput,
13
+ overrides?: Partial<TransactionHeaderInput>,
14
+ ): TransactionHeaderInput {
15
+ if (!overrides) {
16
+ return defaults;
43
17
  }
44
- throw new Error(`${field} is required`);
45
- }
46
-
47
- export function maskForBits(bits: number): number {
48
- return bits === 0 ? 0 : (1 << bits) - 1;
49
- }
50
18
 
51
- export function isHexString(value: string): boolean {
52
- const normalized = value.startsWith("0x") ? value.slice(2) : value;
53
- return normalized.length % 2 === 0 && normalized.length > 0 && /^[0-9a-fA-F]+$/.test(normalized);
54
- }
19
+ const sanitized = Object.fromEntries(
20
+ Object.entries(overrides).filter(([, value]) => value !== undefined),
21
+ ) as Partial<TransactionHeaderInput>;
55
22
 
56
- export function hexToBytes(value: string): Uint8Array {
57
- const normalized = value.startsWith("0x") ? value.slice(2) : value;
58
- if (normalized.length % 2 !== 0) {
59
- throw new Error("Hex string must contain an even number of characters");
60
- }
61
- const bytes = new Uint8Array(normalized.length / 2);
62
- for (let i = 0; i < normalized.length; i += 2) {
63
- const byte = parseInt(normalized.slice(i, i + 2), 16);
64
- if (Number.isNaN(byte)) {
65
- throw new Error("Hex string contains invalid characters");
66
- }
67
- bytes[i / 2] = byte;
68
- }
69
- return bytes;
23
+ return {
24
+ ...defaults,
25
+ ...sanitized,
26
+ };
70
27
  }