From f3aa94ecd8c54438e8afe6822006e5510485f88e Mon Sep 17 00:00:00 2001 From: Greg Nazario Date: Tue, 1 Aug 2023 14:06:06 -0700 Subject: [PATCH] [ts-sdk] Export more types (#9366) * [ts-sdk] Export more types * [ts-sdk] Remove duplicate pagination args * [ts-sdk] Run prettier --- ecosystem/typescript/sdk/CHANGELOG.md | 1 + .../sdk/src/account/aptos_account.ts | 2 +- .../sdk/src/aptos_types/account_address.ts | 3 ++ .../aptos_types/rotation_proof_challenge.ts | 4 +-- ecosystem/typescript/sdk/src/index.ts | 13 +++----- .../typescript/sdk/src/plugins/ans_client.ts | 12 +++---- .../typescript/sdk/src/plugins/aptos_token.ts | 4 +-- .../sdk/src/plugins/fungible_asset_client.ts | 3 +- .../sdk/src/providers/aptos_client.ts | 2 +- .../typescript/sdk/src/providers/indexer.ts | 32 +++++++++++-------- .../sdk/src/transaction_builder/builder.ts | 6 ++-- ecosystem/typescript/sdk/src/utils/hd-key.ts | 9 ++---- ecosystem/typescript/sdk/src/utils/misc.ts | 6 +--- 13 files changed, 49 insertions(+), 48 deletions(-) diff --git a/ecosystem/typescript/sdk/CHANGELOG.md b/ecosystem/typescript/sdk/CHANGELOG.md index 539aa7a1080d7..206ad47c17022 100644 --- a/ecosystem/typescript/sdk/CHANGELOG.md +++ b/ecosystem/typescript/sdk/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to the Aptos Node SDK will be captured in this file. This changelog is written by hand for now. It adheres to the format set out by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased +- Export all remaining types and functions in TS SDK ## 1.15.0 (2023-07-28) diff --git a/ecosystem/typescript/sdk/src/account/aptos_account.ts b/ecosystem/typescript/sdk/src/account/aptos_account.ts index cce5ce2118b6b..35bb28f6419e3 100644 --- a/ecosystem/typescript/sdk/src/account/aptos_account.ts +++ b/ecosystem/typescript/sdk/src/account/aptos_account.ts @@ -37,7 +37,7 @@ export class AptosAccount { } /** - * Test derive path + * Check's if the derive path is valid */ static isValidPath(path: string): boolean { return /^m\/44'\/637'\/[0-9]+'\/[0-9]+'\/[0-9]+'+$/.test(path); diff --git a/ecosystem/typescript/sdk/src/aptos_types/account_address.ts b/ecosystem/typescript/sdk/src/aptos_types/account_address.ts index d64945e8c6676..21329051e944f 100644 --- a/ecosystem/typescript/sdk/src/aptos_types/account_address.ts +++ b/ecosystem/typescript/sdk/src/aptos_types/account_address.ts @@ -4,6 +4,9 @@ import { HexString, MaybeHexString } from "../utils"; import { Serializer, Deserializer, Bytes } from "../bcs"; +/** + * Exported as TransactionBuilderTypes.AccountAddress + */ export class AccountAddress { static readonly LENGTH: number = 32; diff --git a/ecosystem/typescript/sdk/src/aptos_types/rotation_proof_challenge.ts b/ecosystem/typescript/sdk/src/aptos_types/rotation_proof_challenge.ts index eb3b6942e5cc7..6befa3f759236 100644 --- a/ecosystem/typescript/sdk/src/aptos_types/rotation_proof_challenge.ts +++ b/ecosystem/typescript/sdk/src/aptos_types/rotation_proof_challenge.ts @@ -2,14 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 import { AccountAddress } from "./account_address"; -import { Serializer } from "../bcs"; +import { AnyNumber, Serializer } from "../bcs"; export class RotationProofChallenge { constructor( public readonly accountAddress: AccountAddress, public readonly moduleName: string, public readonly structName: string, - public readonly sequenceNumber: number | bigint, + public readonly sequenceNumber: AnyNumber, public readonly originator: AccountAddress, public readonly currentAuthKey: AccountAddress, public readonly newPublicKey: Uint8Array, diff --git a/ecosystem/typescript/sdk/src/index.ts b/ecosystem/typescript/sdk/src/index.ts index c584644499d3e..e115b60abd4b0 100644 --- a/ecosystem/typescript/sdk/src/index.ts +++ b/ecosystem/typescript/sdk/src/index.ts @@ -4,17 +4,14 @@ export * from "./account"; export * from "./providers"; export * as BCS from "./bcs"; -export * from "./utils/hex_string"; export * from "./plugins"; export * from "./transaction_builder"; export * from "./transactions"; export * as TokenTypes from "./aptos_types/token_types"; export * as Types from "./generated/index"; export * from "./client"; -export { derivePath } from "./utils/hd-key"; -export { - deserializePropertyMap, - deserializeValueBasedOnTypeTag, - getPropertyValueRaw, -} from "./utils/property_map_serde"; -export { Network, CustomEndpoints } from "./utils/api-endpoints"; +export * from "./utils/api-endpoints"; +export * from "./utils/hex_string"; +export * from "./utils/hd-key"; +export * from "./utils/property_map_serde"; +export { APTOS_COIN } from "./utils/misc"; diff --git a/ecosystem/typescript/sdk/src/plugins/ans_client.ts b/ecosystem/typescript/sdk/src/plugins/ans_client.ts index 4dd4f863089b0..964c5f56ec2db 100644 --- a/ecosystem/typescript/sdk/src/plugins/ans_client.ts +++ b/ecosystem/typescript/sdk/src/plugins/ans_client.ts @@ -4,15 +4,15 @@ import { AptosAccount } from "../account"; import { AccountAddress } from "../aptos_types"; import { TransactionBuilderRemoteABI } from "../transaction_builder"; -const ansContractsMap: Record = { +export const ansContractsMap: Record = { testnet: "0x5f8fd2347449685cf41d4db97926ec3a096eaf381332be4f1318ad4d16a8497c", mainnet: "0x867ed1f6bf916171b1de3ee92849b8978b7d1b9e0a8cc982a3d19d535dfd9c0c", }; // Each name component can only have lowercase letters, number or hyphens, and cannot start or end with a hyphen. -const nameComponentPattern = /^[a-z\d][a-z\d-]{1,61}[a-z\d]$/; +export const nameComponentPattern = /^[a-z\d][a-z\d-]{1,61}[a-z\d]$/; -const namePattern = new RegExp( +export const namePattern = new RegExp( "^" + // Optional subdomain (cannot be followed by .apt) "(?:(?[^.]+)\\.(?!apt$))?" + @@ -23,19 +23,19 @@ const namePattern = new RegExp( "$", ); -type ReverseLookupRegistryV1 = { +export type ReverseLookupRegistryV1 = { registry: { handle: string; }; }; -type NameRegistryV1 = { +export type NameRegistryV1 = { registry: { handle: string; }; }; -type AnsRegistry = { +export type AnsRegistry = { expirationTimestampSeconds: number; target: string | null; }; diff --git a/ecosystem/typescript/sdk/src/plugins/aptos_token.ts b/ecosystem/typescript/sdk/src/plugins/aptos_token.ts index 227f32ba3bac5..cc744ca073738 100644 --- a/ecosystem/typescript/sdk/src/plugins/aptos_token.ts +++ b/ecosystem/typescript/sdk/src/plugins/aptos_token.ts @@ -42,7 +42,7 @@ const PropertyTypeMap = { export type PropertyType = keyof typeof PropertyTypeMap; -type FungibleTokenParameters = { +export type FungibleTokenParameters = { owner: AptosAccount; tokenAddress: MaybeHexString; recipient: MaybeHexString; @@ -50,7 +50,7 @@ type FungibleTokenParameters = { extraArgs?: OptionalTransactionArgs; }; -type NonFungibleTokenParameters = { +export type NonFungibleTokenParameters = { owner: AptosAccount; tokenAddress: MaybeHexString; recipient: MaybeHexString; diff --git a/ecosystem/typescript/sdk/src/plugins/fungible_asset_client.ts b/ecosystem/typescript/sdk/src/plugins/fungible_asset_client.ts index 519413e6fc14d..1821876867561 100644 --- a/ecosystem/typescript/sdk/src/plugins/fungible_asset_client.ts +++ b/ecosystem/typescript/sdk/src/plugins/fungible_asset_client.ts @@ -4,6 +4,7 @@ import * as Gen from "../generated/index"; import { OptionalTransactionArgs, Provider } from "../providers"; import { TransactionBuilderRemoteABI } from "../transaction_builder"; import { MaybeHexString, HexString } from "../utils"; +import { AnyNumber } from "../bcs"; export class FungibleAssetClient { provider: Provider; @@ -85,7 +86,7 @@ export class FungibleAssetClient { sender: AptosAccount, fungibleAssetMetadataAddress: MaybeHexString, recipient: MaybeHexString, - amount: number | bigint, + amount: AnyNumber, extraArgs?: OptionalTransactionArgs, ): Promise { const builder = new TransactionBuilderRemoteABI(this.provider, { diff --git a/ecosystem/typescript/sdk/src/providers/aptos_client.ts b/ecosystem/typescript/sdk/src/providers/aptos_client.ts index 0d079ba0a3650..9c021890cc43a 100644 --- a/ecosystem/typescript/sdk/src/providers/aptos_client.ts +++ b/ecosystem/typescript/sdk/src/providers/aptos_client.ts @@ -45,7 +45,7 @@ export interface OptionalTransactionArgs { providedSequenceNumber?: string | bigint; } -interface PaginationArgs { +export interface PaginationArgs { start?: AnyNumber; limit?: number; } diff --git a/ecosystem/typescript/sdk/src/providers/indexer.ts b/ecosystem/typescript/sdk/src/providers/indexer.ts index d1935b8b82386..997f803bf018c 100644 --- a/ecosystem/typescript/sdk/src/providers/indexer.ts +++ b/ecosystem/typescript/sdk/src/providers/indexer.ts @@ -53,14 +53,14 @@ import { ApiError } from "./aptos_client"; * you would set the offset parameter to 10 (i.e., the index of the first record to retrieve is 10) * and the limit parameter to 10 (i.e., the number of records to retrieve is 10)) */ -interface PaginationArgs { +export interface IndexerPaginationArgs { offset?: AnyNumber; limit?: number; } -type TokenStandard = "v1" | "v2"; +export type TokenStandard = "v1" | "v2"; -type GraphqlQuery = { +export type GraphqlQuery = { query: string; variables?: {}; }; @@ -134,7 +134,10 @@ export class IndexerClient { * @param ownerAddress Hex-encoded 32 byte Aptos account address * @returns GetAccountCurrentTokensQuery response type */ - async getAccountNFTs(ownerAddress: MaybeHexString, options?: PaginationArgs): Promise { + async getAccountNFTs( + ownerAddress: MaybeHexString, + options?: IndexerPaginationArgs, + ): Promise { const address = HexString.ensure(ownerAddress).hex(); IndexerClient.validateAddress(address); const graphqlQuery = { @@ -151,7 +154,7 @@ export class IndexerClient { * @param idHash token id hash * @returns GetTokenActivitiesQuery response type */ - async getTokenActivities(idHash: string, options?: PaginationArgs): Promise { + async getTokenActivities(idHash: string, options?: IndexerPaginationArgs): Promise { const graphqlQuery = { query: GetTokenActivities, variables: { idHash, offset: options?.offset, limit: options?.limit }, @@ -165,7 +168,10 @@ export class IndexerClient { * @param ownerAddress Owner address * @returns GetAccountCoinsDataQuery response type */ - async getAccountCoinsData(ownerAddress: MaybeHexString, options?: PaginationArgs): Promise { + async getAccountCoinsData( + ownerAddress: MaybeHexString, + options?: IndexerPaginationArgs, + ): Promise { const address = HexString.ensure(ownerAddress).hex(); IndexerClient.validateAddress(address); const graphqlQuery = { @@ -215,7 +221,7 @@ export class IndexerClient { */ async getAccountTransactionsData( accountAddress: MaybeHexString, - options?: PaginationArgs, + options?: IndexerPaginationArgs, ): Promise { const address = HexString.ensure(accountAddress).hex(); IndexerClient.validateAddress(address); @@ -390,7 +396,7 @@ export class IndexerClient { * * @returns GetUserTransactionsQuery response type */ - async getUserTransactions(startVersion?: number, options?: PaginationArgs): Promise { + async getUserTransactions(startVersion?: number, options?: IndexerPaginationArgs): Promise { const graphqlQuery = { query: GetUserTransactions, variables: { start_version: startVersion, offset: options?.offset, limit: options?.limit }, @@ -430,7 +436,7 @@ export class IndexerClient { ownerAddress: MaybeHexString, extraArgs?: { tokenStandard?: TokenStandard; - options?: PaginationArgs; + options?: IndexerPaginationArgs; }, ): Promise { const address = HexString.ensure(ownerAddress).hex(); @@ -468,7 +474,7 @@ export class IndexerClient { collectionAddress: string, extraArgs?: { tokenStandard?: TokenStandard; - options?: PaginationArgs; + options?: IndexerPaginationArgs; }, ): Promise { const ownerHexAddress = HexString.ensure(ownerAddress).hex(); @@ -513,7 +519,7 @@ export class IndexerClient { creatorAddress: MaybeHexString, extraArgs?: { tokenStandard?: TokenStandard; - options?: PaginationArgs; + options?: IndexerPaginationArgs; }, ): Promise { const collectionAddress = await this.getCollectionAddress(creatorAddress, collectionName, extraArgs); @@ -536,7 +542,7 @@ export class IndexerClient { collectionName: string, extraArgs?: { tokenStandard?: TokenStandard; - options?: PaginationArgs; + options?: IndexerPaginationArgs; }, ): Promise { const address = HexString.ensure(creatorAddress).hex(); @@ -590,7 +596,7 @@ export class IndexerClient { ownerAddress: MaybeHexString, extraArgs?: { tokenStandard?: TokenStandard; - options?: PaginationArgs; + options?: IndexerPaginationArgs; }, ): Promise { const ownerHexAddress = HexString.ensure(ownerAddress).hex(); diff --git a/ecosystem/typescript/sdk/src/transaction_builder/builder.ts b/ecosystem/typescript/sdk/src/transaction_builder/builder.ts index 1d14282c858bd..fd73b28de9885 100644 --- a/ecosystem/typescript/sdk/src/transaction_builder/builder.ts +++ b/ecosystem/typescript/sdk/src/transaction_builder/builder.ts @@ -43,7 +43,7 @@ export { TypeTagParser } from "../aptos_types"; const RAW_TRANSACTION_SALT = "APTOS::RawTransaction"; const RAW_TRANSACTION_WITH_DATA_SALT = "APTOS::RawTransactionWithData"; -type AnyRawTransaction = RawTransaction | MultiAgentRawTransaction | FeePayerRawTransaction; +export type AnyRawTransaction = RawTransaction | MultiAgentRawTransaction | FeePayerRawTransaction; /** * Function that takes in a Signing Message (serialized raw transaction) @@ -155,7 +155,7 @@ export class TransactionBuilderMultiEd25519 extends TransactionBuilder> & sender: MaybeHexString | AccountAddress; }; -interface AptosClientInterface { +export interface AptosClientInterface { getAccountModules: (accountAddress: MaybeHexString) => Promise; getAccount: (accountAddress: MaybeHexString) => Promise; getChainId: () => Promise; diff --git a/ecosystem/typescript/sdk/src/utils/hd-key.ts b/ecosystem/typescript/sdk/src/utils/hd-key.ts index 32ffd932472a0..79d7d06e47b1c 100644 --- a/ecosystem/typescript/sdk/src/utils/hd-key.ts +++ b/ecosystem/typescript/sdk/src/utils/hd-key.ts @@ -3,10 +3,7 @@ import { hmac } from "@noble/hashes/hmac"; import { sha512 } from "@noble/hashes/sha512"; import { hexToBytes } from "@noble/hashes/utils"; -type Hex = string; -type Path = string; - -type Keys = { +export type Keys = { key: Uint8Array; chainCode: Uint8Array; }; @@ -18,7 +15,7 @@ const replaceDerive = (val: string): string => val.replace("'", ""); const HMAC_KEY = "ed25519 seed"; const HARDENED_OFFSET = 0x80000000; -export const getMasterKeyFromSeed = (seed: Hex): Keys => { +export const getMasterKeyFromSeed = (seed: string): Keys => { const h = hmac.create(sha512, HMAC_KEY); const I = h.update(hexToBytes(seed)).digest(); const IL = I.slice(0, 32); @@ -63,7 +60,7 @@ export const isValidPath = (path: string): boolean => { .some(Number.isNaN as any); }; -export const derivePath = (path: Path, seed: Hex, offset = HARDENED_OFFSET): Keys => { +export const derivePath = (path: string, seed: string, offset = HARDENED_OFFSET): Keys => { if (!isValidPath(path)) { throw new Error("Invalid derivation path"); } diff --git a/ecosystem/typescript/sdk/src/utils/misc.ts b/ecosystem/typescript/sdk/src/utils/misc.ts index 27d95ba16acbc..0fb36ae73018f 100644 --- a/ecosystem/typescript/sdk/src/utils/misc.ts +++ b/ecosystem/typescript/sdk/src/utils/misc.ts @@ -3,10 +3,6 @@ import { VERSION } from "../version"; -export type Nullable = { [P in keyof T]: T[P] | null }; - -export type AnyObject = { [key: string]: any }; - export async function sleep(timeMs: number): Promise { return new Promise((resolve) => { setTimeout(resolve, timeMs); @@ -29,7 +25,7 @@ export function fixNodeUrl(nodeUrl: string): string { export const DEFAULT_MAX_GAS_AMOUNT = 200000; // Transaction expire timestamp export const DEFAULT_TXN_EXP_SEC_FROM_NOW = 20; -// How long does SDK wait for txhn to finish +// How long does SDK wait for txn to finish export const DEFAULT_TXN_TIMEOUT_SEC = 20; export const APTOS_COIN = "0x1::aptos_coin::AptosCoin";