From b058eee449120414cfc85cba5bbef9a5c0fe0397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 3 Jan 2019 11:43:26 +0200 Subject: [PATCH 01/19] fix(operations): review props and options --- types/stellar-sdk/index.d.ts | 139 +++++++++++++++-------------------- 1 file changed, 61 insertions(+), 78 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 89f534c629..9b2afa301e 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for stellar-sdk 0.10 +// Type definitions for stellar-sdk 0.11 // Project: https://github.com/stellar/js-stellar-sdk // Definitions by: Carl Foster // Triston Jones @@ -154,98 +154,89 @@ export namespace StellarBase { } namespace Operation { - interface Operation { - type: OperationType; - source: string | null; - } - interface AccountMerge extends Operation { - type: OperationType.accountMerge; - destination: string; - } - interface AccountMergeOptions { - destination: string; + interface Operation { + type: T; source?: string; } + interface OperationOptions { + source?: string; + } + + interface AccountMerge extends Operation { + destination: string; + } + interface AccountMergeOptions extends OperationOptions { + destination: string; + } function accountMerge(options: AccountMergeOptions): xdr.Operation; - interface AllowTrust extends Operation { - type: OperationType.allowTrust; + interface AllowTrust extends Operation { trustor: string; assetCode: string; - authorize: boolean; + authorize: boolean | undefined; } - interface AllowTrustOptions { + interface AllowTrustOptions extends OperationOptions { trustor: string; assetCode: string; - authorize: boolean; - source?: string; + authorize?: boolean; } function allowTrust(options: AllowTrustOptions): xdr.Operation; - interface ChangeTrust extends Operation { - type: OperationType.changeTrust; + interface ChangeTrust extends Operation { line: Asset; - limit: string | number; + limit: string; } - interface ChangeTrustOptions { + interface ChangeTrustOptions extends OperationOptions { asset: Asset; limit?: string; - source?: string; } function changeTrust(options: ChangeTrustOptions): xdr.Operation; - interface CreateAccount extends Operation { - type: OperationType.createAccount; - source: string; + interface CreateAccount extends Operation { destination: string; - startingBalance: string | number; + startingBalance: string; } - interface CreateAccountOptions { + interface CreateAccountOptions extends OperationOptions { destination: string; startingBalance: string; - source?: string; } function createAccount(options: CreateAccountOptions): xdr.Operation; - interface CreatePassiveOffer extends Operation { - type: OperationType.createPassiveOffer; - selling: Asset; - buying: Asset; - amount: string | number; - price: string | number; - } - interface CreatePassiveOfferOptions { + interface CreatePassiveOffer extends Operation { selling: Asset; buying: Asset; amount: string; - price: number | string | object; - source?: string; + price: string; + } + interface CreatePassiveOfferOptions extends OperationOptions { + selling: Asset; + buying: Asset; + amount: string; + price: number | string | object /* bignumber.js */; } function createPassiveOffer(options: CreatePassiveOfferOptions): xdr.Operation; - interface Inflation extends Operation { - type: OperationType.inflation; + interface Inflation extends Operation { } - function inflation(options: { source?: string }): xdr.Operation; + interface InflationOptions extends OperationOptions { // tslint:disable-line + } + function inflation(options: InflationOptions): xdr.Operation; - interface ManageData extends Operation { - type: OperationType.manageData; + interface ManageData extends Operation { name: string; value: Buffer; } - interface ManageDataOptions { + interface ManageDataOptions extends OperationOptions { name: string; value: string | Buffer; - source?: string; } function manageData(options: ManageDataOptions): xdr.Operation; - interface ManageOffer extends Operation { - type: OperationType.manageOffer; + interface ManageOffer extends Operation { selling: Asset; buying: Asset; - amount: string | number; - price: string | number; + amount: string; + price: string; offerId: string; } interface ManageOfferOptions extends CreatePassiveOfferOptions { @@ -253,37 +244,33 @@ export namespace StellarBase { } function manageOffer(options: ManageOfferOptions): xdr.Operation; - interface PathPayment extends Operation { - type: OperationType.pathPayment; - sendAsset: Asset; - sendMax: string | number; - destination: string; - destAsset: Asset; - destAmount: string | number; - path: Asset[]; - } - interface PathPaymentOptions { + interface PathPayment extends Operation { sendAsset: Asset; sendMax: string; destination: string; destAsset: Asset; destAmount: string; path: Asset[]; - source?: string; + } + interface PathPaymentOptions extends OperationOptions { + sendAsset: Asset; + sendMax: string; + destination: string; + destAsset: Asset; + destAmount: string; + path?: Asset[]; } function pathPayment(options: PathPaymentOptions): xdr.Operation; - interface Payment extends Operation { - type: OperationType.payment; - destination: string; - asset: Asset; - amount: string | number; - } - interface PaymentOptions { - destination: string; - asset: Asset; + interface Payment extends Operation { amount: string; - source?: string; + asset: Asset; + destination: string; + } + interface PaymentOptions extends OperationOptions { + amount: string; + asset: Asset; + destination: string; } function payment(options: PaymentOptions): xdr.Operation; @@ -303,8 +290,7 @@ export namespace StellarBase { preAuthTx?: Buffer | string; weight?: number | string; } - interface SetOptions extends Operation { - type: OperationType.setOptions; + interface SetOptions extends Operation { inflationDest?: string; clearFlags?: AuthFlags; setFlags?: AuthFlags; @@ -315,7 +301,7 @@ export namespace StellarBase { homeDomain?: string; signer?: Signer; } - interface SetOptionsOptions { + interface SetOptionsOptions extends OperationOptions { inflationDest?: string; clearFlags?: AuthFlags; setFlags?: AuthFlags; @@ -325,17 +311,14 @@ export namespace StellarBase { highThreshold?: number | string; signer?: Signer; homeDomain?: string; - source?: string; } function setOptions(options: SetOptionsOptions): xdr.Operation; - interface BumpSequence extends Operation { - type: OperationType.bumpSequence; + interface BumpSequence extends Operation { bumpTo: string; } - interface BumpSequenceOptions { + interface BumpSequenceOptions extends OperationOptions { bumpTo: string; - source?: string; } function bumpSequence(options: BumpSequenceOptions): xdr.Operation; From f17d1c8ea67cbd8c30345d4a680af33103b9d41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 3 Jan 2019 12:00:57 +0200 Subject: [PATCH 02/19] feat(op): setOptions signer conditional type --- types/stellar-sdk/index.d.ts | 61 ++++++++++++++++++-------- types/stellar-sdk/stellar-sdk-tests.ts | 12 +++++ 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 9b2afa301e..9b7307d9c3 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -284,13 +284,49 @@ export namespace StellarBase { Revocable = 2, Immutable = 4, } - interface Signer { - ed25519PublicKey?: string; - sha256Hash?: Buffer | string; - preAuthTx?: Buffer | string; + interface SignerEd25519PublicKey { + ed25519PublicKey: string; + weight: number | undefined; + } + interface SignerSha256Hash { + sha256Hash: Buffer; + weight: number | undefined; + } + interface SignerPreAuthTx { + preAuthTx: Buffer; + weight: number | undefined; + } + type Signer = SignerEd25519PublicKey | SignerSha256Hash | SignerPreAuthTx; + interface SignerEd25519PublicKeyOptions { + ed25519PublicKey: string; weight?: number | string; } - interface SetOptions extends Operation { + interface SignerSha256HashOptions { + sha256Hash: Buffer | string; + weight?: number | string; + } + interface SignerPreAuthTxOptions { + preAuthTx: Buffer | string; + weight?: number | string; + } + type SignerOptions = SignerEd25519PublicKeyOptions | SignerSha256HashOptions | SignerPreAuthTxOptions; + type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; + interface SetOptions extends Operation { + inflationDest?: string; + clearFlags?: AuthFlags; + setFlags?: AuthFlags; + masterWeight?: number; + lowThreshold?: number; + medThreshold?: number; + highThreshold?: number; + homeDomain?: string; + signer: + T extends {ed25519PublicKey: any} ? SignerEd25519PublicKey : + T extends {sha256Hash: any} ? SignerSha256Hash : + T extends {preAuthTx: any} ? SignerPreAuthTx : + never; + } + interface SetOptionsOptions extends OperationOptions { inflationDest?: string; clearFlags?: AuthFlags; setFlags?: AuthFlags; @@ -299,20 +335,9 @@ export namespace StellarBase { medThreshold?: number | string; highThreshold?: number | string; homeDomain?: string; - signer?: Signer; + signer?: T; } - interface SetOptionsOptions extends OperationOptions { - inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; - masterWeight?: number | string; - lowThreshold?: number | string; - medThreshold?: number | string; - highThreshold?: number | string; - signer?: Signer; - homeDomain?: string; - } - function setOptions(options: SetOptionsOptions): xdr.Operation; + function setOptions(options: SetOptionsOptions): xdr.Operation>; interface BumpSequence extends Operation { bumpTo: string; diff --git a/types/stellar-sdk/stellar-sdk-tests.ts b/types/stellar-sdk/stellar-sdk-tests.ts index bf991b4591..c4dcbda7c8 100644 --- a/types/stellar-sdk/stellar-sdk-tests.ts +++ b/types/stellar-sdk/stellar-sdk-tests.ts @@ -30,3 +30,15 @@ StellarSdk.Memo.hash('asdf').value; // $ExpectType Buffer // P.S. don't use Memo constructor (new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).value; // $ExpectType AnyValue (new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType AnyType + +const noSignerXDR = StellarSdk.Operation.setOptions({lowThreshold: 1}); +StellarSdk.Operation.fromXDRObject(noSignerXDR).signer; // $ExpectType never + +const newSignerXDR1 = StellarSdk.Operation.setOptions({signer: {ed25519PublicKey: sourceKey.publicKey(), weight: '1'}}); +StellarSdk.Operation.fromXDRObject(newSignerXDR1).signer; // $ExpectType SignerEd25519PublicKey + +const newSignerXDR2 = StellarSdk.Operation.setOptions({signer: {sha256Hash: Buffer.from(''), weight: '1'}}); +StellarSdk.Operation.fromXDRObject(newSignerXDR2).signer; // $ExpectType SignerSha256Hash + +const newSignerXDR3 = StellarSdk.Operation.setOptions({signer: {preAuthTx: '', weight: 1}}); +StellarSdk.Operation.fromXDRObject(newSignerXDR3).signer; // $ExpectType SignerPreAuthTx From 1f4c27d940d3396ba7120a666de0f33b8f33a545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 3 Jan 2019 12:26:18 +0200 Subject: [PATCH 03/19] fix: Server.ServerOptions -> Server.Options --- types/stellar-sdk/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 9b7307d9c3..82ee2f583e 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -481,7 +481,7 @@ export namespace Config { } export class Server { - constructor(serverURL: string, options?: Server.ServerOptions) + constructor(serverURL: string, options?: Server.Options) accounts(): Server.AccountCallBuilder; assets(): Server.AssetsCallBuilder; effects(): Server.EffectCallBuilder; @@ -975,7 +975,7 @@ export namespace Server { forTransaction(transactionId: string): this; } - interface ServerOptions { + interface Options { allowHttp: boolean; } From 62448ddf78d61bb9f2db512529eb0f0ca6ddd2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Mon, 7 Jan 2019 15:19:19 +0200 Subject: [PATCH 04/19] fix(Operation): default to wide type --- types/stellar-sdk/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 82ee2f583e..2663704322 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -311,7 +311,7 @@ export namespace StellarBase { } type SignerOptions = SignerEd25519PublicKeyOptions | SignerSha256HashOptions | SignerPreAuthTxOptions; type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; - interface SetOptions extends Operation { + interface SetOptions extends Operation { inflationDest?: string; clearFlags?: AuthFlags; setFlags?: AuthFlags; From 4fd0ffd900369f307b49177a316fe596c08aa97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Mon, 7 Jan 2019 15:20:21 +0200 Subject: [PATCH 05/19] fix: add default type argument everywhere --- types/stellar-sdk/index.d.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 2663704322..14d3b1d30e 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -347,7 +347,7 @@ export namespace StellarBase { } function bumpSequence(options: BumpSequenceOptions): xdr.Operation; - function fromXDRObject(xdrOperation: xdr.Operation): T; + function fromXDRObject(xdrOperation: xdr.Operation): T; } namespace StrKey { @@ -384,7 +384,7 @@ export namespace StellarBase { class TransactionBuilder { constructor(sourceAccount: Account, options?: TransactionBuilder.TransactionBuilderOptions) - addOperation(operation: xdr.Operation): this; + addOperation(operation: xdr.Operation): this; addMemo(memo: Memo): this; build(): Transaction; } @@ -407,8 +407,8 @@ export namespace StellarBase { toXDR(base?: string): Buffer; toXDR(encoding: string): string; } - class Operation extends XDRStruct { - static fromXDR(xdr: Buffer): Operation; + class Operation extends XDRStruct { + static fromXDR(xdr: Buffer): Operation; } class Asset extends XDRStruct { static fromXDR(xdr: Buffer): Asset; @@ -512,7 +512,7 @@ export class Server { } export namespace Server { - abstract class CallBuilder { + abstract class CallBuilder { constructor(serverUrl: string) call(): Promise>; cursor(cursor: string): this; @@ -521,7 +521,7 @@ export namespace Server { stream(options?: { onmessage?: (record: T) => void, onerror?: (error: Error) => void }): () => void; } - interface CollectionPage { + interface CollectionPage { records: T[]; next: () => Promise>; prev: () => Promise>; @@ -539,7 +539,7 @@ export namespace Server { } /* Due to a bug with the recursive function requests */ - interface CollectionRecord { + interface CollectionRecord { _links: { next: RecordLink prev: RecordLink @@ -556,8 +556,8 @@ export namespace Server { order?: 'asc' | 'desc'; } - type CallFunction = () => Promise; - type CallCollectionFunction = + type CallFunction = () => Promise; + type CallCollectionFunction = (options?: CallFunctionTemplateOptions) => Promise>; interface BalanceLineNative { From 1cad21f4ecc91406e4e763724fcf2fd4ded5dd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Tue, 8 Jan 2019 15:22:44 +0200 Subject: [PATCH 06/19] feat: seperate out Horizon.*OperationResponse --- types/stellar-sdk/index.d.ts | 366 +++++++++++++++++++++++------------ 1 file changed, 240 insertions(+), 126 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 14d3b1d30e..ba22de39d3 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -673,137 +673,34 @@ export namespace Server { seller?: CallFunction; } - interface BaseOperationRecord extends Record { - id: string; - paging_token: string; - type: string; - type_i: number; - source_account: string; - - self: CallFunction; - succeeds: CallFunction; - precedes: CallFunction; - effects: CallCollectionFunction; - transaction: CallFunction; + import OperationResponseType = Horizon.OperationResponseType; + import OperationResponseTypeI = Horizon.OperationResponseTypeI; + interface BaseOperationRecord< + T extends OperationResponseType = OperationResponseType, + TI extends OperationResponseTypeI = OperationResponseTypeI, + > extends Horizon.BaseOperationResponse { + self: CallFunction; + succeeds: CallFunction; + precedes: CallFunction; + effects: CallCollectionFunction; + transaction: CallFunction; } - interface CreateAccountOperationRecord extends BaseOperationRecord { - type: 'create_account'; - account: string; - funder: string; - starting_balance: string; - } - - interface PaymentOperationRecord extends BaseOperationRecord { - type: 'payment'; - from: string; - to: string; - asset_type: string; - asset_code?: string; - asset_issuer?: string; - amount: string; - + interface CreateAccountOperationRecord extends BaseOperationRecord, Horizon.CreateAccountOperationResponse {} + interface PaymentOperationRecord extends BaseOperationRecord, Horizon.PaymentOperationResponse { sender: CallFunction; receiver: CallFunction; } - - interface PathPaymentOperationRecord extends BaseOperationRecord { - type: 'path_payment'; - from: string; - to: string; - asset_code?: string; - asset_issuer?: string; - asset_type: string; - amount: string; - source_asset_code?: string; - source_asset_issuer?: string; - source_asset_type: string; - source_max: string; - source_amount: string; - } - - interface ManageOfferOperationRecord extends BaseOperationRecord { - type: 'manage_offer'; - offer_id: number; - amount: string; - buying_asset_code?: string; - buying_asset_issuer?: string; - buying_asset_type: string; - price: string; - price_r: { numerator: number, denominator: number }; - selling_asset_code?: string; - selling_asset_issuer?: string; - selling_asset_type: string; - } - - interface PassiveOfferOperationRecord extends BaseOperationRecord { - type: 'create_passive_offer'; - offer_id: number; - amount: string; - buying_asset_code?: string; - buying_asset_issuer?: string; - buying_asset_type: string; - price: string; - price_r: { numerator: number, denominator: number }; - selling_asset_code?: string; - selling_asset_issuer?: string; - selling_asset_type: string; - } - - interface SetOptionsOperationRecord extends BaseOperationRecord { - type: 'set_options'; - signer_key?: string; - signer_weight?: number; - master_key_weight?: number; - low_threshold?: number; - med_threshold?: number; - high_threshold?: number; - home_domain?: string; - set_flags: Array<(1 | 2)>; - set_flags_s: Array<('auth_required_flag' | 'auth_revocable_flag')>; - clear_flags: Array<(1 | 2)>; - clear_flags_s: Array<('auth_required_flag' | 'auth_revocable_flag')>; - } - - interface ChangeTrustOperationRecord extends BaseOperationRecord { - type: 'change_trust'; - asset_code: string; - asset_issuer: string; - asset_type: string; - trustee: string; - trustor: string; - limit: string; - } - - interface AllowTrustOperationRecord extends BaseOperationRecord { - type: 'allow_trust'; - asset_code: string; - asset_issuer: string; - asset_type: string; - authorize: boolean; - trustee: string; - trustor: string; - } - - interface AccountMergeOperationRecord extends BaseOperationRecord { - type: 'account_merge'; - into: string; - } - - interface InflationOperationRecord extends BaseOperationRecord { - type: 'inflation'; - } - - interface ManageDataOperationRecord extends BaseOperationRecord { - type: 'manage_data'; - name: string; - value: Buffer; - } - - interface BumpSequenceOperationRecord extends BaseOperationRecord { - type: 'bump_sequence'; - bump_to: string; - } + interface PathPaymentOperationRecord extends BaseOperationRecord, Horizon.PathPaymentOperationResponse {} + interface ManageOfferOperationRecord extends BaseOperationRecord, Horizon.ManageOfferOperationResponse {} + interface PassiveOfferOperationRecord extends BaseOperationRecord, Horizon.PassiveOfferOperationResponse {} + interface SetOptionsOperationRecord extends BaseOperationRecord, Horizon.SetOptionsOperationResponse {} + interface ChangeTrustOperationRecord extends BaseOperationRecord, Horizon.ChangeTrustOperationResponse {} + interface AllowTrustOperationRecord extends BaseOperationRecord, Horizon.AllowTrustOperationResponse {} + interface AccountMergeOperationRecord extends BaseOperationRecord, Horizon.AccountMergeOperationResponse {} + interface InflationOperationRecord extends BaseOperationRecord, Horizon.InflationOperationResponse {} + interface ManageDataOperationRecord extends BaseOperationRecord, Horizon.ManageDataOperationResponse {} + interface BumpSequenceOperationRecord extends BaseOperationRecord, Horizon.BumpSequenceOperationResponse {} type OperationRecord = CreateAccountOperationRecord | PaymentOperationRecord @@ -1021,3 +918,220 @@ export namespace StellarTomlResolver { function resolve(domain: string, options?: StellarTomlResolveOptions): Promise<{ [key: string]: any }>; } + +export namespace Horizon { + interface ResponseLink { + href: string; + templated?: boolean; + } + interface BaseResponse { + _links: { + [key in T|'self']: ResponseLink + }; + } + interface TransactionResponse extends BaseResponse<'account'|'ledger'|'operations'|'effects'|'succeeds'|'precedes'> { + created_at: string; + envelope_xdr: string; + fee_meta_xdr: string; + fee_paid: number; + hash: string; + id: string; + ledger: number; + memo_type: string; + operation_count: number; + paging_token: string; + result_meta_xdr: string; + result_xdr: string; + signatures: string[]; + source_account: string; + source_account_sequence: string; + } + + interface BalanceLineNative { + balance: string; + asset_type: ASSET_TYPE.native; + } + interface BalanceLineAsset { + balance: string; + limit: string; + asset_type: T; + asset_code: string; + asset_issuer: string; + } + type BalanceLine = + T extends ASSET_TYPE.native ? BalanceLineNative : + T extends ASSET_TYPE.credit4 | ASSET_TYPE.credit12 ? BalanceLineAsset : + BalanceLineNative | BalanceLineAsset; + + interface AccountResponseThresholds { + low_threshold: number; + med_threshold: number; + high_threshold: number; + } + interface AccountResponseFlags { + auth_required: boolean; + auth_revocable: boolean; + } + interface AccountResponseSigner { + public_key: string; + weight: number; + } + interface AccountResponse extends BaseResponse<'transactions'|'operations'|'payments'|'effects'|'offers'|'trades'|'data'> { + id: string; + paging_token: string; + account_id: string; + sequence: string; + subentry_count: number; + thresholds: AccountResponseThresholds; + flags: AccountResponseFlags; + balances: BalanceLine[]; + signers: AccountResponseSigner[]; + data: { + [key: string]: string + }; + } + + enum OperationResponseType { + createAccount = 'create_account', + payment = 'payment', + pathPayment = 'path_payment', + createPassiveOffer = 'create_passive_offer', + manageOffer = 'manage_offer', + setOptions = 'set_options', + changeTrust = 'change_trust', + allowTrust = 'allow_trust', + accountMerge = 'account_merge', + inflation = 'inflation', + manageData = 'manage_data', + bumpSequence = 'bump_sequence', + } + enum OperationResponseTypeI { + createAccount = 0, + payment = 1, + pathPayment = 2, + createPassiveOffer = 3, + manageOffer = 4, + setOptions = 5, + changeTrust = 6, + allowTrust = 7, + accountMerge = 8, + inflation = 9, + manageData = 10, + bumpSequence = 11, + } + interface BaseOperationResponse< + T extends OperationResponseType = OperationResponseType, + TI extends OperationResponseTypeI = OperationResponseTypeI, + > extends BaseResponse<'succeeds'|'precedes'|'effects'|'transaction'> { + id: string; + paging_token: string; + source_account: string; + type: T; + type_i: TI; + created_at: string; + transaction_hash: string; + } + interface CreateAccountOperationResponse extends BaseOperationResponse { + account: string; + funder: string; + starting_balance: string; + } + interface PaymentOperationResponse extends BaseOperationResponse { + from: string; + to: string; + asset_type: ASSET_TYPE; + asset_code?: string; + asset_issuer?: string; + amount: string; + } + interface PathPaymentOperationResponse extends BaseOperationResponse { + from: string; + to: string; + asset_type: ASSET_TYPE; + asset_code?: string; + asset_issuer?: string; + amount: string; + source_asset_type: ASSET_TYPE; + source_asset_code?: string; + source_asset_issuer?: string; + source_max: string; + source_amount: string; + } + interface ManageOfferOperationResponse extends BaseOperationResponse { + offer_id: number; + amount: string; + buying_asset_type: ASSET_TYPE; + buying_asset_code?: string; + buying_asset_issuer?: string; + price: string; + price_r: { numerator: number, denominator: number }; + selling_asset_type: ASSET_TYPE; + selling_asset_code?: string; + selling_asset_issuer?: string; + } + interface PassiveOfferOperationResponse extends BaseOperationResponse { + offer_id: number; + amount: string; + buying_asset_type: ASSET_TYPE; + buying_asset_code?: string; + buying_asset_issuer?: string; + price: string; + price_r: { numerator: number, denominator: number }; + selling_asset_type: ASSET_TYPE; + selling_asset_code?: string; + selling_asset_issuer?: string; + } + interface SetOptionsOperationResponse extends BaseOperationResponse { + signer_key?: string; + signer_weight?: number; + master_key_weight?: number; + low_threshold?: number; + med_threshold?: number; + high_threshold?: number; + home_domain?: string; + set_flags: Array<(1 | 2)>; + set_flags_s: Array<('auth_required_flag' | 'auth_revocable_flag')>; + clear_flags: Array<(1 | 2)>; + clear_flags_s: Array<('auth_required_flag' | 'auth_revocable_flag')>; + } + interface ChangeTrustOperationResponse extends BaseOperationResponse { + asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_code: string; + asset_issuer: string; + trustee: string; + trustor: string; + limit: string; + } + interface AllowTrustOperationResponse extends BaseOperationResponse { + asset_type: ASSET_TYPE; + asset_code: string; + asset_issuer: string; + authorize: boolean; + trustee: string; + trustor: string; + } + interface AccountMergeOperationResponse extends BaseOperationResponse { + into: string; + } + interface InflationOperationResponse extends BaseOperationResponse { + } + interface ManageDataOperationResponse extends BaseOperationResponse { + name: string; + value: Buffer; + } + interface BumpSequenceOperationResponse extends BaseOperationResponse { + bump_to: string; + } + + interface ResponseCollection { + _links: { + self: ResponseLink + next: ResponseLink + prev: ResponseLink + }; + _embedded: { + records: T[] + }; + } + interface TransactionResponseCollection extends ResponseCollection {} +} From 2606bbe34763b168850ba63fc3cfb674f0fa6a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Tue, 8 Jan 2019 15:28:39 +0200 Subject: [PATCH 07/19] refactor: consolidate dublicate types --- types/stellar-sdk/index.d.ts | 115 ++++++++++------------------------- 1 file changed, 33 insertions(+), 82 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index ba22de39d3..6bb8136e58 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -512,7 +512,7 @@ export class Server { } export namespace Server { - abstract class CallBuilder { + abstract class CallBuilder { constructor(serverUrl: string) call(): Promise>; cursor(cursor: string): this; @@ -521,29 +521,18 @@ export namespace Server { stream(options?: { onmessage?: (record: T) => void, onerror?: (error: Error) => void }): () => void; } - interface CollectionPage { + interface CollectionPage { records: T[]; next: () => Promise>; prev: () => Promise>; } - interface Record { - _links: { - [key: string]: RecordLink - }; - } - - interface RecordLink { - href: string; - templated?: boolean; - } - /* Due to a bug with the recursive function requests */ - interface CollectionRecord { + interface CollectionRecord { _links: { - next: RecordLink - prev: RecordLink - self: RecordLink + next: Horizon.ResponseLink + prev: Horizon.ResponseLink + self: Horizon.ResponseLink }; _embedded: { records: T[] @@ -556,42 +545,19 @@ export namespace Server { order?: 'asc' | 'desc'; } - type CallFunction = () => Promise; - type CallCollectionFunction = + type CallFunction = () => Promise; + type CallCollectionFunction = (options?: CallFunctionTemplateOptions) => Promise>; - interface BalanceLineNative { - balance: string; - asset_type: ASSET_TYPE.native; - } - interface BalanceLineAsset { - balance: string; - limit: string; - asset_type: T; - asset_code: string; - asset_issuer: string; - } - type BalanceLine = - T extends ASSET_TYPE.native ? BalanceLineNative : - T extends ASSET_TYPE.credit4 | ASSET_TYPE.credit12 ? BalanceLineAsset : - BalanceLineNative | BalanceLineAsset; - - interface AccountRecord extends Record { + interface AccountRecord extends Horizon.BaseResponse { id: string; paging_token: string; account_id: string; sequence: number; subentry_count: number; - thresholds: { - low_threshold: number - med_threshold: number - high_threshold: number - }; - flags: { - auth_required: boolean - auth_revocable: boolean - }; - balances: BalanceLine[]; + thresholds: Horizon.AccountThresholds; + flags: Horizon.Flags; + balances: Horizon.BalanceLine[]; signers: Array< { public_key: string @@ -609,20 +575,17 @@ export namespace Server { trades: CallCollectionFunction; } - interface AssetRecord extends Record { + interface AssetRecord extends Horizon.BaseResponse { asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; asset_code: string; asset_issuer: string; paging_token: string; amount: string; num_accounts: number; - flags: { - auth_required: boolean - auth_revocable: boolean - }; + flags: Horizon.Flags; } - interface EffectRecord extends Record { + interface EffectRecord extends Horizon.BaseResponse { account: string; paging_token: string; starting_balance: string; @@ -635,7 +598,7 @@ export namespace Server { succeeds?: CallFunction; } - interface LedgerRecord extends Record { + interface LedgerRecord extends Horizon.BaseResponse { id: string; paging_token: string; hash: string; @@ -660,7 +623,7 @@ export namespace Server { transactions: CallCollectionFunction; } - interface OfferRecord extends Record { + interface OfferRecord extends Horizon.BaseResponse { id: string; paging_token: string; seller_attr: string; @@ -715,14 +678,14 @@ export namespace Server { | ManageDataOperationRecord | BumpSequenceOperationRecord; - interface OrderbookRecord extends Record { + interface OrderbookRecord extends Horizon.BaseResponse { bids: Array<{ price_r: {}, price: number, amount: string }>; asks: Array<{ price_r: {}, price: number, amount: string }>; selling: Asset; buying: Asset; } - interface PaymentPathRecord extends Record { + interface PaymentPathRecord extends Horizon.BaseResponse { path: Array<{ asset_code: string asset_issuer: string @@ -738,7 +701,7 @@ export namespace Server { destination_asset_issuer: string; } - interface TradeRecord extends Record { + interface TradeRecord extends Horizon.BaseResponse { id: string; paging_token: string; ledger_close_time: string; @@ -759,7 +722,7 @@ export namespace Server { operation: CallFunction; } - interface TradeAggregationRecord extends Record { + interface TradeAggregationRecord extends Horizon.BaseResponse { timestamp: string; trade_count: number; base_volume: string; @@ -771,7 +734,7 @@ export namespace Server { close: string; } - interface TransactionRecord extends Record { + interface TransactionRecord extends Horizon.BaseResponse { id: string; paging_token: string; hash: string; @@ -804,28 +767,16 @@ export namespace Server { accountId(id: string): this; } class AccountResponse implements AccountRecord { - _links: { [key: string]: { href: string } }; + _links: { [key in 'self']: Horizon.ResponseLink }; id: string; paging_token: string; account_id: string; sequence: number; subentry_count: number; - thresholds: { - low_threshold: number - med_threshold: number - high_threshold: number - }; - flags: { - auth_required: boolean - auth_revocable: boolean - }; - balances: BalanceLine[]; - signers: Array< - { - public_key: string - weight: number - } - >; + thresholds: Horizon.AccountThresholds; + flags: Horizon.Flags; + balances: Horizon.BalanceLine[]; + signers: Horizon.AccountSigner[]; data: (options: {value: string}) => Promise<{value: string}>; data_attr: { [key: string]: string @@ -963,16 +914,16 @@ export namespace Horizon { T extends ASSET_TYPE.credit4 | ASSET_TYPE.credit12 ? BalanceLineAsset : BalanceLineNative | BalanceLineAsset; - interface AccountResponseThresholds { + interface AccountThresholds { low_threshold: number; med_threshold: number; high_threshold: number; } - interface AccountResponseFlags { + interface Flags { auth_required: boolean; auth_revocable: boolean; } - interface AccountResponseSigner { + interface AccountSigner { public_key: string; weight: number; } @@ -982,10 +933,10 @@ export namespace Horizon { account_id: string; sequence: string; subentry_count: number; - thresholds: AccountResponseThresholds; - flags: AccountResponseFlags; + thresholds: AccountThresholds; + flags: Flags; balances: BalanceLine[]; - signers: AccountResponseSigner[]; + signers: AccountSigner[]; data: { [key: string]: string }; From 158a9090710870b620b3b9a76eb1b92b8a45b4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Tue, 8 Jan 2019 15:40:50 +0200 Subject: [PATCH 08/19] feat: interface PriceR --- types/stellar-sdk/index.d.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 6bb8136e58..5180c2e14d 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -630,7 +630,7 @@ export namespace Server { selling: Asset; buying: Asset; amount: string; - price_r: { numerator: number, denominator: number }; + price_r: Horizon.PriceR; price: string; seller?: CallFunction; @@ -914,6 +914,10 @@ export namespace Horizon { T extends ASSET_TYPE.credit4 | ASSET_TYPE.credit12 ? BalanceLineAsset : BalanceLineNative | BalanceLineAsset; + interface PriceR { + numerator: number; + denominator: number; + } interface AccountThresholds { low_threshold: number; med_threshold: number; @@ -1015,7 +1019,7 @@ export namespace Horizon { buying_asset_code?: string; buying_asset_issuer?: string; price: string; - price_r: { numerator: number, denominator: number }; + price_r: PriceR; selling_asset_type: ASSET_TYPE; selling_asset_code?: string; selling_asset_issuer?: string; @@ -1027,7 +1031,7 @@ export namespace Horizon { buying_asset_code?: string; buying_asset_issuer?: string; price: string; - price_r: { numerator: number, denominator: number }; + price_r: PriceR; selling_asset_type: ASSET_TYPE; selling_asset_code?: string; selling_asset_issuer?: string; From 0db26808f992714424374ffb7c7b420da3acee64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Wed, 9 Jan 2019 11:51:16 +0200 Subject: [PATCH 09/19] fix: TransactionRecord --- types/stellar-sdk/index.d.ts | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 5180c2e14d..a5692f56f4 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -11,6 +11,11 @@ /// +// Dependency +export type Key = string|number|symbol; +export type Diff = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; +export type Omit = Pick>; + export namespace StellarBase { class Account { constructor(accountId: string, sequence: string | number) @@ -734,25 +739,8 @@ export namespace Server { close: string; } - interface TransactionRecord extends Horizon.BaseResponse { - id: string; - paging_token: string; - hash: string; - ledger_attr: number; - created_at: string; - max_fee: number; - fee_paid: number; - operation_count: number; - result_code: number; - result_code_s: string; - source_account: string; - source_account_sequence: string; - envelope_xdr: string; - result_xdr: string; - result_meta_xdr: string; - memo: string; - envelope: any; - memo_type: any; + interface TransactionRecord extends Omit { + ledger_attr: Horizon.TransactionResponse['ledger']; account: CallFunction; effects: CallCollectionFunction; @@ -888,7 +876,8 @@ export namespace Horizon { hash: string; id: string; ledger: number; - memo_type: string; + memo_type: Memo.AnyType; + memo?: string; operation_count: number; paging_token: string; result_meta_xdr: string; From 73845813f9d05cbc6bca9a2a8a5964119ea180e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Wed, 9 Jan 2019 19:55:00 +0200 Subject: [PATCH 10/19] feat: parameterize Transaction --- types/stellar-sdk/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index a5692f56f4..ca2fbc4651 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -371,7 +371,7 @@ export namespace StellarBase { function decodeSha256Hash(data: string): Buffer; } - class Transaction { + class Transaction { constructor(envelope: string | xdr.TransactionEnvelope) hash(): Buffer; sign(...keypairs: Keypair[]): void; @@ -379,11 +379,11 @@ export namespace StellarBase { signHashX(preimage: Buffer | string): void; toEnvelope(): xdr.TransactionEnvelope; - operations: TransactionOperation[]; + operations: TOps; sequence: number; fee: number; source: string; - memo: Memo; + memo: TMemo; signatures: xdr.DecoratedSignature[]; } From 875ec553cffcdc35cbfa0728096de49afc7b22fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Wed, 9 Jan 2019 20:02:58 +0200 Subject: [PATCH 11/19] fix: linter & tests --- types/stellar-sdk/index.d.ts | 2 +- types/stellar-sdk/stellar-sdk-tests.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index ca2fbc4651..d632b6c0a2 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -7,7 +7,7 @@ // Timur Ramazanov // Kalvis Kalniņš // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 2.9 /// diff --git a/types/stellar-sdk/stellar-sdk-tests.ts b/types/stellar-sdk/stellar-sdk-tests.ts index c4dcbda7c8..143ccdaba4 100644 --- a/types/stellar-sdk/stellar-sdk-tests.ts +++ b/types/stellar-sdk/stellar-sdk-tests.ts @@ -6,8 +6,7 @@ const account = new StellarSdk.Account(sourceKey.publicKey(), '1'); const transaction = new StellarSdk.TransactionBuilder(account) .addOperation(StellarSdk.Operation.accountMerge({destination: destKey.publicKey()})) .addMemo(new StellarSdk.Memo(StellarSdk.MemoText, "memo")) - .build(); // $ExpectType () => Transaction -transaction; // $ExpectType Transaction + .build(); // $ExpectType () => Transaction StellarSdk.StellarTomlResolver.resolve("example.com", {allowHttp: true, timeout: 100}) .then(toml => toml.FEDERATION_SERVER); From 1d7a611d80963df33626ad4cf109e309239645e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 09:35:07 +0200 Subject: [PATCH 12/19] fix: Transaction Memo type --- types/stellar-sdk/index.d.ts | 2 +- types/stellar-sdk/stellar-sdk-tests.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index d632b6c0a2..bd28677bf0 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -371,7 +371,7 @@ export namespace StellarBase { function decodeSha256Hash(data: string): Buffer; } - class Transaction { + class Transaction { constructor(envelope: string | xdr.TransactionEnvelope) hash(): Buffer; sign(...keypairs: Keypair[]): void; diff --git a/types/stellar-sdk/stellar-sdk-tests.ts b/types/stellar-sdk/stellar-sdk-tests.ts index 143ccdaba4..2533fefaa4 100644 --- a/types/stellar-sdk/stellar-sdk-tests.ts +++ b/types/stellar-sdk/stellar-sdk-tests.ts @@ -6,7 +6,7 @@ const account = new StellarSdk.Account(sourceKey.publicKey(), '1'); const transaction = new StellarSdk.TransactionBuilder(account) .addOperation(StellarSdk.Operation.accountMerge({destination: destKey.publicKey()})) .addMemo(new StellarSdk.Memo(StellarSdk.MemoText, "memo")) - .build(); // $ExpectType () => Transaction + .build(); // $ExpectType () => Transaction, TransactionOperation[]> StellarSdk.StellarTomlResolver.resolve("example.com", {allowHttp: true, timeout: 100}) .then(toml => toml.FEDERATION_SERVER); From 4ede6d6049ab82bcd711e72b2ee47d0c52754e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 10:31:50 +0200 Subject: [PATCH 13/19] feat: new module stellar-base --- types/stellar-base/index.d.ts | 439 ++++++++++++++++++++ types/stellar-base/stellar-base-tests.ts | 40 ++ types/stellar-base/tsconfig.json | 23 ++ types/stellar-base/tslint.json | 3 + types/stellar-sdk/index.d.ts | 488 ++--------------------- types/stellar-sdk/stellar-sdk-tests.ts | 39 -- 6 files changed, 537 insertions(+), 495 deletions(-) create mode 100644 types/stellar-base/index.d.ts create mode 100644 types/stellar-base/stellar-base-tests.ts create mode 100644 types/stellar-base/tsconfig.json create mode 100644 types/stellar-base/tslint.json diff --git a/types/stellar-base/index.d.ts b/types/stellar-base/index.d.ts new file mode 100644 index 0000000000..4b86065c40 --- /dev/null +++ b/types/stellar-base/index.d.ts @@ -0,0 +1,439 @@ +// Type definitions for stellar-base 0.10 +// Project: https://github.com/stellar/js-stellar-sdk +// Definitions by: Carl Foster +// Triston Jones +// Paul Selden +// Max Bause +// Timur Ramazanov +// Kalvis Kalniņš +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.9 + +/// + +export class Account { + constructor(accountId: string, sequence: string | number) + accountId(): string; + sequenceNumber(): string; + incrementSequenceNumber(): void; +} + +export enum ASSET_TYPE { + native = 'native', + credit4 = 'credit_alphanum4', + credit12 = 'credit_alphanum12', +} +export class Asset { + static native(): Asset; + static fromOperation(xdr: xdr.Asset): Asset; + + constructor(code: string, issuer: string); + + getCode(): string; + getIssuer(): string; + getAssetType(): ASSET_TYPE; + isNative(): boolean; + equals(other: Asset): boolean; + toXDRObject(): xdr.Asset; + + code: string; + issuer: string; +} + +export const FastSigning: boolean; + +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; + static random(): Keypair; + + constructor(keys: { type: 'ed25519', secretKey: string } | { type: 'ed25519', Key: string }) + + publicKey(): string; + secret(): string; + rawPublicKey(): Buffer; + rawSecretKey(): Buffer; + canSign(): boolean; + sign(data: Buffer): Buffer; + signDecorated(data: Buffer): xdr.DecoratedSignature; + signatureHint(): xdr.SignatureHint; + verify(data: Buffer, signature: Buffer): boolean; +} + +export const MemoNone = 'none'; +export const MemoID = 'id'; +export const MemoText = 'text'; +export const MemoHash = 'hash'; +export const MemoReturn = 'return'; +export type MemoNone = 'none'; +export type MemoID = 'id'; +export type MemoText = 'text'; +export type MemoHash = 'hash'; +export type MemoReturn = 'return'; +export class Memo { + static fromXDRObject(memo: xdr.Memo): Memo; + static hash(hash: string): Memo; + static id(id: string): Memo; + static none(): Memo; + static return(hash: string): Memo; + static text(text: string): Memo; + + constructor(type: MemoNone, value?: null); + constructor(type: MemoHash | MemoReturn, value: Buffer) + constructor(type: MemoHash | MemoReturn | MemoID | MemoText, value: string) + constructor(type: T, value: Memo.AnyValue) + + type: T; + value: + T extends MemoNone ? null : + T extends MemoID ? string : + T extends MemoText ? string : + T extends MemoHash ? Buffer : + T extends MemoReturn ? Buffer : + Memo.AnyValue; + + toXDRObject(): xdr.Memo; +} +export namespace Memo { + type AnyType = MemoNone | MemoID | MemoText | MemoHash | MemoReturn; + type AnyValue = null | string | Buffer; +} + +export enum Networks { + PUBLIC = 'Public Global Stellar Network ; September 2015', + TESTNET = 'Test SDF Network ; September 2015', +} + +export class Network { + static use(network: Network): void; + static usePublicNetwork(): void; + static useTestNetwork(): void; + static current(): Network; + + constructor(passphrase: string) + + networkPassphrase(): string; + networkId(): string; +} + +export const AuthRequiredFlag: 1; +export const AuthRevocableFlag: 2; +export const AuthImmutableFlag: 4; + +export type TransactionOperation = + Operation.CreateAccount + | Operation.Payment + | Operation.PathPayment + | Operation.CreatePassiveOffer + | Operation.ManageOffer + | Operation.SetOptions + | Operation.ChangeTrust + | Operation.AllowTrust + | Operation.AccountMerge + | Operation.Inflation + | Operation.ManageData + | Operation.BumpSequence; + +export enum OperationType { + createAccount = 'createAccount', + payment = 'payment', + pathPayment = 'pathPayment', + createPassiveOffer = 'createPassiveOffer', + manageOffer = 'manageOffer', + setOptions = 'setOptions', + changeTrust = 'changeTrust', + allowTrust = 'allowTrust', + accountMerge = 'accountMerge', + inflation = 'inflation', + manageData = 'manageData', + bumpSequence = 'bumpSequence', +} + +export namespace Operation { + interface Operation { + type: T; + source?: string; + } + interface OperationOptions { + source?: string; + } + + interface AccountMerge extends Operation { + destination: string; + } + interface AccountMergeOptions extends OperationOptions { + destination: string; + } + function accountMerge(options: AccountMergeOptions): xdr.Operation; + + interface AllowTrust extends Operation { + trustor: string; + assetCode: string; + authorize: boolean | undefined; + } + interface AllowTrustOptions extends OperationOptions { + trustor: string; + assetCode: string; + authorize?: boolean; + } + function allowTrust(options: AllowTrustOptions): xdr.Operation; + + interface ChangeTrust extends Operation { + line: Asset; + limit: string; + } + interface ChangeTrustOptions extends OperationOptions { + asset: Asset; + limit?: string; + } + function changeTrust(options: ChangeTrustOptions): xdr.Operation; + + interface CreateAccount extends Operation { + destination: string; + startingBalance: string; + } + interface CreateAccountOptions extends OperationOptions { + destination: string; + startingBalance: string; + } + function createAccount(options: CreateAccountOptions): xdr.Operation; + + interface CreatePassiveOffer extends Operation { + selling: Asset; + buying: Asset; + amount: string; + price: string; + } + interface CreatePassiveOfferOptions extends OperationOptions { + selling: Asset; + buying: Asset; + amount: string; + price: number | string | object /* bignumber.js */; + } + function createPassiveOffer(options: CreatePassiveOfferOptions): xdr.Operation; + + interface Inflation extends Operation { + } + interface InflationOptions extends OperationOptions { // tslint:disable-line + } + function inflation(options: InflationOptions): xdr.Operation; + + interface ManageData extends Operation { + name: string; + value: Buffer; + } + interface ManageDataOptions extends OperationOptions { + name: string; + value: string | Buffer; + } + function manageData(options: ManageDataOptions): xdr.Operation; + + interface ManageOffer extends Operation { + selling: Asset; + buying: Asset; + amount: string; + price: string; + offerId: string; + } + interface ManageOfferOptions extends CreatePassiveOfferOptions { + offerId?: number | string; + } + function manageOffer(options: ManageOfferOptions): xdr.Operation; + + interface PathPayment extends Operation { + sendAsset: Asset; + sendMax: string; + destination: string; + destAsset: Asset; + destAmount: string; + path: Asset[]; + } + interface PathPaymentOptions extends OperationOptions { + sendAsset: Asset; + sendMax: string; + destination: string; + destAsset: Asset; + destAmount: string; + path?: Asset[]; + } + function pathPayment(options: PathPaymentOptions): xdr.Operation; + + interface Payment extends Operation { + amount: string; + asset: Asset; + destination: string; + } + interface PaymentOptions extends OperationOptions { + amount: string; + asset: Asset; + destination: string; + } + function payment(options: PaymentOptions): xdr.Operation; + + /* + * Required = 1 << 0 + * Revocable = 1 << 1 + * Immutable = 1 << 2 + */ + enum AuthFlags { + Required = 1, + Revocable = 2, + Immutable = 4, + } + interface SignerEd25519PublicKey { + ed25519PublicKey: string; + weight: number | undefined; + } + interface SignerSha256Hash { + sha256Hash: Buffer; + weight: number | undefined; + } + interface SignerPreAuthTx { + preAuthTx: Buffer; + weight: number | undefined; + } + type Signer = SignerEd25519PublicKey | SignerSha256Hash | SignerPreAuthTx; + interface SignerEd25519PublicKeyOptions { + ed25519PublicKey: string; + weight?: number | string; + } + interface SignerSha256HashOptions { + sha256Hash: Buffer | string; + weight?: number | string; + } + interface SignerPreAuthTxOptions { + preAuthTx: Buffer | string; + weight?: number | string; + } + type SignerOptions = SignerEd25519PublicKeyOptions | SignerSha256HashOptions | SignerPreAuthTxOptions; + type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; + interface SetOptions extends Operation { + inflationDest?: string; + clearFlags?: AuthFlags; + setFlags?: AuthFlags; + masterWeight?: number; + lowThreshold?: number; + medThreshold?: number; + highThreshold?: number; + homeDomain?: string; + signer: + T extends {ed25519PublicKey: any} ? SignerEd25519PublicKey : + T extends {sha256Hash: any} ? SignerSha256Hash : + T extends {preAuthTx: any} ? SignerPreAuthTx : + never; + } + interface SetOptionsOptions extends OperationOptions { + inflationDest?: string; + clearFlags?: AuthFlags; + setFlags?: AuthFlags; + masterWeight?: number | string; + lowThreshold?: number | string; + medThreshold?: number | string; + highThreshold?: number | string; + homeDomain?: string; + signer?: T; + } + function setOptions(options: SetOptionsOptions): xdr.Operation>; + + interface BumpSequence extends Operation { + bumpTo: string; + } + interface BumpSequenceOptions extends OperationOptions { + bumpTo: string; + } + function bumpSequence(options: BumpSequenceOptions): xdr.Operation; + + function fromXDRObject(xdrOperation: xdr.Operation): T; +} + +export namespace StrKey { + function encodeEd25519PublicKey(data: Buffer): string; + function decodeEd25519PublicKey(data: string): Buffer; + function isValidEd25519PublicKey(Key: string): boolean; + + function encodeEd25519SecretSeed(data: Buffer): string; + function decodeEd25519SecretSeed(data: string): Buffer; + function isValidEd25519SecretSeed(seed: string): boolean; + + function encodePreAuthTx(data: Buffer): string; + function decodePreAuthTx(data: string): Buffer; + + function encodeSha256Hash(data: Buffer): string; + function decodeSha256Hash(data: string): Buffer; +} + +export class Transaction { + constructor(envelope: string | xdr.TransactionEnvelope) + hash(): Buffer; + sign(...keypairs: Keypair[]): void; + signatureBase(): Buffer; + signHashX(preimage: Buffer | string): void; + toEnvelope(): xdr.TransactionEnvelope; + + operations: TOps; + sequence: number; + fee: number; + source: string; + memo: TMemo; + signatures: xdr.DecoratedSignature[]; +} + +export class TransactionBuilder { + constructor(sourceAccount: Account, options?: TransactionBuilder.TransactionBuilderOptions) + addOperation(operation: xdr.Operation): this; + addMemo(memo: Memo): this; + build(): Transaction; +} + +export namespace TransactionBuilder { + interface TransactionBuilderOptions { + fee?: number; + timebounds?: { + minTime?: number | string + maxTime?: number | string + }; + memo?: Memo; + } +} + +export namespace xdr { + class XDRStruct { + static fromXDR(xdr: Buffer): XDRStruct; + + toXDR(base?: string): Buffer; + toXDR(encoding: string): string; + } + 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 { + static fromXDR(xdr: Buffer): DecoratedSignature; + + 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 function hash(data: Buffer): Buffer; +export function sign(data: Buffer, rawSecret: Buffer): Buffer; +export function verify(data: Buffer, signature: Buffer, rawPublicKey: Buffer): boolean; diff --git a/types/stellar-base/stellar-base-tests.ts b/types/stellar-base/stellar-base-tests.ts new file mode 100644 index 0000000000..0a8176919c --- /dev/null +++ b/types/stellar-base/stellar-base-tests.ts @@ -0,0 +1,40 @@ +import * as StellarSdk from 'stellar-base'; + +const sourceKey = StellarSdk.Keypair.random(); // $ExpectType Keypair +const destKey = StellarSdk.Keypair.random(); +const account = new StellarSdk.Account(sourceKey.publicKey(), '1'); +const transaction = new StellarSdk.TransactionBuilder(account) + .addOperation(StellarSdk.Operation.accountMerge({destination: destKey.publicKey()})) + .addMemo(new StellarSdk.Memo(StellarSdk.MemoText, "memo")) + .build(); // $ExpectType () => Transaction, TransactionOperation[]> + +const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)); // $ExpectType DecoratedSignature +sig.hint(); // $ExpectType Buffer +sig.signature(); // $ExpectType Buffer + +StellarSdk.Memo.none(); // $ExpectType Memo<"none"> +StellarSdk.Memo.text('asdf'); // $ExpectType Memo<"text"> +StellarSdk.Memo.id('asdf'); // $ExpectType Memo<"id"> +StellarSdk.Memo.return('asdf'); // $ExpectType Memo<"return"> +StellarSdk.Memo.hash('asdf'); // $ExpectType Memo<"hash"> +StellarSdk.Memo.none().value; // $ExpectType null +StellarSdk.Memo.id('asdf').value; // $ExpectType string +StellarSdk.Memo.text('asdf').value; // $ExpectType string +StellarSdk.Memo.return('asdf').value; // $ExpectType Buffer +StellarSdk.Memo.hash('asdf').value; // $ExpectType Buffer + +// P.S. don't use Memo constructor +(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).value; // $ExpectType AnyValue +(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType AnyType + +const noSignerXDR = StellarSdk.Operation.setOptions({lowThreshold: 1}); +StellarSdk.Operation.fromXDRObject(noSignerXDR).signer; // $ExpectType never + +const newSignerXDR1 = StellarSdk.Operation.setOptions({signer: {ed25519PublicKey: sourceKey.publicKey(), weight: '1'}}); +StellarSdk.Operation.fromXDRObject(newSignerXDR1).signer; // $ExpectType SignerEd25519PublicKey + +const newSignerXDR2 = StellarSdk.Operation.setOptions({signer: {sha256Hash: Buffer.from(''), weight: '1'}}); +StellarSdk.Operation.fromXDRObject(newSignerXDR2).signer; // $ExpectType SignerSha256Hash + +const newSignerXDR3 = StellarSdk.Operation.setOptions({signer: {preAuthTx: '', weight: 1}}); +StellarSdk.Operation.fromXDRObject(newSignerXDR3).signer; // $ExpectType SignerPreAuthTx diff --git a/types/stellar-base/tsconfig.json b/types/stellar-base/tsconfig.json new file mode 100644 index 0000000000..ea8f34b1d4 --- /dev/null +++ b/types/stellar-base/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "stellar-base-tests.ts" + ] +} diff --git a/types/stellar-base/tslint.json b/types/stellar-base/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/stellar-base/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index bd28677bf0..d0f3d86023 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -11,464 +11,40 @@ /// -// Dependency -export type Key = string|number|symbol; -export type Diff = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; -export type Omit = Pick>; - -export namespace StellarBase { - class Account { - constructor(accountId: string, sequence: string | number) - accountId(): string; - sequenceNumber(): string; - incrementSequenceNumber(): void; - } - - enum ASSET_TYPE { - native = 'native', - credit4 = 'credit_alphanum4', - credit12 = 'credit_alphanum12', - } - class Asset { - static native(): Asset; - static fromOperation(xdr: xdr.Asset): Asset; - - constructor(code: string, issuer: string); - - getCode(): string; - getIssuer(): string; - getAssetType(): ASSET_TYPE; - isNative(): boolean; - equals(other: Asset): boolean; - toXDRObject(): xdr.Asset; - - code: string; - issuer: string; - } - - const FastSigning: boolean; - - 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; - static random(): Keypair; - - constructor(keys: { type: 'ed25519', secretKey: string } | { type: 'ed25519', Key: string }) - - publicKey(): string; - secret(): string; - rawPublicKey(): Buffer; - rawSecretKey(): Buffer; - canSign(): boolean; - sign(data: Buffer): Buffer; - signDecorated(data: Buffer): xdr.DecoratedSignature; - signatureHint(): xdr.SignatureHint; - verify(data: Buffer, signature: Buffer): boolean; - } - - const MemoNone = 'none'; - const MemoID = 'id'; - const MemoText = 'text'; - const MemoHash = 'hash'; - const MemoReturn = 'return'; - type MemoNone = 'none'; - type MemoID = 'id'; - type MemoText = 'text'; - type MemoHash = 'hash'; - type MemoReturn = 'return'; - class Memo { - static fromXDRObject(memo: xdr.Memo): Memo; - static hash(hash: string): Memo; - static id(id: string): Memo; - static none(): Memo; - static return(hash: string): Memo; - static text(text: string): Memo; - - constructor(type: MemoNone, value?: null); - constructor(type: MemoHash | MemoReturn, value: Buffer) - constructor(type: MemoHash | MemoReturn | MemoID | MemoText, value: string) - constructor(type: T, value: Memo.AnyValue) - - type: T; - value: - T extends MemoNone ? null : - T extends MemoID ? string : - T extends MemoText ? string : - T extends MemoHash ? Buffer : - T extends MemoReturn ? Buffer : - Memo.AnyValue; - - toXDRObject(): xdr.Memo; - } - namespace Memo { - type AnyType = MemoNone | MemoID | MemoText | MemoHash | MemoReturn; - type AnyValue = null | string | Buffer; - } - - enum Networks { - PUBLIC = 'Public Global Stellar Network ; September 2015', - TESTNET = 'Test SDF Network ; September 2015', - } - - class Network { - static use(network: Network): void; - static usePublicNetwork(): void; - static useTestNetwork(): void; - static current(): Network; - - constructor(passphrase: string) - - networkPassphrase(): string; - networkId(): string; - } - - const AuthRequiredFlag: 1; - const AuthRevocableFlag: 2; - const AuthImmutableFlag: 4; - - type TransactionOperation = - Operation.CreateAccount - | Operation.Payment - | Operation.PathPayment - | Operation.CreatePassiveOffer - | Operation.ManageOffer - | Operation.SetOptions - | Operation.ChangeTrust - | Operation.AllowTrust - | Operation.AccountMerge - | Operation.Inflation - | Operation.ManageData - | Operation.BumpSequence; - - enum OperationType { - createAccount = 'createAccount', - payment = 'payment', - pathPayment = 'pathPayment', - createPassiveOffer = 'createPassiveOffer', - manageOffer = 'manageOffer', - setOptions = 'setOptions', - changeTrust = 'changeTrust', - allowTrust = 'allowTrust', - accountMerge = 'accountMerge', - inflation = 'inflation', - manageData = 'manageData', - bumpSequence = 'bumpSequence', - } - - namespace Operation { - interface Operation { - type: T; - source?: string; - } - interface OperationOptions { - source?: string; - } - - interface AccountMerge extends Operation { - destination: string; - } - interface AccountMergeOptions extends OperationOptions { - destination: string; - } - function accountMerge(options: AccountMergeOptions): xdr.Operation; - - interface AllowTrust extends Operation { - trustor: string; - assetCode: string; - authorize: boolean | undefined; - } - interface AllowTrustOptions extends OperationOptions { - trustor: string; - assetCode: string; - authorize?: boolean; - } - function allowTrust(options: AllowTrustOptions): xdr.Operation; - - interface ChangeTrust extends Operation { - line: Asset; - limit: string; - } - interface ChangeTrustOptions extends OperationOptions { - asset: Asset; - limit?: string; - } - function changeTrust(options: ChangeTrustOptions): xdr.Operation; - - interface CreateAccount extends Operation { - destination: string; - startingBalance: string; - } - interface CreateAccountOptions extends OperationOptions { - destination: string; - startingBalance: string; - } - function createAccount(options: CreateAccountOptions): xdr.Operation; - - interface CreatePassiveOffer extends Operation { - selling: Asset; - buying: Asset; - amount: string; - price: string; - } - interface CreatePassiveOfferOptions extends OperationOptions { - selling: Asset; - buying: Asset; - amount: string; - price: number | string | object /* bignumber.js */; - } - function createPassiveOffer(options: CreatePassiveOfferOptions): xdr.Operation; - - interface Inflation extends Operation { - } - interface InflationOptions extends OperationOptions { // tslint:disable-line - } - function inflation(options: InflationOptions): xdr.Operation; - - interface ManageData extends Operation { - name: string; - value: Buffer; - } - interface ManageDataOptions extends OperationOptions { - name: string; - value: string | Buffer; - } - function manageData(options: ManageDataOptions): xdr.Operation; - - interface ManageOffer extends Operation { - selling: Asset; - buying: Asset; - amount: string; - price: string; - offerId: string; - } - interface ManageOfferOptions extends CreatePassiveOfferOptions { - offerId?: number | string; - } - function manageOffer(options: ManageOfferOptions): xdr.Operation; - - interface PathPayment extends Operation { - sendAsset: Asset; - sendMax: string; - destination: string; - destAsset: Asset; - destAmount: string; - path: Asset[]; - } - interface PathPaymentOptions extends OperationOptions { - sendAsset: Asset; - sendMax: string; - destination: string; - destAsset: Asset; - destAmount: string; - path?: Asset[]; - } - function pathPayment(options: PathPaymentOptions): xdr.Operation; - - interface Payment extends Operation { - amount: string; - asset: Asset; - destination: string; - } - interface PaymentOptions extends OperationOptions { - amount: string; - asset: Asset; - destination: string; - } - function payment(options: PaymentOptions): xdr.Operation; - - /* - * Required = 1 << 0 - * Revocable = 1 << 1 - * Immutable = 1 << 2 - */ - enum AuthFlags { - Required = 1, - Revocable = 2, - Immutable = 4, - } - interface SignerEd25519PublicKey { - ed25519PublicKey: string; - weight: number | undefined; - } - interface SignerSha256Hash { - sha256Hash: Buffer; - weight: number | undefined; - } - interface SignerPreAuthTx { - preAuthTx: Buffer; - weight: number | undefined; - } - type Signer = SignerEd25519PublicKey | SignerSha256Hash | SignerPreAuthTx; - interface SignerEd25519PublicKeyOptions { - ed25519PublicKey: string; - weight?: number | string; - } - interface SignerSha256HashOptions { - sha256Hash: Buffer | string; - weight?: number | string; - } - interface SignerPreAuthTxOptions { - preAuthTx: Buffer | string; - weight?: number | string; - } - type SignerOptions = SignerEd25519PublicKeyOptions | SignerSha256HashOptions | SignerPreAuthTxOptions; - type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; - interface SetOptions extends Operation { - inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; - masterWeight?: number; - lowThreshold?: number; - medThreshold?: number; - highThreshold?: number; - homeDomain?: string; - signer: - T extends {ed25519PublicKey: any} ? SignerEd25519PublicKey : - T extends {sha256Hash: any} ? SignerSha256Hash : - T extends {preAuthTx: any} ? SignerPreAuthTx : - never; - } - interface SetOptionsOptions extends OperationOptions { - inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; - masterWeight?: number | string; - lowThreshold?: number | string; - medThreshold?: number | string; - highThreshold?: number | string; - homeDomain?: string; - signer?: T; - } - function setOptions(options: SetOptionsOptions): xdr.Operation>; - - interface BumpSequence extends Operation { - bumpTo: string; - } - interface BumpSequenceOptions extends OperationOptions { - bumpTo: string; - } - function bumpSequence(options: BumpSequenceOptions): xdr.Operation; - - function fromXDRObject(xdrOperation: xdr.Operation): T; - } - - namespace StrKey { - function encodeEd25519PublicKey(data: Buffer): string; - function decodeEd25519PublicKey(data: string): Buffer; - function isValidEd25519PublicKey(Key: string): boolean; - - function encodeEd25519SecretSeed(data: Buffer): string; - function decodeEd25519SecretSeed(data: string): Buffer; - function isValidEd25519SecretSeed(seed: string): boolean; - - function encodePreAuthTx(data: Buffer): string; - function decodePreAuthTx(data: string): Buffer; - - function encodeSha256Hash(data: Buffer): string; - function decodeSha256Hash(data: string): Buffer; - } - - class Transaction { - constructor(envelope: string | xdr.TransactionEnvelope) - hash(): Buffer; - sign(...keypairs: Keypair[]): void; - signatureBase(): Buffer; - signHashX(preimage: Buffer | string): void; - toEnvelope(): xdr.TransactionEnvelope; - - operations: TOps; - sequence: number; - fee: number; - source: string; - memo: TMemo; - signatures: xdr.DecoratedSignature[]; - } - - class TransactionBuilder { - constructor(sourceAccount: Account, options?: TransactionBuilder.TransactionBuilderOptions) - addOperation(operation: xdr.Operation): this; - addMemo(memo: Memo): this; - build(): Transaction; - } - - namespace TransactionBuilder { - interface TransactionBuilderOptions { - fee?: number; - timebounds?: { - minTime?: number | string - maxTime?: number | string - }; - memo?: Memo; - } - } - - namespace xdr { - class XDRStruct { - static fromXDR(xdr: Buffer): XDRStruct; - - toXDR(base?: string): Buffer; - toXDR(encoding: string): string; - } - 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 { - static fromXDR(xdr: Buffer): DecoratedSignature; - - 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; - } - } - - function hash(data: Buffer): Buffer; - function sign(data: Buffer, rawSecret: Buffer): Buffer; - function verify(data: Buffer, signature: Buffer, rawPublicKey: Buffer): boolean; -} +import { ASSET_TYPE, Asset, Memo, Transaction } from 'stellar-base'; // Re-StellarBase -export import Account = StellarBase.Account; -export import ASSET_TYPE = StellarBase.ASSET_TYPE; -export import Asset = StellarBase.Asset; -export import FastSigning = StellarBase.FastSigning; -export import Keypair = StellarBase.Keypair; -export import Memo = StellarBase.Memo; -export import MemoHash = StellarBase.MemoHash; -export import MemoID = StellarBase.MemoID; -export import MemoNone = StellarBase.MemoNone; -export import MemoReturn = StellarBase.MemoReturn; -export import MemoText = StellarBase.MemoText; -export import Network = StellarBase.Network; -export import Networks = StellarBase.Networks; -export import Operation = StellarBase.Operation; -export import OperationType = StellarBase.OperationType; -export import StrKey = StellarBase.StrKey; -export import Transaction = StellarBase.Transaction; -export import TransactionBuilder = StellarBase.TransactionBuilder; -export import TransactionOperation = StellarBase.TransactionOperation; -export import hash = StellarBase.hash; -export import sign = StellarBase.sign; -export import verify = StellarBase.verify; -export import xdr = StellarBase.xdr; +export { + Account, + ASSET_TYPE, + Asset, + FastSigning, + Keypair, + Memo, + MemoHash, + MemoID, + MemoNone, + MemoReturn, + MemoText, + Network, + Networks, + Operation, + OperationType, + StrKey, + Transaction, + TransactionBuilder, + TransactionOperation, + hash, + sign, + verify, + xdr +} from 'stellar-base'; + +// Shorthands, not-to-export. +export {}; +type Key = string|number|symbol; +type Diff = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; +type Omit = Pick>; export class NetworkError extends Error { private response: any; diff --git a/types/stellar-sdk/stellar-sdk-tests.ts b/types/stellar-sdk/stellar-sdk-tests.ts index 2533fefaa4..d531fc850f 100644 --- a/types/stellar-sdk/stellar-sdk-tests.ts +++ b/types/stellar-sdk/stellar-sdk-tests.ts @@ -1,43 +1,4 @@ import * as StellarSdk from 'stellar-sdk'; -const sourceKey = StellarSdk.Keypair.random(); // $ExpectType Keypair -const destKey = StellarSdk.Keypair.random(); -const account = new StellarSdk.Account(sourceKey.publicKey(), '1'); -const transaction = new StellarSdk.TransactionBuilder(account) - .addOperation(StellarSdk.Operation.accountMerge({destination: destKey.publicKey()})) - .addMemo(new StellarSdk.Memo(StellarSdk.MemoText, "memo")) - .build(); // $ExpectType () => Transaction, TransactionOperation[]> - StellarSdk.StellarTomlResolver.resolve("example.com", {allowHttp: true, timeout: 100}) .then(toml => toml.FEDERATION_SERVER); - -const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)); // $ExpectType DecoratedSignature -sig.hint(); // $ExpectType Buffer -sig.signature(); // $ExpectType Buffer - -StellarSdk.Memo.none(); // $ExpectType Memo<"none"> -StellarSdk.Memo.text('asdf'); // $ExpectType Memo<"text"> -StellarSdk.Memo.id('asdf'); // $ExpectType Memo<"id"> -StellarSdk.Memo.return('asdf'); // $ExpectType Memo<"return"> -StellarSdk.Memo.hash('asdf'); // $ExpectType Memo<"hash"> -StellarSdk.Memo.none().value; // $ExpectType null -StellarSdk.Memo.id('asdf').value; // $ExpectType string -StellarSdk.Memo.text('asdf').value; // $ExpectType string -StellarSdk.Memo.return('asdf').value; // $ExpectType Buffer -StellarSdk.Memo.hash('asdf').value; // $ExpectType Buffer - -// P.S. don't use Memo constructor -(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).value; // $ExpectType AnyValue -(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType AnyType - -const noSignerXDR = StellarSdk.Operation.setOptions({lowThreshold: 1}); -StellarSdk.Operation.fromXDRObject(noSignerXDR).signer; // $ExpectType never - -const newSignerXDR1 = StellarSdk.Operation.setOptions({signer: {ed25519PublicKey: sourceKey.publicKey(), weight: '1'}}); -StellarSdk.Operation.fromXDRObject(newSignerXDR1).signer; // $ExpectType SignerEd25519PublicKey - -const newSignerXDR2 = StellarSdk.Operation.setOptions({signer: {sha256Hash: Buffer.from(''), weight: '1'}}); -StellarSdk.Operation.fromXDRObject(newSignerXDR2).signer; // $ExpectType SignerSha256Hash - -const newSignerXDR3 = StellarSdk.Operation.setOptions({signer: {preAuthTx: '', weight: 1}}); -StellarSdk.Operation.fromXDRObject(newSignerXDR3).signer; // $ExpectType SignerPreAuthTx From 7d36f72435e8868f74c87e9d0f59f8d278c94411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 11:51:54 +0200 Subject: [PATCH 14/19] fix: change fake enums into type+namespace --- types/stellar-base/index.d.ts | 143 ++++++++++++++--------- types/stellar-base/stellar-base-tests.ts | 6 +- types/stellar-sdk/index.d.ts | 42 ++++--- 3 files changed, 112 insertions(+), 79 deletions(-) diff --git a/types/stellar-base/index.d.ts b/types/stellar-base/index.d.ts index 4b86065c40..b0815017c6 100644 --- a/types/stellar-base/index.d.ts +++ b/types/stellar-base/index.d.ts @@ -18,11 +18,17 @@ export class Account { incrementSequenceNumber(): void; } -export enum ASSET_TYPE { - native = 'native', - credit4 = 'credit_alphanum4', - credit12 = 'credit_alphanum12', +export namespace AssetType { + type native = 'native'; + type credit4 = 'credit_alphanum4'; + type credit12 = 'credit_alphanum12'; } +export type AssetType = + | AssetType.native + | AssetType.credit4 + | AssetType.credit12 +; + export class Asset { static native(): Asset; static fromOperation(xdr: xdr.Asset): Asset; @@ -31,7 +37,7 @@ export class Asset { getCode(): string; getIssuer(): string; - getAssetType(): ASSET_TYPE; + getAssetType(): AssetType; isNative(): boolean; equals(other: Asset): boolean; toXDRObject(): xdr.Asset; @@ -68,39 +74,46 @@ export const MemoID = 'id'; export const MemoText = 'text'; export const MemoHash = 'hash'; export const MemoReturn = 'return'; -export type MemoNone = 'none'; -export type MemoID = 'id'; -export type MemoText = 'text'; -export type MemoHash = 'hash'; -export type MemoReturn = 'return'; -export class Memo { - static fromXDRObject(memo: xdr.Memo): Memo; - static hash(hash: string): Memo; - static id(id: string): Memo; - static none(): Memo; - static return(hash: string): Memo; - static text(text: string): Memo; +export namespace MemoType { + type None = typeof MemoNone; + type ID = typeof MemoID; + type Text = typeof MemoText; + type Hash = typeof MemoHash; + type Return = typeof MemoReturn; +} +export type MemoType = + | MemoType.None + | MemoType.ID + | MemoType.Text + | MemoType.Hash + | MemoType.Return +; +export type MemoValue = null | string | Buffer; - constructor(type: MemoNone, value?: null); - constructor(type: MemoHash | MemoReturn, value: Buffer) - constructor(type: MemoHash | MemoReturn | MemoID | MemoText, value: string) - constructor(type: T, value: Memo.AnyValue) +export class Memo { + static fromXDRObject(memo: xdr.Memo): Memo; + static hash(hash: string): Memo; + static id(id: string): Memo; + static none(): Memo; + static return(hash: string): Memo; + static text(text: string): Memo; + + constructor(type: MemoType.None, value?: null); + constructor(type: MemoType.Hash | MemoType.Return, value: Buffer) + constructor(type: MemoType.Hash | MemoType.Return | MemoType.ID | MemoType.Text, value: string) + constructor(type: T, value: MemoValue) type: T; value: - T extends MemoNone ? null : - T extends MemoID ? string : - T extends MemoText ? string : - T extends MemoHash ? Buffer : - T extends MemoReturn ? Buffer : - Memo.AnyValue; + T extends MemoType.None ? null : + T extends MemoType.ID ? string : + T extends MemoType.Text ? string : + T extends MemoType.Hash ? Buffer : + T extends MemoType.Return ? Buffer : + MemoValue; toXDRObject(): xdr.Memo; } -export namespace Memo { - type AnyType = MemoNone | MemoID | MemoText | MemoHash | MemoReturn; - type AnyValue = null | string | Buffer; -} export enum Networks { PUBLIC = 'Public Global Stellar Network ; September 2015', @@ -122,6 +135,16 @@ export class Network { export const AuthRequiredFlag: 1; export const AuthRevocableFlag: 2; export const AuthImmutableFlag: 4; +export namespace AuthFlag { + type required = typeof AuthRequiredFlag; + type revocable = typeof AuthRevocableFlag; + type rmmutable = typeof AuthImmutableFlag; +} +export type AuthFlag = + | AuthFlag.required + | AuthFlag.revocable + | AuthFlag.rmmutable +; export type TransactionOperation = Operation.CreateAccount @@ -137,20 +160,34 @@ export type TransactionOperation = | Operation.ManageData | Operation.BumpSequence; -export enum OperationType { - createAccount = 'createAccount', - payment = 'payment', - pathPayment = 'pathPayment', - createPassiveOffer = 'createPassiveOffer', - manageOffer = 'manageOffer', - setOptions = 'setOptions', - changeTrust = 'changeTrust', - allowTrust = 'allowTrust', - accountMerge = 'accountMerge', - inflation = 'inflation', - manageData = 'manageData', - bumpSequence = 'bumpSequence', +export namespace OperationType { + type createAccount = 'createAccount'; + type payment = 'payment'; + type pathPayment = 'pathPayment'; + type createPassiveOffer = 'createPassiveOffer'; + type manageOffer = 'manageOffer'; + type setOptions = 'setOptions'; + type changeTrust = 'changeTrust'; + type allowTrust = 'allowTrust'; + type accountMerge = 'accountMerge'; + type inflation = 'inflation'; + type manageData = 'manageData'; + type bumpSequence = 'bumpSequence'; } +export type OperationType = + | OperationType.createAccount + | OperationType.payment + | OperationType.pathPayment + | OperationType.createPassiveOffer + | OperationType.manageOffer + | OperationType.setOptions + | OperationType.changeTrust + | OperationType.allowTrust + | OperationType.accountMerge + | OperationType.inflation + | OperationType.manageData + | OperationType.bumpSequence +; export namespace Operation { interface Operation { @@ -273,16 +310,6 @@ export namespace Operation { } function payment(options: PaymentOptions): xdr.Operation; - /* - * Required = 1 << 0 - * Revocable = 1 << 1 - * Immutable = 1 << 2 - */ - enum AuthFlags { - Required = 1, - Revocable = 2, - Immutable = 4, - } interface SignerEd25519PublicKey { ed25519PublicKey: string; weight: number | undefined; @@ -312,8 +339,8 @@ export namespace Operation { type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; interface SetOptions extends Operation { inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; + clearFlags?: AuthFlag; + setFlags?: AuthFlag; masterWeight?: number; lowThreshold?: number; medThreshold?: number; @@ -327,8 +354,8 @@ export namespace Operation { } interface SetOptionsOptions extends OperationOptions { inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; + clearFlags?: AuthFlag; + setFlags?: AuthFlag; masterWeight?: number | string; lowThreshold?: number | string; medThreshold?: number | string; diff --git a/types/stellar-base/stellar-base-tests.ts b/types/stellar-base/stellar-base-tests.ts index 0a8176919c..8b703d4c84 100644 --- a/types/stellar-base/stellar-base-tests.ts +++ b/types/stellar-base/stellar-base-tests.ts @@ -6,7 +6,7 @@ const account = new StellarSdk.Account(sourceKey.publicKey(), '1'); const transaction = new StellarSdk.TransactionBuilder(account) .addOperation(StellarSdk.Operation.accountMerge({destination: destKey.publicKey()})) .addMemo(new StellarSdk.Memo(StellarSdk.MemoText, "memo")) - .build(); // $ExpectType () => Transaction, TransactionOperation[]> + .build(); // $ExpectType () => Transaction, TransactionOperation[]> const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)); // $ExpectType DecoratedSignature sig.hint(); // $ExpectType Buffer @@ -24,8 +24,8 @@ StellarSdk.Memo.return('asdf').value; // $ExpectType Buffer StellarSdk.Memo.hash('asdf').value; // $ExpectType Buffer // P.S. don't use Memo constructor -(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).value; // $ExpectType AnyValue -(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType AnyType +(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).value; // $ExpectType MemoValue +(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType MemoType const noSignerXDR = StellarSdk.Operation.setOptions({lowThreshold: 1}); StellarSdk.Operation.fromXDRObject(noSignerXDR).signer; // $ExpectType never diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index d0f3d86023..5afec167cd 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -11,16 +11,22 @@ /// -import { ASSET_TYPE, Asset, Memo, Transaction } from 'stellar-base'; +import { AssetType, Asset, Memo, MemoType, Transaction } from 'stellar-base'; // Re-StellarBase export { Account, - ASSET_TYPE, Asset, + AssetType, + AuthFlag, + AuthImmutableFlag, + AuthRequiredFlag, + AuthRevocableFlag, FastSigning, Keypair, Memo, + MemoType, + MemoValue, MemoHash, MemoID, MemoNone, @@ -157,7 +163,7 @@ export namespace Server { } interface AssetRecord extends Horizon.BaseResponse { - asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_type: AssetType.credit4 | AssetType.credit12; asset_code: string; asset_issuer: string; paging_token: string; @@ -452,7 +458,7 @@ export namespace Horizon { hash: string; id: string; ledger: number; - memo_type: Memo.AnyType; + memo_type: MemoType; memo?: string; operation_count: number; paging_token: string; @@ -465,18 +471,18 @@ export namespace Horizon { interface BalanceLineNative { balance: string; - asset_type: ASSET_TYPE.native; + asset_type: AssetType.native; } - interface BalanceLineAsset { + interface BalanceLineAsset { balance: string; limit: string; asset_type: T; asset_code: string; asset_issuer: string; } - type BalanceLine = - T extends ASSET_TYPE.native ? BalanceLineNative : - T extends ASSET_TYPE.credit4 | ASSET_TYPE.credit12 ? BalanceLineAsset : + type BalanceLine = + T extends AssetType.native ? BalanceLineNative : + T extends AssetType.credit4 | AssetType.credit12 ? BalanceLineAsset : BalanceLineNative | BalanceLineAsset; interface PriceR { @@ -559,7 +565,7 @@ export namespace Horizon { interface PaymentOperationResponse extends BaseOperationResponse { from: string; to: string; - asset_type: ASSET_TYPE; + asset_type: AssetType; asset_code?: string; asset_issuer?: string; amount: string; @@ -567,11 +573,11 @@ export namespace Horizon { interface PathPaymentOperationResponse extends BaseOperationResponse { from: string; to: string; - asset_type: ASSET_TYPE; + asset_type: AssetType; asset_code?: string; asset_issuer?: string; amount: string; - source_asset_type: ASSET_TYPE; + source_asset_type: AssetType; source_asset_code?: string; source_asset_issuer?: string; source_max: string; @@ -580,24 +586,24 @@ export namespace Horizon { interface ManageOfferOperationResponse extends BaseOperationResponse { offer_id: number; amount: string; - buying_asset_type: ASSET_TYPE; + buying_asset_type: AssetType; buying_asset_code?: string; buying_asset_issuer?: string; price: string; price_r: PriceR; - selling_asset_type: ASSET_TYPE; + selling_asset_type: AssetType; selling_asset_code?: string; selling_asset_issuer?: string; } interface PassiveOfferOperationResponse extends BaseOperationResponse { offer_id: number; amount: string; - buying_asset_type: ASSET_TYPE; + buying_asset_type: AssetType; buying_asset_code?: string; buying_asset_issuer?: string; price: string; price_r: PriceR; - selling_asset_type: ASSET_TYPE; + selling_asset_type: AssetType; selling_asset_code?: string; selling_asset_issuer?: string; } @@ -615,7 +621,7 @@ export namespace Horizon { clear_flags_s: Array<('auth_required_flag' | 'auth_revocable_flag')>; } interface ChangeTrustOperationResponse extends BaseOperationResponse { - asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_type: AssetType.credit4 | AssetType.credit12; asset_code: string; asset_issuer: string; trustee: string; @@ -623,7 +629,7 @@ export namespace Horizon { limit: string; } interface AllowTrustOperationResponse extends BaseOperationResponse { - asset_type: ASSET_TYPE; + asset_type: AssetType; asset_code: string; asset_issuer: string; authorize: boolean; From 8a8c081052ccb3a67857f84d9522bade2426ed20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 12:53:36 +0200 Subject: [PATCH 15/19] feat: TransactionOperation -> Operation --- types/stellar-base/index.d.ts | 479 ++++++++++++----------- types/stellar-base/stellar-base-tests.ts | 8 +- types/stellar-sdk/index.d.ts | 3 +- 3 files changed, 264 insertions(+), 226 deletions(-) diff --git a/types/stellar-base/index.d.ts b/types/stellar-base/index.d.ts index b0815017c6..e47c28e3e7 100644 --- a/types/stellar-base/index.d.ts +++ b/types/stellar-base/index.d.ts @@ -10,6 +10,7 @@ // TypeScript Version: 2.9 /// +export {}; export class Account { constructor(accountId: string, sequence: string | number) @@ -146,7 +147,253 @@ export type AuthFlag = | AuthFlag.rmmutable ; -export type TransactionOperation = +export namespace Signer { + interface Ed25519PublicKey { + ed25519PublicKey: string; + weight: number | undefined; + } + interface Sha256Hash { + sha256Hash: Buffer; + weight: number | undefined; + } + interface PreAuthTx { + preAuthTx: Buffer; + weight: number | undefined; + } +} +export type Signer = + | Signer.Ed25519PublicKey + | Signer.Sha256Hash + | Signer.PreAuthTx +; + +export namespace SignerOptions { + interface Ed25519PublicKey { + ed25519PublicKey: string; + weight?: number | string; + } + interface Sha256Hash { + sha256Hash: Buffer | string; + weight?: number | string; + } + interface PreAuthTx { + preAuthTx: Buffer | string; + weight?: number | string; + } +} +export type SignerOptions = + | SignerOptions.Ed25519PublicKey + | SignerOptions.Sha256Hash + | SignerOptions.PreAuthTx +; +export type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; + +export namespace OperationType { + type CreateAccount = 'createAccount'; + type Payment = 'payment'; + type PathPayment = 'pathPayment'; + type CreatePassiveOffer = 'createPassiveOffer'; + type ManageOffer = 'manageOffer'; + type SetOptions = 'setOptions'; + type ChangeTrust = 'changeTrust'; + type AllowTrust = 'allowTrust'; + type AccountMerge = 'accountMerge'; + type Inflation = 'inflation'; + type ManageData = 'manageData'; + type BumpSequence = 'bumpSequence'; +} +export type OperationType = + | OperationType.CreateAccount + | OperationType.Payment + | OperationType.PathPayment + | OperationType.CreatePassiveOffer + | OperationType.ManageOffer + | OperationType.SetOptions + | OperationType.ChangeTrust + | OperationType.AllowTrust + | OperationType.AccountMerge + | OperationType.Inflation + | OperationType.ManageData + | OperationType.BumpSequence +; + +export namespace OperationOptions { + interface BaseOptions { + source?: string; + } + interface AccountMerge extends BaseOptions { + destination: string; + } + interface AllowTrust extends BaseOptions { + trustor: string; + assetCode: string; + authorize?: boolean; + } + interface ChangeTrust extends BaseOptions { + asset: Asset; + limit?: string; + } + interface CreateAccount extends BaseOptions { + destination: string; + startingBalance: string; + } + interface CreatePassiveOffer extends BaseOptions { + selling: Asset; + buying: Asset; + amount: string; + price: number | string | object /* bignumber.js */; + } + interface ManageOffer extends CreatePassiveOffer { + offerId?: number | string; + } + interface Inflation extends BaseOptions { // tslint:disable-line + } + interface ManageData extends BaseOptions { + name: string; + value: string | Buffer; + } + interface PathPayment extends BaseOptions { + sendAsset: Asset; + sendMax: string; + destination: string; + destAsset: Asset; + destAmount: string; + path?: Asset[]; + } + interface Payment extends BaseOptions { + amount: string; + asset: Asset; + destination: string; + } + interface SetOptions extends BaseOptions { + inflationDest?: string; + clearFlags?: AuthFlag; + setFlags?: AuthFlag; + masterWeight?: number | string; + lowThreshold?: number | string; + medThreshold?: number | string; + highThreshold?: number | string; + homeDomain?: string; + signer?: T; + } + interface BumpSequence extends BaseOptions { + bumpTo: string; + } +} +export type OperationOptions = + | OperationOptions.CreateAccount + | OperationOptions.Payment + | OperationOptions.PathPayment + | OperationOptions.CreatePassiveOffer + | OperationOptions.ManageOffer + | OperationOptions.SetOptions + | OperationOptions.ChangeTrust + | OperationOptions.AllowTrust + | OperationOptions.AccountMerge + | OperationOptions.Inflation + | OperationOptions.ManageData + | OperationOptions.BumpSequence +; + +export namespace Operation { + interface BaseOperation { + type: T; + source?: string; + } + + interface AccountMerge extends BaseOperation { + destination: string; + } + function accountMerge(options: OperationOptions.AccountMerge): xdr.Operation; + + interface AllowTrust extends BaseOperation { + trustor: string; + assetCode: string; + authorize: boolean | undefined; + } + function allowTrust(options: OperationOptions.AllowTrust): xdr.Operation; + + interface ChangeTrust extends BaseOperation { + line: Asset; + limit: string; + } + function changeTrust(options: OperationOptions.ChangeTrust): xdr.Operation; + + interface CreateAccount extends BaseOperation { + destination: string; + startingBalance: string; + } + function createAccount(options: OperationOptions.CreateAccount): xdr.Operation; + + interface CreatePassiveOffer extends BaseOperation { + selling: Asset; + buying: Asset; + amount: string; + price: string; + } + function createPassiveOffer(options: OperationOptions.CreatePassiveOffer): xdr.Operation; + + interface Inflation extends BaseOperation { + } + function inflation(options: OperationOptions.Inflation): xdr.Operation; + + interface ManageData extends BaseOperation { + name: string; + value: Buffer; + } + function manageData(options: OperationOptions.ManageData): xdr.Operation; + + interface ManageOffer extends BaseOperation { + selling: Asset; + buying: Asset; + amount: string; + price: string; + offerId: string; + } + function manageOffer(options: OperationOptions.ManageOffer): xdr.Operation; + + interface PathPayment extends BaseOperation { + sendAsset: Asset; + sendMax: string; + destination: string; + destAsset: Asset; + destAmount: string; + path: Asset[]; + } + function pathPayment(options: OperationOptions.PathPayment): xdr.Operation; + + interface Payment extends BaseOperation { + amount: string; + asset: Asset; + destination: string; + } + function payment(options: OperationOptions.Payment): xdr.Operation; + + interface SetOptions extends BaseOperation { + inflationDest?: string; + clearFlags?: AuthFlag; + setFlags?: AuthFlag; + masterWeight?: number; + lowThreshold?: number; + medThreshold?: number; + highThreshold?: number; + homeDomain?: string; + signer: + T extends {ed25519PublicKey: any} ? Signer.Ed25519PublicKey : + T extends {sha256Hash: any} ? Signer.Sha256Hash : + T extends {preAuthTx: any} ? Signer.PreAuthTx : + never; + } + function setOptions(options: OperationOptions.SetOptions): xdr.Operation>; + + interface BumpSequence extends BaseOperation { + bumpTo: string; + } + function bumpSequence(options: OperationOptions.BumpSequence): xdr.Operation; + + function fromXDRObject(xdrOperation: xdr.Operation): T; +} +export type Operation = Operation.CreateAccount | Operation.Payment | Operation.PathPayment @@ -158,224 +405,9 @@ export type TransactionOperation = | Operation.AccountMerge | Operation.Inflation | Operation.ManageData - | Operation.BumpSequence; - -export namespace OperationType { - type createAccount = 'createAccount'; - type payment = 'payment'; - type pathPayment = 'pathPayment'; - type createPassiveOffer = 'createPassiveOffer'; - type manageOffer = 'manageOffer'; - type setOptions = 'setOptions'; - type changeTrust = 'changeTrust'; - type allowTrust = 'allowTrust'; - type accountMerge = 'accountMerge'; - type inflation = 'inflation'; - type manageData = 'manageData'; - type bumpSequence = 'bumpSequence'; -} -export type OperationType = - | OperationType.createAccount - | OperationType.payment - | OperationType.pathPayment - | OperationType.createPassiveOffer - | OperationType.manageOffer - | OperationType.setOptions - | OperationType.changeTrust - | OperationType.allowTrust - | OperationType.accountMerge - | OperationType.inflation - | OperationType.manageData - | OperationType.bumpSequence + | Operation.BumpSequence ; -export namespace Operation { - interface Operation { - type: T; - source?: string; - } - interface OperationOptions { - source?: string; - } - - interface AccountMerge extends Operation { - destination: string; - } - interface AccountMergeOptions extends OperationOptions { - destination: string; - } - function accountMerge(options: AccountMergeOptions): xdr.Operation; - - interface AllowTrust extends Operation { - trustor: string; - assetCode: string; - authorize: boolean | undefined; - } - interface AllowTrustOptions extends OperationOptions { - trustor: string; - assetCode: string; - authorize?: boolean; - } - function allowTrust(options: AllowTrustOptions): xdr.Operation; - - interface ChangeTrust extends Operation { - line: Asset; - limit: string; - } - interface ChangeTrustOptions extends OperationOptions { - asset: Asset; - limit?: string; - } - function changeTrust(options: ChangeTrustOptions): xdr.Operation; - - interface CreateAccount extends Operation { - destination: string; - startingBalance: string; - } - interface CreateAccountOptions extends OperationOptions { - destination: string; - startingBalance: string; - } - function createAccount(options: CreateAccountOptions): xdr.Operation; - - interface CreatePassiveOffer extends Operation { - selling: Asset; - buying: Asset; - amount: string; - price: string; - } - interface CreatePassiveOfferOptions extends OperationOptions { - selling: Asset; - buying: Asset; - amount: string; - price: number | string | object /* bignumber.js */; - } - function createPassiveOffer(options: CreatePassiveOfferOptions): xdr.Operation; - - interface Inflation extends Operation { - } - interface InflationOptions extends OperationOptions { // tslint:disable-line - } - function inflation(options: InflationOptions): xdr.Operation; - - interface ManageData extends Operation { - name: string; - value: Buffer; - } - interface ManageDataOptions extends OperationOptions { - name: string; - value: string | Buffer; - } - function manageData(options: ManageDataOptions): xdr.Operation; - - interface ManageOffer extends Operation { - selling: Asset; - buying: Asset; - amount: string; - price: string; - offerId: string; - } - interface ManageOfferOptions extends CreatePassiveOfferOptions { - offerId?: number | string; - } - function manageOffer(options: ManageOfferOptions): xdr.Operation; - - interface PathPayment extends Operation { - sendAsset: Asset; - sendMax: string; - destination: string; - destAsset: Asset; - destAmount: string; - path: Asset[]; - } - interface PathPaymentOptions extends OperationOptions { - sendAsset: Asset; - sendMax: string; - destination: string; - destAsset: Asset; - destAmount: string; - path?: Asset[]; - } - function pathPayment(options: PathPaymentOptions): xdr.Operation; - - interface Payment extends Operation { - amount: string; - asset: Asset; - destination: string; - } - interface PaymentOptions extends OperationOptions { - amount: string; - asset: Asset; - destination: string; - } - function payment(options: PaymentOptions): xdr.Operation; - - interface SignerEd25519PublicKey { - ed25519PublicKey: string; - weight: number | undefined; - } - interface SignerSha256Hash { - sha256Hash: Buffer; - weight: number | undefined; - } - interface SignerPreAuthTx { - preAuthTx: Buffer; - weight: number | undefined; - } - type Signer = SignerEd25519PublicKey | SignerSha256Hash | SignerPreAuthTx; - interface SignerEd25519PublicKeyOptions { - ed25519PublicKey: string; - weight?: number | string; - } - interface SignerSha256HashOptions { - sha256Hash: Buffer | string; - weight?: number | string; - } - interface SignerPreAuthTxOptions { - preAuthTx: Buffer | string; - weight?: number | string; - } - type SignerOptions = SignerEd25519PublicKeyOptions | SignerSha256HashOptions | SignerPreAuthTxOptions; - type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; - interface SetOptions extends Operation { - inflationDest?: string; - clearFlags?: AuthFlag; - setFlags?: AuthFlag; - masterWeight?: number; - lowThreshold?: number; - medThreshold?: number; - highThreshold?: number; - homeDomain?: string; - signer: - T extends {ed25519PublicKey: any} ? SignerEd25519PublicKey : - T extends {sha256Hash: any} ? SignerSha256Hash : - T extends {preAuthTx: any} ? SignerPreAuthTx : - never; - } - interface SetOptionsOptions extends OperationOptions { - inflationDest?: string; - clearFlags?: AuthFlag; - setFlags?: AuthFlag; - masterWeight?: number | string; - lowThreshold?: number | string; - medThreshold?: number | string; - highThreshold?: number | string; - homeDomain?: string; - signer?: T; - } - function setOptions(options: SetOptionsOptions): xdr.Operation>; - - interface BumpSequence extends Operation { - bumpTo: string; - } - interface BumpSequenceOptions extends OperationOptions { - bumpTo: string; - } - function bumpSequence(options: BumpSequenceOptions): xdr.Operation; - - function fromXDRObject(xdrOperation: xdr.Operation): T; -} - export namespace StrKey { function encodeEd25519PublicKey(data: Buffer): string; function decodeEd25519PublicKey(data: string): Buffer; @@ -392,7 +424,7 @@ export namespace StrKey { function decodeSha256Hash(data: string): Buffer; } -export class Transaction { +export class Transaction { constructor(envelope: string | xdr.TransactionEnvelope) hash(): Buffer; sign(...keypairs: Keypair[]): void; @@ -426,6 +458,13 @@ export namespace TransactionBuilder { } } +// Hidden namespace as hack to work around name collision. +declare namespace xdrHidden { // tslint:disable-line:strict-export-declare-modifiers + class Operation2 extends xdr.XDRStruct { + static fromXDR(xdr: Buffer): xdr.Operation; + } +} + export namespace xdr { class XDRStruct { static fromXDR(xdr: Buffer): XDRStruct; @@ -433,9 +472,7 @@ export namespace xdr { toXDR(base?: string): Buffer; toXDR(encoding: string): string; } - class Operation extends XDRStruct { - static fromXDR(xdr: Buffer): Operation; - } + export import Operation = xdrHidden.Operation2; // tslint:disable-line:strict-export-declare-modifiers class Asset extends XDRStruct { static fromXDR(xdr: Buffer): Asset; } diff --git a/types/stellar-base/stellar-base-tests.ts b/types/stellar-base/stellar-base-tests.ts index 8b703d4c84..9950120554 100644 --- a/types/stellar-base/stellar-base-tests.ts +++ b/types/stellar-base/stellar-base-tests.ts @@ -6,7 +6,7 @@ const account = new StellarSdk.Account(sourceKey.publicKey(), '1'); const transaction = new StellarSdk.TransactionBuilder(account) .addOperation(StellarSdk.Operation.accountMerge({destination: destKey.publicKey()})) .addMemo(new StellarSdk.Memo(StellarSdk.MemoText, "memo")) - .build(); // $ExpectType () => Transaction, TransactionOperation[]> + .build(); // $ExpectType () => Transaction, Operation[]> const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)); // $ExpectType DecoratedSignature sig.hint(); // $ExpectType Buffer @@ -31,10 +31,10 @@ const noSignerXDR = StellarSdk.Operation.setOptions({lowThreshold: 1}); StellarSdk.Operation.fromXDRObject(noSignerXDR).signer; // $ExpectType never const newSignerXDR1 = StellarSdk.Operation.setOptions({signer: {ed25519PublicKey: sourceKey.publicKey(), weight: '1'}}); -StellarSdk.Operation.fromXDRObject(newSignerXDR1).signer; // $ExpectType SignerEd25519PublicKey +StellarSdk.Operation.fromXDRObject(newSignerXDR1).signer; // $ExpectType Ed25519PublicKey const newSignerXDR2 = StellarSdk.Operation.setOptions({signer: {sha256Hash: Buffer.from(''), weight: '1'}}); -StellarSdk.Operation.fromXDRObject(newSignerXDR2).signer; // $ExpectType SignerSha256Hash +StellarSdk.Operation.fromXDRObject(newSignerXDR2).signer; // $ExpectType Sha256Hash const newSignerXDR3 = StellarSdk.Operation.setOptions({signer: {preAuthTx: '', weight: 1}}); -StellarSdk.Operation.fromXDRObject(newSignerXDR3).signer; // $ExpectType SignerPreAuthTx +StellarSdk.Operation.fromXDRObject(newSignerXDR3).signer; // $ExpectType PreAuthTx diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 5afec167cd..58336bc456 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -37,9 +37,10 @@ export { Operation, OperationType, StrKey, + Signer, + SignerOptions, Transaction, TransactionBuilder, - TransactionOperation, hash, sign, verify, From 326ee2bd7c1b4ebb43dab01d05763b7bbcd86ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 12:54:49 +0200 Subject: [PATCH 16/19] refactor: simplify SignerUnion --- types/stellar-base/index.d.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/types/stellar-base/index.d.ts b/types/stellar-base/index.d.ts index e47c28e3e7..fa107e1922 100644 --- a/types/stellar-base/index.d.ts +++ b/types/stellar-base/index.d.ts @@ -186,7 +186,6 @@ export type SignerOptions = | SignerOptions.Sha256Hash | SignerOptions.PreAuthTx ; -export type SignerUnion = {ed25519PublicKey: any} | {sha256Hash: any} | {preAuthTx: any} | null; export namespace OperationType { type CreateAccount = 'createAccount'; @@ -265,7 +264,7 @@ export namespace OperationOptions { asset: Asset; destination: string; } - interface SetOptions extends BaseOptions { + interface SetOptions extends BaseOptions { inflationDest?: string; clearFlags?: AuthFlag; setFlags?: AuthFlag; @@ -369,7 +368,7 @@ export namespace Operation { } function payment(options: OperationOptions.Payment): xdr.Operation; - interface SetOptions extends BaseOperation { + interface SetOptions extends BaseOperation { inflationDest?: string; clearFlags?: AuthFlag; setFlags?: AuthFlag; @@ -384,7 +383,7 @@ export namespace Operation { T extends {preAuthTx: any} ? Signer.PreAuthTx : never; } - function setOptions(options: OperationOptions.SetOptions): xdr.Operation>; + function setOptions(options: OperationOptions.SetOptions): xdr.Operation>; interface BumpSequence extends BaseOperation { bumpTo: string; From ff90ed990cfd17bec6f7c82f702e896b4378e3c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 12:59:25 +0200 Subject: [PATCH 17/19] test: disable weird-behaviour test --- types/stellar-base/stellar-base-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/stellar-base/stellar-base-tests.ts b/types/stellar-base/stellar-base-tests.ts index 9950120554..81409bb1c1 100644 --- a/types/stellar-base/stellar-base-tests.ts +++ b/types/stellar-base/stellar-base-tests.ts @@ -25,7 +25,7 @@ StellarSdk.Memo.hash('asdf').value; // $ExpectType Buffer // P.S. don't use Memo constructor (new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).value; // $ExpectType MemoValue -(new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType MemoType +// (new StellarSdk.Memo(StellarSdk.MemoHash, 'asdf')).type; // $ExpectType MemoType // TODO: Inspect what's wrong with linter. const noSignerXDR = StellarSdk.Operation.setOptions({lowThreshold: 1}); StellarSdk.Operation.fromXDRObject(noSignerXDR).signer; // $ExpectType never From 5a23b6d05ee17e3578834ae30f269f6d0eefad3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 13:01:41 +0200 Subject: [PATCH 18/19] fix: export forgot OperationType --- types/stellar-sdk/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 58336bc456..cf05b3412d 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -35,6 +35,7 @@ export { Network, Networks, Operation, + OperationOptions, OperationType, StrKey, Signer, From c62f0dc807d4fedfa0c1a1e8ccd7ab0bd4718e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Thu, 10 Jan 2019 13:41:59 +0200 Subject: [PATCH 19/19] fix: Memo.text can be either string or Buffer --- types/stellar-base/index.d.ts | 2 +- types/stellar-base/stellar-base-tests.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/stellar-base/index.d.ts b/types/stellar-base/index.d.ts index fa107e1922..a230c36942 100644 --- a/types/stellar-base/index.d.ts +++ b/types/stellar-base/index.d.ts @@ -108,7 +108,7 @@ export class Memo { value: T extends MemoType.None ? null : T extends MemoType.ID ? string : - T extends MemoType.Text ? string : + T extends MemoType.Text ? string | Buffer : // github.com/stellar/js-stellar-base/issues/152 T extends MemoType.Hash ? Buffer : T extends MemoType.Return ? Buffer : MemoValue; diff --git a/types/stellar-base/stellar-base-tests.ts b/types/stellar-base/stellar-base-tests.ts index 81409bb1c1..83a52cbe7b 100644 --- a/types/stellar-base/stellar-base-tests.ts +++ b/types/stellar-base/stellar-base-tests.ts @@ -19,7 +19,7 @@ StellarSdk.Memo.return('asdf'); // $ExpectType Memo<"return"> StellarSdk.Memo.hash('asdf'); // $ExpectType Memo<"hash"> StellarSdk.Memo.none().value; // $ExpectType null StellarSdk.Memo.id('asdf').value; // $ExpectType string -StellarSdk.Memo.text('asdf').value; // $ExpectType string +StellarSdk.Memo.text('asdf').value; // $ExpectType string | Buffer StellarSdk.Memo.return('asdf').value; // $ExpectType Buffer StellarSdk.Memo.hash('asdf').value; // $ExpectType Buffer