feat(op): setOptions signer conditional type

This commit is contained in:
Kalvis Kalniņš
2019-01-03 12:00:57 +02:00
parent b058eee449
commit f17d1c8ea6
2 changed files with 55 additions and 18 deletions
+43 -18
View File
@@ -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<OperationType.setOptions> {
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<T extends SignerUnion = never> extends Operation<OperationType.setOptions> {
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<T extends SignerUnion = never> 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<SetOptions>;
function setOptions<T extends SignerUnion = never>(options: SetOptionsOptions<T>): xdr.Operation<SetOptions<T>>;
interface BumpSequence extends Operation<OperationType.bumpSequence> {
bumpTo: string;
+12
View File
@@ -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