From ce0ad7ff1c8fc2329dfdea2c7cf5e053b2537623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 14:20:16 +0200 Subject: [PATCH 01/12] refactor: move StellarBase into namespace --- types/stellar-sdk/index.d.ts | 859 +++++++++++++++++++---------------- 1 file changed, 459 insertions(+), 400 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index d690398d0c..63748e65be 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -5,23 +5,442 @@ // Paul Selden // Max Bause // Timur Ramazanov +// Kalvis Kalniņš // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 /// -export class Account { - constructor(accountId: string, sequence: string) - accountId(): string; - sequenceNumber(): string; - incrementSequenceNumber(): void; +export namespace StellarBase { + const hash: any; // TODO + const sign: any; // TODO + const verify: any; // TODO + const FastSigning: any; // TODO + + class Account { + constructor(accountId: string, sequence: string | number) + accountId(): string; + sequenceNumber(): string; + incrementSequenceNumber(): void; + } + + 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; + } + + 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 MemoType = typeof MemoNone | typeof MemoID | typeof MemoText | typeof MemoHash | typeof MemoReturn; + 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: typeof MemoNone); + constructor(type: typeof MemoID | typeof MemoText | typeof MemoHash | typeof MemoReturn, value: string) + constructor(type: typeof MemoHash | typeof MemoReturn, value: Buffer) + + type: string; + value: null | string | Buffer; + + toXDRObject(): xdr.Memo; + } + + 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: OperationType; + source: string | null; + } + interface AccountMerge extends Operation { + type: OperationType.accountMerge; + destination: string; + } + interface AccountMergeOptions { + destination: string; + source?: string; + } + function accountMerge(options: AccountMergeOptions): xdr.Operation; + + interface AllowTrust extends Operation { + type: OperationType.allowTrust; + trustor: string; + assetCode: string; + authorize: boolean; + } + interface AllowTrustOptions { + trustor: string; + assetCode: string; + authorize: boolean; + source?: string; + } + function allowTrust(options: AllowTrustOptions): xdr.Operation; + + interface ChangeTrust extends Operation { + type: OperationType.changeTrust; + line: Asset; + limit: string | number; + } + interface ChangeTrustOptions { + asset: Asset; + limit?: string; + source?: string; + } + function changeTrust(options: ChangeTrustOptions): xdr.Operation; + + interface CreateAccount extends Operation { + type: OperationType.createAccount; + source: string; + destination: string; + startingBalance: string | number; + } + interface CreateAccountOptions { + 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 { + selling: Asset; + buying: Asset; + amount: string; + price: number | string | object; + source?: string; + } + function createPassiveOffer(options: CreatePassiveOfferOptions): xdr.Operation; + + interface Inflation extends Operation { + type: OperationType.inflation; + } + function inflation(options: { source?: string }): xdr.Operation; + + interface ManageData extends Operation { + type: OperationType.manageData; + name: string; + value: Buffer; + } + interface ManageDataOptions { + name: string; + value: string | Buffer; + source?: string; + } + function manageData(options: ManageDataOptions): xdr.Operation; + + interface ManageOffer extends Operation { + type: OperationType.manageOffer; + selling: Asset; + buying: Asset; + amount: string | number; + price: string | number; + offerId: string; + } + interface ManageOfferOptions extends CreatePassiveOfferOptions { + offerId?: number | string; + } + 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 { + sendAsset: Asset; + sendMax: string; + destination: string; + destAsset: Asset; + destAmount: string; + path: Asset[]; + source?: string; + } + 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; + amount: string; + source?: 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 Signer { + ed25519PublicKey?: string; + sha256Hash?: Buffer | string; + preAuthTx?: Buffer | string; + weight?: number | string; + } + interface SetOptions extends Operation { + type: OperationType.setOptions; + inflationDest?: string; + clearFlags?: AuthFlags; + setFlags?: AuthFlags; + masterWeight?: number | string; + lowThreshold?: number | string; + medThreshold?: number | string; + highThreshold?: number | string; + homeDomain?: string; + signer?: Signer; + } + interface SetOptionsOptions { + inflationDest?: string; + clearFlags?: AuthFlags; + setFlags?: AuthFlags; + masterWeight?: number | string; + lowThreshold?: number | string; + medThreshold?: number | string; + highThreshold?: number | string; + signer?: Signer; + homeDomain?: string; + source?: string; + } + function setOptions(options: SetOptionsOptions): xdr.Operation; + + interface BumpSequence extends Operation { + type: OperationType.bumpSequence; + bumpTo: string; + } + interface BumpSequenceOptions { + bumpTo: string; + source?: 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: TransactionOperation[]; + sequence: number; + fee: number; + source: string; + memo: Memo; + 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; + } + } } +// Re-export StellarBase +export import Account = StellarBase.Account; +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 MemoType = StellarBase.MemoType; +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 TransactionOperation = StellarBase.TransactionOperation; +export import hash = StellarBase.hash; +export import sign = StellarBase.sign; +export import verify = StellarBase.verify; +export import Transaction = StellarBase.Transaction; +export import TransactionBuilder = StellarBase.TransactionBuilder; +export import xdr = StellarBase.xdr; + export class CallBuilder { constructor(serverUrl: string) call(): Promise>; cursor(cursor: string): this; - limit(limit: number): this; + limit(limit: number | string): this; order(direction: 'asc' | 'desc'): this; stream(options?: { onmessage?: (record: T) => void, onerror?: (error: Error) => void }): () => void; } @@ -65,6 +484,25 @@ export type CallFunction = () => Promise; export type CallCollectionFunction = (options?: CallFunctionTemplateOptions) => Promise>; +export enum ASSET_TYPE { + native = 'native', + credit4 = 'credit_alphanum4', + credit12 = 'credit_alphanum12', +} + +export interface BalanceLineNative { + balance: string; + asset_type: ASSET_TYPE.native; +} +export interface BalanceLineAsset { + balance: string; + limit: string; + asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_code: string; + asset_issuer: string; +} +export type BalanceLine = BalanceLineNative | BalanceLineAsset; + export interface AccountRecord extends Record { id: string; paging_token: string; @@ -80,19 +518,7 @@ export interface AccountRecord extends Record { auth_required: boolean auth_revocable: boolean }; - balances: Array< - { - balance: string - asset_type: 'native' - } | - { - balance: string - limit: string - asset_type: 'credit_alphanum4' | 'credit_alphanum12' - asset_code: string - asset_issuer: string - } - >; + balances: BalanceLine[]; signers: Array< { public_key: string @@ -111,7 +537,7 @@ export interface AccountRecord extends Record { } export interface AssetRecord extends Record { - asset_type: 'credit_alphanum4' | 'credit_alphanum12'; + asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; asset_code: string; asset_issuer: string; paging_token: string; @@ -129,6 +555,7 @@ export interface EffectRecord extends Record { starting_balance: string; type_i: string; type: string; + amount?: any; operation?: CallFunction; precedes?: CallFunction; @@ -178,6 +605,7 @@ export interface BaseOperationRecord extends Record { paging_token: string; type: string; type_i: number; + source_account: string; self: CallFunction; succeeds: CallFunction; @@ -390,6 +818,8 @@ export interface TransactionRecord extends Record { result_xdr: string; result_meta_xdr: string; memo: string; + envelope: any; + memo_type: any; account: CallFunction; effects: CallCollectionFunction; @@ -419,19 +849,7 @@ export class AccountResponse implements AccountRecord { auth_required: boolean auth_revocable: boolean }; - balances: Array< - { - balance: string - asset_type: 'native' - } | - { - balance: string - limit: string - asset_type: 'credit_alphanum4' | 'credit_alphanum12' - asset_code: string - asset_issuer: string - } - >; + balances: BalanceLine[]; signers: Array< { public_key: string @@ -442,6 +860,7 @@ export class AccountResponse implements AccountRecord { data_attr: { [key: string]: string }; + inflation_destination?: any; effects: CallCollectionFunction; offers: CallCollectionFunction; @@ -454,23 +873,6 @@ export class AccountResponse implements AccountRecord { incrementSequenceNumber(): void; } -export class Asset { - static native(): Asset; - static fromOperation(xdr: xdr.Asset): Asset; - - constructor(code: string, issuer: string) - - getCode(): string; - getIssuer(): string; - getAssetType(): 'native' | 'credit_alphanum4' | 'credit_alphanum12'; - isNative(): boolean; - equals(other: Asset): boolean; - toXDRObject(): xdr.Asset; - - code: string; - issuer: string; -} - export class AssetsCallBuilder extends CallBuilder { forCode(value: string): this; forIssuer(value: string): this; @@ -510,267 +912,8 @@ export class FederationServer { export class LedgerCallBuilder extends CallBuilder { } -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: 'none'); - constructor(type: 'id' | 'text' | 'hash' | 'return', value: string) - constructor(type: 'hash' | 'return', value: Buffer) - - type: string; - value: null | string | Buffer; - - toXDRObject(): xdr.Memo; -} - -export const MemoNone = 'none'; -export const MemoID = 'id'; -export const MemoText = 'text'; -export const MemoHash = 'hash'; -export const MemoReturn = 'return'; - -export enum Networks { - PUBLIC = 'Public Global Stellar Network ; September 2015', - TESTNET = 'Test SDF Network ; September 2015', -} - -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 class OfferCallBuilder extends CallBuilder { } -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: OperationType; - source: string | null; - } - interface AccountMerge extends Operation { - type: OperationType.accountMerge; - destination: string; - } - interface AccountMergeOptions { - destination: string; - source?: string; - } - function accountMerge(options: AccountMergeOptions): xdr.Operation; - - interface AllowTrust extends Operation { - type: OperationType.allowTrust; - trustor: string; - assetCode: string; - authorize: boolean; - } - interface AllowTrustOptions { - trustor: string; - assetCode: string; - authorize: boolean; - source?: string; - } - function allowTrust(options: AllowTrustOptions): xdr.Operation; - - interface ChangeTrust extends Operation { - type: OperationType.changeTrust; - line: Asset; - limit: string | number; - } - interface ChangeTrustOptions { - asset: Asset; - limit?: string; - source?: string; - } - function changeTrust(options: ChangeTrustOptions): xdr.Operation; - - interface CreateAccount extends Operation { - type: OperationType.createAccount; - source: string; - destination: string; - startingBalance: string | number; - } - interface CreateAccountOptions { - 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 { - selling: Asset; - buying: Asset; - amount: string; - price: number | string | object; - source?: string; - } - function createPassiveOffer(options: CreatePassiveOfferOptions): xdr.Operation; - - interface Inflation extends Operation { - type: OperationType.inflation; - } - function inflation(options: { source?: string }): xdr.Operation; - - interface ManageData extends Operation { - type: OperationType.manageData; - name: string; - value: string; - } - interface ManageDataOptions { - name: string; - value: string | Buffer; - source?: string; - } - function manageData(options: ManageDataOptions): xdr.Operation; - - interface ManageOffer extends Operation { - type: OperationType.manageOffer; - selling: Asset; - buying: Asset; - amount: string | number; - price: string | number; - offerId: string; - } - interface ManageOfferOptions extends CreatePassiveOfferOptions { - offerId: number | string; - } - 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 { - sendAsset: Asset; - sendMax: string; - destination: string; - destAsset: Asset; - destAmount: string; - path: Asset[]; - source?: string; - } - 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; - amount: string; - source?: 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 Signer { - ed25519PublicKey?: string; - sha256Hash?: Buffer | string; - preAuthTx?: Buffer | string; - weight?: number | string; - } - interface SetOptions extends Operation { - type: OperationType.setOptions; - inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; - masterWeight?: number | string; - lowThreshold?: number | string; - medThreshold?: number | string; - highThreshold?: number | string; - homeDomain?: string; - signer?: Signer; - } - interface SetOptionsOptions { - inflationDest?: string; - clearFlags?: AuthFlags; - setFlags?: AuthFlags; - masterWeight?: number | string; - lowThreshold?: number | string; - medThreshold?: number | string; - highThreshold?: number | string; - signer?: Signer; - homeDomain?: string; - source?: string; - } - function setOptions(options: SetOptionsOptions): xdr.Operation; - - interface BumpSequence extends Operation { - type: OperationType.bumpSequence; - bumpTo: string; - } - interface BumpSequenceOptions { - bumpTo: string; - source?: string; - } - function bumpSequence(options: BumpSequenceOptions): xdr.Operation; - - function fromXDRObject(xdrOperation: xdr.Operation): T; -} - export class OperationCallBuilder extends CallBuilder { forAccount(accountId: string): this; forLedger(sequence: string): this; @@ -784,8 +927,12 @@ export class PaymentCallBuilder extends CallBuilder { forTransaction(transactionId: string): this; } +export interface ServerOptions { + allowHttp: boolean; +} + export class Server { - constructor(serverURL: string, options?: { allowHttp: boolean }) + constructor(serverURL: string, options?: ServerOptions) accounts(): AccountCallBuilder; assets(): AssetsCallBuilder; effects(): EffectCallBuilder; @@ -801,7 +948,7 @@ export class Server { destinationAmount: string, ): PathCallBuilder; payments(): PaymentCallBuilder; - submitTransaction(transaction: Transaction): Promise; + submitTransaction(transaction: Transaction): Promise; tradeAggregation( base: Asset, counter: Asset, @@ -811,22 +958,8 @@ export class Server { ): TradeAggregationCallBuilder; trades(): TradesCallBuilder; transactions(): TransactionCallBuilder; -} -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; + serverURL: any; // TODO: require("urijs") } export class TradeAggregationCallBuilder extends CallBuilder { } @@ -835,86 +968,12 @@ export class TradesCallBuilder extends CallBuilder { forOffer(offerId: string): this; } -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: TransactionOperation[]; - sequence: number; - fee: number; - source: string; - memo: Memo; - 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 class TransactionCallBuilder extends CallBuilder { transaction(transactionId: string): this; forAccount(accountId: string): this; forLedger(sequence: string | number): this; } -export class Keypair { - static fromRawEd25519Seed(secretSeed: Buffer): 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; - signatureHint(): xdr.SignatureHint; - verify(data: Buffer, signature: Buffer): boolean; -} - -export namespace xdr { - class XDRStruct { - static fromXDR(xdr: Buffer): XDRStruct; - - toXDR(): Buffer; - toXDR(encoding: string): string; - } - class Operation extends XDRStruct { } - class Asset extends XDRStruct { } - class Memo extends XDRStruct { } - class TransactionEnvelope extends XDRStruct { } - class DecoratedSignature extends XDRStruct { - constructor(keys: { hint: SignatureHint, signature: Signature }) - - hint(): SignatureHint; - signature(): Buffer; - } - type SignatureHint = Buffer; - type Signature = Buffer; -} - export namespace StellarTomlResolver { interface StellarTomlResolveOptions { allowHttp?: boolean; From 84b8d1a7f16a561d8edda3d2b25c744b2b696624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 14:34:04 +0200 Subject: [PATCH 02/12] fix: FastSigning, hash, sign, verify --- types/stellar-sdk/index.d.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 63748e65be..92ca938e2b 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -12,11 +12,6 @@ /// export namespace StellarBase { - const hash: any; // TODO - const sign: any; // TODO - const verify: any; // TODO - const FastSigning: any; // TODO - class Account { constructor(accountId: string, sequence: string | number) accountId(): string; @@ -41,6 +36,8 @@ export namespace StellarBase { issuer: string; } + const FastSigning: boolean; + class Keypair { static fromRawEd25519Seed(secretSeed: Buffer): Keypair; static fromBase58Seed(secretSeed: string): Keypair; @@ -409,6 +406,10 @@ export namespace StellarBase { 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; } // Re-export StellarBase @@ -428,12 +429,12 @@ 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 Transaction = StellarBase.Transaction; -export import TransactionBuilder = StellarBase.TransactionBuilder; export import xdr = StellarBase.xdr; export class CallBuilder { From 8b828727e769ada075d683d206fc3c5f298115ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 14:40:08 +0200 Subject: [PATCH 03/12] fix: NetworkError --- types/stellar-sdk/index.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 92ca938e2b..acb05e02a9 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -437,6 +437,15 @@ export import sign = StellarBase.sign; export import verify = StellarBase.verify; export import xdr = StellarBase.xdr; +export class NetworkError extends Error { + private response: any; + constructor(message: string, response: any) + getResponse(): any; +} +export class NotFoundError extends NetworkError {} +export class BadRequestError extends NetworkError {} +export class BadResponseError extends NetworkError {} + export class CallBuilder { constructor(serverUrl: string) call(): Promise>; From 8d8c9f33ee8ec180b1c34ed10f2b2b024d8058a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 14:48:23 +0200 Subject: [PATCH 04/12] refactor: move Config --- types/stellar-sdk/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index acb05e02a9..4b988d76aa 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -446,6 +446,12 @@ export class NotFoundError extends NetworkError {} export class BadRequestError extends NetworkError {} export class BadResponseError extends NetworkError {} +export namespace Config { + function setAllowHttp(allow: boolean): void; + function isAllowHttp(): boolean; + function setDefault(): void; +} + export class CallBuilder { constructor(serverUrl: string) call(): Promise>; @@ -888,12 +894,6 @@ export class AssetsCallBuilder extends CallBuilder { forIssuer(value: string): this; } -export namespace Config { - function setAllowHttp(allow: boolean): void; - function isAllowHttp(): boolean; - function setDefault(): void; -} - export class EffectCallBuilder extends CallBuilder { forAccount(accountId: string): this; forLedger(sequence: string): this; From 52182e8890bf8ff09bfa0fd170138135b99466af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 14:49:28 +0200 Subject: [PATCH 05/12] refactor: move FederationServer --- types/stellar-sdk/index.d.ts | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 4b988d76aa..85201e47de 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -901,25 +901,6 @@ export class EffectCallBuilder extends CallBuilder { forTransaction(transactionId: string): this; } -export interface FederationRecord { - account_id: string; - memo_type?: string; - memo?: string; -} - -export interface FederationOptions { - allowHttp: boolean; -} -export class FederationServer { - static createForDomain(domain: string, options?: FederationOptions): Promise; - static resolve(value: string, options?: FederationOptions): Promise; - - constructor(serverURL: string, domain: string, options?: FederationOptions) - resolveAccountId(account: string): Promise; - resolveAddress(address: string): Promise; - resolveTransactionId(transactionId: string): Promise; -} - export class LedgerCallBuilder extends CallBuilder { } export class OfferCallBuilder extends CallBuilder { } @@ -984,6 +965,25 @@ export class TransactionCallBuilder extends CallBuilder { forLedger(sequence: string | number): this; } +export interface FederationRecord { + account_id: string; + memo_type?: string; + memo?: string; +} + +export interface FederationOptions { + allowHttp: boolean; +} +export class FederationServer { + static createForDomain(domain: string, options?: FederationOptions): Promise; + static resolve(value: string, options?: FederationOptions): Promise; + + constructor(serverURL: string, domain: string, options?: FederationOptions) + resolveAccountId(account: string): Promise; + resolveAddress(address: string): Promise; + resolveTransactionId(transactionId: string): Promise; +} + export namespace StellarTomlResolver { interface StellarTomlResolveOptions { allowHttp?: boolean; From cbedf99049f47194205124c0123265165a1fbca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 15:03:37 +0200 Subject: [PATCH 06/12] feat: introduce and organize namespace Server BREAKING CHANGES: Need to prefix `Server.` to plenty of stuff. --- types/stellar-sdk/index.d.ts | 992 ++++++++++++++++++----------------- 1 file changed, 497 insertions(+), 495 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index 85201e47de..a8b8317c2d 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -412,7 +412,7 @@ export namespace StellarBase { function verify(data: Buffer, signature: Buffer, rawPublicKey: Buffer): boolean; } -// Re-export StellarBase +// Re-StellarBase export import Account = StellarBase.Account; export import Asset = StellarBase.Asset; export import FastSigning = StellarBase.FastSigning; @@ -452,517 +452,519 @@ export namespace Config { function setDefault(): void; } -export class CallBuilder { - constructor(serverUrl: string) - call(): Promise>; - cursor(cursor: string): this; - limit(limit: number | string): this; - order(direction: 'asc' | 'desc'): this; - stream(options?: { onmessage?: (record: T) => void, onerror?: (error: Error) => void }): () => void; -} - -export interface CollectionPage { - records: T[]; - next: () => Promise>; - prev: () => Promise>; -} - -export interface Record { - _links: { - [key: string]: RecordLink - }; -} - -export interface RecordLink { - href: string; - templated?: boolean; -} - -/* Due to a bug with the recursive function requests */ -export interface CollectionRecord { - _links: { - next: RecordLink - prev: RecordLink - self: RecordLink - }; - _embedded: { - records: T[] - }; -} - -export interface CallFunctionTemplateOptions { - cursor?: string | number; - limit?: number; - order?: 'asc' | 'desc'; -} - -export type CallFunction = () => Promise; -export type CallCollectionFunction = - (options?: CallFunctionTemplateOptions) => Promise>; - -export enum ASSET_TYPE { - native = 'native', - credit4 = 'credit_alphanum4', - credit12 = 'credit_alphanum12', -} - -export interface BalanceLineNative { - balance: string; - asset_type: ASSET_TYPE.native; -} -export interface BalanceLineAsset { - balance: string; - limit: string; - asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; - asset_code: string; - asset_issuer: string; -} -export type BalanceLine = BalanceLineNative | BalanceLineAsset; - -export interface AccountRecord extends Record { - id: string; - paging_token: string; - 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 - } - >; - data: (options: {value: string}) => Promise<{value: string}>; - data_attr: { - [key: string]: string - }; - effects: CallCollectionFunction; - offers: CallCollectionFunction; - operations: CallCollectionFunction; - payments: CallCollectionFunction; - trades: CallCollectionFunction; -} - -export interface AssetRecord extends Record { - 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 - }; -} - -export interface EffectRecord extends Record { - account: string; - paging_token: string; - starting_balance: string; - type_i: string; - type: string; - amount?: any; - - operation?: CallFunction; - precedes?: CallFunction; - succeeds?: CallFunction; -} - -export interface LedgerRecord extends Record { - id: string; - paging_token: string; - hash: string; - prev_hash: string; - sequence: number; - transaction_count: number; - operation_count: number; - closed_at: string; - total_coins: string; - fee_pool: string; - base_fee: number; - base_reserve: string; - max_tx_set_size: number; - protocol_version: number; - header_xdr: string; - base_fee_in_stroops: number; - base_reserve_in_stroops: number; - - effects: CallCollectionFunction; - operations: CallCollectionFunction; - self: CallFunction; - transactions: CallCollectionFunction; -} - -export interface OfferRecord extends Record { - id: string; - paging_token: string; - seller_attr: string; - selling: Asset; - buying: Asset; - amount: string; - price_r: { numerator: number, denominator: number }; - price: string; - - seller?: CallFunction; -} - -export 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; -} - -export interface CreateAccountOperationRecord extends BaseOperationRecord { - type: 'create_account'; - account: string; - funder: string; - starting_balance: string; -} - -export interface PaymentOperationRecord extends BaseOperationRecord { - type: 'payment'; - from: string; - to: string; - asset_type: string; - asset_code?: string; - asset_issuer?: string; - amount: string; - - sender: CallFunction; - receiver: CallFunction; -} - -export 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; -} - -export 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; -} - -export 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; -} - -export 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')>; -} - -export interface ChangeTrustOperationRecord extends BaseOperationRecord { - type: 'change_trust'; - asset_code: string; - asset_issuer: string; - asset_type: string; - trustee: string; - trustor: string; - limit: string; -} - -export interface AllowTrustOperationRecord extends BaseOperationRecord { - type: 'allow_trust'; - asset_code: string; - asset_issuer: string; - asset_type: string; - authorize: boolean; - trustee: string; - trustor: string; -} - -export interface AccountMergeOperationRecord extends BaseOperationRecord { - type: 'account_merge'; - into: string; -} - -export interface InflationOperationRecord extends BaseOperationRecord { - type: 'inflation'; -} - -export interface ManageDataOperationRecord extends BaseOperationRecord { - type: 'manage_data'; - name: string; - value: string; -} - -export interface BumpSequenceOperationRecord extends BaseOperationRecord { - type: 'bump_sequence'; - bump_to: string; -} - -export type OperationRecord = CreateAccountOperationRecord - | PaymentOperationRecord - | PathPaymentOperationRecord - | ManageOfferOperationRecord - | PassiveOfferOperationRecord - | SetOptionsOperationRecord - | ChangeTrustOperationRecord - | AllowTrustOperationRecord - | AccountMergeOperationRecord - | InflationOperationRecord - | ManageDataOperationRecord - | BumpSequenceOperationRecord; - -export interface OrderbookRecord extends Record { - bids: Array<{ price_r: {}, price: number, amount: string }>; - asks: Array<{ price_r: {}, price: number, amount: string }>; - selling: Asset; - buying: Asset; -} - -export interface PaymentPathRecord extends Record { - path: Array<{ - asset_code: string - asset_issuer: string - asset_type: string - }>; - source_amount: string; - source_asset_type: string; - source_asset_code: string; - source_asset_issuer: string; - destination_amount: string; - destination_asset_type: string; - destination_asset_code: string; - destination_asset_issuer: string; -} - -export interface TradeRecord extends Record { - id: string; - paging_token: string; - ledger_close_time: string; - base_account: string; - base_amount: string; - base_asset_type: string; - base_asset_code: string; - base_asset_issuer: string; - counter_account: string; - counter_amount: string; - counter_asset_type: string; - counter_asset_code: string; - counter_asset_issuer: string; - base_is_seller: boolean; - - base: CallFunction; - counter: CallFunction; - operation: CallFunction; -} - -export interface TradeAggregationRecord extends Record { - timestamp: string; - trade_count: number; - base_volume: string; - counter_volume: string; - avg: string; - high: string; - low: string; - open: string; - close: string; -} - -export interface TransactionRecord extends Record { - 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; - - account: CallFunction; - effects: CallCollectionFunction; - ledger: CallFunction; - operations: CallCollectionFunction; - precedes: CallFunction; - self: CallFunction; - succeeds: CallFunction; -} - -export class AccountCallBuilder extends CallBuilder { - accountId(id: string): this; -} -export class AccountResponse implements AccountRecord { - _links: { [key: string]: { href: string } }; - 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 - } - >; - data: (options: {value: string}) => Promise<{value: string}>; - data_attr: { - [key: string]: string - }; - inflation_destination?: any; - - effects: CallCollectionFunction; - offers: CallCollectionFunction; - operations: CallCollectionFunction; - payments: CallCollectionFunction; - trades: CallCollectionFunction; - constructor(response: AccountRecord) - accountId(): string; - sequenceNumber(): string; - incrementSequenceNumber(): void; -} - -export class AssetsCallBuilder extends CallBuilder { - forCode(value: string): this; - forIssuer(value: string): this; -} - -export class EffectCallBuilder extends CallBuilder { - forAccount(accountId: string): this; - forLedger(sequence: string): this; - forOperation(operationId: number): this; - forTransaction(transactionId: string): this; -} - -export class LedgerCallBuilder extends CallBuilder { } - -export class OfferCallBuilder extends CallBuilder { } - -export class OperationCallBuilder extends CallBuilder { - forAccount(accountId: string): this; - forLedger(sequence: string): this; - forTransaction(transactionId: string): this; -} -export class OrderbookCallBuilder extends CallBuilder { } -export class PathCallBuilder extends CallBuilder { } -export class PaymentCallBuilder extends CallBuilder { - forAccount(accountId: string): this; - forLedger(sequence: string): this; - forTransaction(transactionId: string): this; -} - -export interface ServerOptions { - allowHttp: boolean; -} - export class Server { - constructor(serverURL: string, options?: ServerOptions) - accounts(): AccountCallBuilder; - assets(): AssetsCallBuilder; - effects(): EffectCallBuilder; - ledgers(): LedgerCallBuilder; - loadAccount(accountId: string): Promise; - offers(resource: string, ...parameters: string[]): OfferCallBuilder; - operations(): OperationCallBuilder; - orderbook(selling: Asset, buying: Asset): OrderbookCallBuilder; + constructor(serverURL: string, options?: Server.ServerOptions) + accounts(): Server.AccountCallBuilder; + assets(): Server.AssetsCallBuilder; + effects(): Server.EffectCallBuilder; + ledgers(): Server.LedgerCallBuilder; + loadAccount(accountId: string): Promise; + offers(resource: string, ...parameters: string[]): Server.OfferCallBuilder; + operations(): Server.OperationCallBuilder; + orderbook(selling: Asset, buying: Asset): Server.OrderbookCallBuilder; paths( source: string, destination: string, destinationAsset: Asset, destinationAmount: string, - ): PathCallBuilder; - payments(): PaymentCallBuilder; - submitTransaction(transaction: Transaction): Promise; + ): Server.PathCallBuilder; + payments(): Server.PaymentCallBuilder; + submitTransaction(transaction: Transaction): Promise; tradeAggregation( base: Asset, counter: Asset, startTime: Date, endTime: Date, resolution: Date, - ): TradeAggregationCallBuilder; - trades(): TradesCallBuilder; - transactions(): TransactionCallBuilder; + ): Server.TradeAggregationCallBuilder; + trades(): Server.TradesCallBuilder; + transactions(): Server.TransactionCallBuilder; serverURL: any; // TODO: require("urijs") } -export class TradeAggregationCallBuilder extends CallBuilder { } -export class TradesCallBuilder extends CallBuilder { - forAssetPair(base: Asset, counter: Asset): this; - forOffer(offerId: string): this; -} +export namespace Server { + class CallBuilder { + constructor(serverUrl: string) + call(): Promise>; + cursor(cursor: string): this; + limit(limit: number | string): this; + order(direction: 'asc' | 'desc'): this; + stream(options?: { onmessage?: (record: T) => void, onerror?: (error: Error) => void }): () => void; + } -export class TransactionCallBuilder extends CallBuilder { - transaction(transactionId: string): this; - forAccount(accountId: string): this; - forLedger(sequence: string | number): this; + 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 { + _links: { + next: RecordLink + prev: RecordLink + self: RecordLink + }; + _embedded: { + records: T[] + }; + } + + interface CallFunctionTemplateOptions { + cursor?: string | number; + limit?: number; + order?: 'asc' | 'desc'; + } + + type CallFunction = () => Promise; + type CallCollectionFunction = + (options?: CallFunctionTemplateOptions) => Promise>; + + enum ASSET_TYPE { + native = 'native', + credit4 = 'credit_alphanum4', + credit12 = 'credit_alphanum12', + } + + interface BalanceLineNative { + balance: string; + asset_type: ASSET_TYPE.native; + } + interface BalanceLineAsset { + balance: string; + limit: string; + asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_code: string; + asset_issuer: string; + } + type BalanceLine = BalanceLineNative | BalanceLineAsset; + + interface AccountRecord extends Record { + 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 + } + >; + data: (options: {value: string}) => Promise<{value: string}>; + data_attr: { + [key: string]: string + }; + effects: CallCollectionFunction; + offers: CallCollectionFunction; + operations: CallCollectionFunction; + payments: CallCollectionFunction; + trades: CallCollectionFunction; + } + + interface AssetRecord extends Record { + 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 + }; + } + + interface EffectRecord extends Record { + account: string; + paging_token: string; + starting_balance: string; + type_i: string; + type: string; + amount?: any; + + operation?: CallFunction; + precedes?: CallFunction; + succeeds?: CallFunction; + } + + interface LedgerRecord extends Record { + id: string; + paging_token: string; + hash: string; + prev_hash: string; + sequence: number; + transaction_count: number; + operation_count: number; + closed_at: string; + total_coins: string; + fee_pool: string; + base_fee: number; + base_reserve: string; + max_tx_set_size: number; + protocol_version: number; + header_xdr: string; + base_fee_in_stroops: number; + base_reserve_in_stroops: number; + + effects: CallCollectionFunction; + operations: CallCollectionFunction; + self: CallFunction; + transactions: CallCollectionFunction; + } + + interface OfferRecord extends Record { + id: string; + paging_token: string; + seller_attr: string; + selling: Asset; + buying: Asset; + amount: string; + price_r: { numerator: number, denominator: number }; + price: string; + + 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; + } + + 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; + + 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: string; + } + + interface BumpSequenceOperationRecord extends BaseOperationRecord { + type: 'bump_sequence'; + bump_to: string; + } + + type OperationRecord = CreateAccountOperationRecord + | PaymentOperationRecord + | PathPaymentOperationRecord + | ManageOfferOperationRecord + | PassiveOfferOperationRecord + | SetOptionsOperationRecord + | ChangeTrustOperationRecord + | AllowTrustOperationRecord + | AccountMergeOperationRecord + | InflationOperationRecord + | ManageDataOperationRecord + | BumpSequenceOperationRecord; + + interface OrderbookRecord extends Record { + bids: Array<{ price_r: {}, price: number, amount: string }>; + asks: Array<{ price_r: {}, price: number, amount: string }>; + selling: Asset; + buying: Asset; + } + + interface PaymentPathRecord extends Record { + path: Array<{ + asset_code: string + asset_issuer: string + asset_type: string + }>; + source_amount: string; + source_asset_type: string; + source_asset_code: string; + source_asset_issuer: string; + destination_amount: string; + destination_asset_type: string; + destination_asset_code: string; + destination_asset_issuer: string; + } + + interface TradeRecord extends Record { + id: string; + paging_token: string; + ledger_close_time: string; + base_account: string; + base_amount: string; + base_asset_type: string; + base_asset_code: string; + base_asset_issuer: string; + counter_account: string; + counter_amount: string; + counter_asset_type: string; + counter_asset_code: string; + counter_asset_issuer: string; + base_is_seller: boolean; + + base: CallFunction; + counter: CallFunction; + operation: CallFunction; + } + + interface TradeAggregationRecord extends Record { + timestamp: string; + trade_count: number; + base_volume: string; + counter_volume: string; + avg: string; + high: string; + low: string; + open: string; + close: string; + } + + interface TransactionRecord extends Record { + 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; + + account: CallFunction; + effects: CallCollectionFunction; + ledger: CallFunction; + operations: CallCollectionFunction; + precedes: CallFunction; + self: CallFunction; + succeeds: CallFunction; + } + + class AccountCallBuilder extends CallBuilder { + accountId(id: string): this; + } + class AccountResponse implements AccountRecord { + _links: { [key: string]: { href: string } }; + 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 + } + >; + data: (options: {value: string}) => Promise<{value: string}>; + data_attr: { + [key: string]: string + }; + inflation_destination?: any; + + effects: CallCollectionFunction; + offers: CallCollectionFunction; + operations: CallCollectionFunction; + payments: CallCollectionFunction; + trades: CallCollectionFunction; + constructor(response: AccountRecord) + accountId(): string; + sequenceNumber(): string; + incrementSequenceNumber(): void; + } + + class AssetsCallBuilder extends CallBuilder { + forCode(value: string): this; + forIssuer(value: string): this; + } + + class EffectCallBuilder extends CallBuilder { + forAccount(accountId: string): this; + forLedger(sequence: string): this; + forOperation(operationId: number): this; + forTransaction(transactionId: string): this; + } + + class LedgerCallBuilder extends CallBuilder { } + + class OfferCallBuilder extends CallBuilder { } + + class OperationCallBuilder extends CallBuilder { + forAccount(accountId: string): this; + forLedger(sequence: string): this; + forTransaction(transactionId: string): this; + } + class OrderbookCallBuilder extends CallBuilder { } + class PathCallBuilder extends CallBuilder { } + class PaymentCallBuilder extends CallBuilder { + forAccount(accountId: string): this; + forLedger(sequence: string): this; + forTransaction(transactionId: string): this; + } + + interface ServerOptions { + allowHttp: boolean; + } + + class TradeAggregationCallBuilder extends CallBuilder { } + class TradesCallBuilder extends CallBuilder { + forAssetPair(base: Asset, counter: Asset): this; + forOffer(offerId: string): this; + } + + class TransactionCallBuilder extends CallBuilder { + transaction(transactionId: string): this; + forAccount(accountId: string): this; + forLedger(sequence: string | number): this; + } } export interface FederationRecord { From 3e4539f9d1d92c8c3575112f7e2165a48db473f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 15:04:51 +0200 Subject: [PATCH 07/12] fix: *CallBuilder are effectively abstract class --- types/stellar-sdk/index.d.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index a8b8317c2d..ff93930fe9 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -484,7 +484,7 @@ export class Server { } export namespace Server { - class CallBuilder { + abstract class CallBuilder { constructor(serverUrl: string) call(): Promise>; cursor(cursor: string): this; @@ -878,7 +878,7 @@ export namespace Server { succeeds: CallFunction; } - class AccountCallBuilder extends CallBuilder { + abstract class AccountCallBuilder extends CallBuilder { accountId(id: string): this; } class AccountResponse implements AccountRecord { @@ -921,30 +921,30 @@ export namespace Server { incrementSequenceNumber(): void; } - class AssetsCallBuilder extends CallBuilder { + abstract class AssetsCallBuilder extends CallBuilder { forCode(value: string): this; forIssuer(value: string): this; } - class EffectCallBuilder extends CallBuilder { + abstract class EffectCallBuilder extends CallBuilder { forAccount(accountId: string): this; forLedger(sequence: string): this; forOperation(operationId: number): this; forTransaction(transactionId: string): this; } - class LedgerCallBuilder extends CallBuilder { } + abstract class LedgerCallBuilder extends CallBuilder { } - class OfferCallBuilder extends CallBuilder { } + abstract class OfferCallBuilder extends CallBuilder { } - class OperationCallBuilder extends CallBuilder { + abstract class OperationCallBuilder extends CallBuilder { forAccount(accountId: string): this; forLedger(sequence: string): this; forTransaction(transactionId: string): this; } - class OrderbookCallBuilder extends CallBuilder { } - class PathCallBuilder extends CallBuilder { } - class PaymentCallBuilder extends CallBuilder { + abstract class OrderbookCallBuilder extends CallBuilder { } + abstract class PathCallBuilder extends CallBuilder { } + abstract class PaymentCallBuilder extends CallBuilder { forAccount(accountId: string): this; forLedger(sequence: string): this; forTransaction(transactionId: string): this; @@ -954,13 +954,13 @@ export namespace Server { allowHttp: boolean; } - class TradeAggregationCallBuilder extends CallBuilder { } - class TradesCallBuilder extends CallBuilder { + abstract class TradeAggregationCallBuilder extends CallBuilder { } + abstract class TradesCallBuilder extends CallBuilder { forAssetPair(base: Asset, counter: Asset): this; forOffer(offerId: string): this; } - class TransactionCallBuilder extends CallBuilder { + abstract class TransactionCallBuilder extends CallBuilder { transaction(transactionId: string): this; forAccount(accountId: string): this; forLedger(sequence: string | number): this; From 72a40070b0bc8b0454458cd83f887162bce97870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 15:07:42 +0200 Subject: [PATCH 08/12] feat: introduce namespace FederationServer BREAKING CHANGES: Rename interfaces to - `FederationRecord` -> `FederationServer.Record` - `FederationOptions` -> `FederationServer.Options` --- types/stellar-sdk/index.d.ts | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index ff93930fe9..bc1980d37f 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -967,23 +967,25 @@ export namespace Server { } } -export interface FederationRecord { - account_id: string; - memo_type?: string; - memo?: string; -} - -export interface FederationOptions { - allowHttp: boolean; -} export class FederationServer { - static createForDomain(domain: string, options?: FederationOptions): Promise; - static resolve(value: string, options?: FederationOptions): Promise; + static createForDomain(domain: string, options?: FederationServer.Options): Promise; + static resolve(value: string, options?: FederationServer.Options): Promise; - constructor(serverURL: string, domain: string, options?: FederationOptions) - resolveAccountId(account: string): Promise; - resolveAddress(address: string): Promise; - resolveTransactionId(transactionId: string): Promise; + constructor(serverURL: string, domain: string, options?: FederationServer.Options) + resolveAccountId(account: string): Promise; + resolveAddress(address: string): Promise; + resolveTransactionId(transactionId: string): Promise; +} +export namespace FederationServer { + interface Record { + account_id: string; + memo_type?: string; + memo?: string; + } + + interface Options { + allowHttp: boolean; + } } export namespace StellarTomlResolver { From a19c1e5cdad685417c1b841dbee322dfb5875a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 15:13:03 +0200 Subject: [PATCH 09/12] fix: ASSET_TYPE --- types/stellar-sdk/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index bc1980d37f..d82fef63dc 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -19,6 +19,11 @@ export namespace StellarBase { incrementSequenceNumber(): void; } + enum ASSET_TYPE { + native = 'native', + credit4 = 'credit_alphanum4', + credit12 = 'credit_alphanum12', + } class Asset { static native(): Asset; static fromOperation(xdr: xdr.Asset): Asset; @@ -414,6 +419,7 @@ export namespace StellarBase { // 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; @@ -532,12 +538,6 @@ export namespace Server { type CallCollectionFunction = (options?: CallFunctionTemplateOptions) => Promise>; - enum ASSET_TYPE { - native = 'native', - credit4 = 'credit_alphanum4', - credit12 = 'credit_alphanum12', - } - interface BalanceLineNative { balance: string; asset_type: ASSET_TYPE.native; From c9af52463510893e54da961da7a0dd7193ba4cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 15:38:44 +0200 Subject: [PATCH 10/12] feat: Memo namespace & conditionally typed value BREAKING CHANGES: Requires Typescript 2.8 --- types/stellar-sdk/index.d.ts | 42 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index d82fef63dc..ec1413af6c 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.4 +// TypeScript Version: 2.8 /// @@ -69,24 +69,39 @@ export namespace StellarBase { const MemoText = 'text'; const MemoHash = 'hash'; const MemoReturn = 'return'; - type MemoType = typeof MemoNone | typeof MemoID | typeof MemoText | typeof MemoHash | typeof MemoReturn; - class Memo { + 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; + 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: typeof MemoNone); - constructor(type: typeof MemoID | typeof MemoText | typeof MemoHash | typeof MemoReturn, value: string) - constructor(type: typeof MemoHash | typeof MemoReturn, value: 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) - type: string; - value: null | string | Buffer; + 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', @@ -429,7 +444,6 @@ export import MemoID = StellarBase.MemoID; export import MemoNone = StellarBase.MemoNone; export import MemoReturn = StellarBase.MemoReturn; export import MemoText = StellarBase.MemoText; -export import MemoType = StellarBase.MemoType; export import Network = StellarBase.Network; export import Networks = StellarBase.Networks; export import Operation = StellarBase.Operation; From 7577cf4f155f3e856b0de01d957a6542224d7a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 15:56:23 +0200 Subject: [PATCH 11/12] feat: conditionally typed BalanceLine --- types/stellar-sdk/index.d.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/types/stellar-sdk/index.d.ts b/types/stellar-sdk/index.d.ts index ec1413af6c..6041aff0a7 100644 --- a/types/stellar-sdk/index.d.ts +++ b/types/stellar-sdk/index.d.ts @@ -556,14 +556,17 @@ export namespace Server { balance: string; asset_type: ASSET_TYPE.native; } - interface BalanceLineAsset { + interface BalanceLineAsset { balance: string; limit: string; - asset_type: ASSET_TYPE.credit4 | ASSET_TYPE.credit12; + asset_type: T; asset_code: string; asset_issuer: string; } - type BalanceLine = BalanceLineNative | BalanceLineAsset; + type BalanceLine = + T extends ASSET_TYPE.native ? BalanceLineNative : + T extends ASSET_TYPE.credit4 | ASSET_TYPE.credit12 ? BalanceLineAsset : + BalanceLineNative | BalanceLineAsset; interface AccountRecord extends Record { id: string; From e261d712fe367c878d0ef9f22bc491647ab37c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalvis=20Kalni=C5=86=C5=A1?= Date: Fri, 28 Dec 2018 16:12:04 +0200 Subject: [PATCH 12/12] test: Memo --- types/stellar-sdk/stellar-sdk-tests.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/types/stellar-sdk/stellar-sdk-tests.ts b/types/stellar-sdk/stellar-sdk-tests.ts index fe72b84a66..bf991b4591 100644 --- a/types/stellar-sdk/stellar-sdk-tests.ts +++ b/types/stellar-sdk/stellar-sdk-tests.ts @@ -12,6 +12,21 @@ transaction; // $ExpectType Transaction StellarSdk.StellarTomlResolver.resolve("example.com", {allowHttp: true, timeout: 100}) .then(toml => toml.FEDERATION_SERVER); -const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)) as StellarSdk.xdr.DecoratedSignature; +const sig = StellarSdk.xdr.DecoratedSignature.fromXDR(Buffer.of(1, 2)); // $ExpectType DecoratedSignature sig.hint(); // $ExpectType Buffer sig.signature(); // $ExpectType Buffer + +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