From f686070b7ddd1ea9d29f9aa47a394b390402cf20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 21 Dec 2018 13:16:17 +0200 Subject: [PATCH] fix(stellar-sdk): misc improvements --- types/stellar-sdk/index.d.ts | 126 +++++++++++++++---------- types/stellar-sdk/stellar-sdk-tests.ts | 2 +- 2 files changed, 76 insertions(+), 52 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index d690398d0c..3583bf98ee 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -5,13 +5,14 @@ // Paul Selden // Max Bause // Timur Ramazanov +// Kalvis Kalniņš // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 /// export class Account { - constructor(accountId: string, sequence: string) + constructor(accountId: string, sequence: string | number) accountId(): string; sequenceNumber(): string; incrementSequenceNumber(): void; @@ -21,7 +22,7 @@ export class CallBuilder { constructor(serverUrl: string) call(): Promise>; cursor(cursor: string): this; - limit(limit: number): this; + limit(limit: number | string): this; order(direction: 'asc' | 'desc'): this; stream(options?: { onmessage?: (record: T) => void, onerror?: (error: Error) => void }): () => void; } @@ -65,6 +66,25 @@ export type CallFunction = () => Promise; export type CallCollectionFunction = (options?: CallFunctionTemplateOptions) => Promise>; +export enum ASSET_TYPE { + native = 'native', + credit4 = 'credit_alphanum4', + credit12 = 'credit_alphanum12', +} + +export interface BalanceLineNative { + balance: string; + asset_type: ASSET_TYPE.native; +} +export interface BalanceLineAsset { + balance: string; + limit: string; + asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_code: string; + asset_issuer: string; +} +export type BalanceLine = BalanceLineNative | BalanceLineAsset; + export interface AccountRecord extends Record { id: string; paging_token: string; @@ -80,19 +100,7 @@ export interface AccountRecord extends Record { auth_required: boolean auth_revocable: boolean }; - balances: Array< - { - balance: string - asset_type: 'native' - } | - { - balance: string - limit: string - asset_type: 'credit_alphanum4' | 'credit_alphanum12' - asset_code: string - asset_issuer: string - } - >; + balances: BalanceLine[]; signers: Array< { public_key: string @@ -111,7 +119,7 @@ export interface AccountRecord extends Record { } export interface AssetRecord extends Record { - asset_type: 'credit_alphanum4' | 'credit_alphanum12'; + asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; asset_code: string; asset_issuer: string; paging_token: string; @@ -129,6 +137,7 @@ export interface EffectRecord extends Record { starting_balance: string; type_i: string; type: string; + amount?: any; operation?: CallFunction; precedes?: CallFunction; @@ -178,6 +187,7 @@ export interface BaseOperationRecord extends Record { paging_token: string; type: string; type_i: number; + source_account: string; self: CallFunction; succeeds: CallFunction; @@ -390,6 +400,8 @@ export interface TransactionRecord extends Record { result_xdr: string; result_meta_xdr: string; memo: string; + envelope: any; + memo_type: any; account: CallFunction; effects: CallCollectionFunction; @@ -419,19 +431,7 @@ export class AccountResponse implements AccountRecord { auth_required: boolean auth_revocable: boolean }; - balances: Array< - { - balance: string - asset_type: 'native' - } | - { - balance: string - limit: string - asset_type: 'credit_alphanum4' | 'credit_alphanum12' - asset_code: string - asset_issuer: string - } - >; + balances: BalanceLine[]; signers: Array< { public_key: string @@ -442,6 +442,7 @@ export class AccountResponse implements AccountRecord { data_attr: { [key: string]: string }; + inflation_destination?: any; effects: CallCollectionFunction; offers: CallCollectionFunction; @@ -462,7 +463,7 @@ export class Asset { getCode(): string; getIssuer(): string; - getAssetType(): 'native' | 'credit_alphanum4' | 'credit_alphanum12'; + getAssetType(): ASSET_TYPE; isNative(): boolean; equals(other: Asset): boolean; toXDRObject(): xdr.Asset; @@ -510,6 +511,13 @@ export class FederationServer { export class LedgerCallBuilder extends CallBuilder { } +export const MemoNone = 'none'; +export const MemoID = 'id'; +export const MemoText = 'text'; +export const MemoHash = 'hash'; +export const MemoReturn = 'return'; +export type MemoType = typeof MemoNone | typeof MemoID | typeof MemoText | typeof MemoHash | typeof MemoReturn; + export class Memo { static fromXDRObject(memo: xdr.Memo): Memo; static hash(hash: string): Memo; @@ -518,9 +526,9 @@ export class Memo { static return(hash: string): Memo; static text(text: string): Memo; - constructor(type: 'none'); - constructor(type: 'id' | 'text' | 'hash' | 'return', value: string) - constructor(type: 'hash' | 'return', value: Buffer) + constructor(type: typeof MemoNone); + constructor(type: typeof MemoID | typeof MemoText | typeof MemoHash | typeof MemoReturn, value: string) + constructor(type: typeof MemoHash | typeof MemoReturn, value: Buffer) type: string; value: null | string | Buffer; @@ -528,12 +536,6 @@ export class Memo { toXDRObject(): xdr.Memo; } -export const MemoNone = 'none'; -export const MemoID = 'id'; -export const MemoText = 'text'; -export const MemoHash = 'hash'; -export const MemoReturn = 'return'; - export enum Networks { PUBLIC = 'Public Global Stellar Network ; September 2015', TESTNET = 'Test SDF Network ; September 2015', @@ -660,7 +662,7 @@ export namespace Operation { interface ManageData extends Operation { type: OperationType.manageData; name: string; - value: string; + value: Buffer; } interface ManageDataOptions { name: string; @@ -678,7 +680,7 @@ export namespace Operation { offerId: string; } interface ManageOfferOptions extends CreatePassiveOfferOptions { - offerId: number | string; + offerId?: number | string; } function manageOffer(options: ManageOfferOptions): xdr.Operation; @@ -784,8 +786,12 @@ export class PaymentCallBuilder extends CallBuilder { forTransaction(transactionId: string): this; } +export interface ServerOptions { + allowHttp: boolean; +} + export class Server { - constructor(serverURL: string, options?: { allowHttp: boolean }) + constructor(serverURL: string, options?: ServerOptions) accounts(): AccountCallBuilder; assets(): AssetsCallBuilder; effects(): EffectCallBuilder; @@ -801,7 +807,7 @@ export class Server { destinationAmount: string, ): PathCallBuilder; payments(): PaymentCallBuilder; - submitTransaction(transaction: Transaction): Promise; + submitTransaction(transaction: Transaction): Promise; tradeAggregation( base: Asset, counter: Asset, @@ -811,6 +817,8 @@ export class Server { ): TradeAggregationCallBuilder; trades(): TradesCallBuilder; transactions(): TransactionCallBuilder; + + serverURL: any; // TODO: require("urijs") } export namespace StrKey { @@ -877,6 +885,7 @@ export class TransactionCallBuilder extends CallBuilder { export class Keypair { static fromRawEd25519Seed(secretSeed: Buffer): Keypair; + static fromBase58Seed(secretSeed: string): Keypair; static fromSecret(secretKey: string): Keypair; static master(): Keypair; static fromPublicKey(publicKey: string): Keypair; @@ -890,6 +899,7 @@ export class Keypair { rawSecretKey(): Buffer; canSign(): boolean; sign(data: Buffer): Buffer; + signDecorated(data: Buffer): xdr.DecoratedSignature; signatureHint(): xdr.SignatureHint; verify(data: Buffer, signature: Buffer): boolean; } @@ -898,21 +908,35 @@ export namespace xdr { class XDRStruct { static fromXDR(xdr: Buffer): XDRStruct; - toXDR(): Buffer; + toXDR(base?: string): Buffer; toXDR(encoding: string): string; } - class Operation extends XDRStruct { } - class Asset extends XDRStruct { } - class Memo extends XDRStruct { } - class TransactionEnvelope extends XDRStruct { } + class Operation extends XDRStruct { + static fromXDR(xdr: Buffer): Operation; + } + class Asset extends XDRStruct { + static fromXDR(xdr: Buffer): Asset; + } + class Memo extends XDRStruct { + static fromXDR(xdr: Buffer): Memo; + } + class TransactionEnvelope extends XDRStruct { + static fromXDR(xdr: Buffer): TransactionEnvelope; + } class DecoratedSignature extends XDRStruct { - constructor(keys: { hint: SignatureHint, signature: Signature }) + static fromXDR(xdr: Buffer): DecoratedSignature; - hint(): SignatureHint; - signature(): Buffer; + constructor(keys: { hint: SignatureHint, signature: Signature }) + + hint(): SignatureHint; + signature(): Buffer; } type SignatureHint = Buffer; type Signature = Buffer; + + class TransactionResult extends XDRStruct { + static fromXDR(xdr: Buffer): TransactionResult; + } } export namespace StellarTomlResolver { diff --git a/types/stellar-sdk/stellar-sdk-tests.ts b/types/stellar-sdk/stellar-sdk-tests.ts index fe72b84a66..c80a17b556 100644 --- a/types/stellar-sdk/stellar-sdk-tests.ts +++ b/types/stellar-sdk/stellar-sdk-tests.ts @@ -12,6 +12,6 @@ transaction; // $ExpectType Transaction StellarSdk.StellarTomlResolver.resolve("example.com", {allowHttp: true, timeout: 100}) .then(toml => toml.FEDERATION_SERVER); -const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)) as StellarSdk.xdr.DecoratedSignature; +const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)); // $ExpectType DecoratedSignature sig.hint(); // $ExpectType Buffer sig.signature(); // $ExpectType Buffer