1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-24 11:01:28 +01:00

fix: improve transaction typedefs

This commit is contained in:
getlarge 2022-02-15 14:13:04 +01:00
parent fbc3d79d98
commit 1ba488b28f
No known key found for this signature in database
GPG Key ID: E4E13243600F9566

View File

@ -33,9 +33,9 @@ export enum TransactionOperations {
} }
export interface TransactionCommon< export interface TransactionCommon<
O = TransactionOperations, O extends TransactionOperations = TransactionOperations.CREATE,
A = Record<string, unknown>, A extends Record<string, any> = Record<string, unknown>,
M = Record<string, unknown> M extends Record<string, any> = Record<string, unknown>
> { > {
id?: string; id?: string;
inputs: TransactionInput[]; inputs: TransactionInput[];
@ -47,16 +47,16 @@ export interface TransactionCommon<
} }
export interface TransactionCommonSigned< export interface TransactionCommonSigned<
O = TransactionOperations, O extends TransactionOperations = TransactionOperations.CREATE,
A = Record<string, unknown>, A extends Record<string, any> = Record<string, unknown>,
M = Record<string, unknown> M extends Record<string, any> = Record<string, unknown>
> extends Omit<TransactionCommon<O, A, M>, 'id'> { > extends Omit<TransactionCommon<O, A, M>, 'id'> {
id: string; id: string;
} }
export type TransactionAssetMap< export type TransactionAssetMap<
Operation, Operation,
A = Record<string, unknown> A extends Record<string, any>
> = Operation extends TransactionOperations.CREATE > = Operation extends TransactionOperations.CREATE
? { ? {
data: A; data: A;
@ -66,18 +66,19 @@ export type TransactionAssetMap<
}; };
export interface CreateTransaction< export interface CreateTransaction<
A = Record<string, unknown>, A extends Record<string, any> = Record<string, unknown>,
M = Record<string, unknown> M extends Record<string, any> = Record<string, unknown>
> extends TransactionCommon<TransactionOperations.CREATE, A, M> { > extends TransactionCommon<TransactionOperations.CREATE, A, M> {
id: string; id: string;
asset: TransactionAssetMap<TransactionOperations.CREATE, A>; asset: TransactionAssetMap<TransactionOperations.CREATE, A>;
operation: TransactionOperations.CREATE; operation: TransactionOperations.CREATE;
} }
export interface TransferTransaction<M = Record<string, unknown>> export interface TransferTransaction<
extends TransactionCommon<TransactionOperations.TRANSFER, any, M> { M extends Record<string, any> = Record<string, unknown>
> extends TransactionCommon<TransactionOperations.TRANSFER, any, M> {
id: string; id: string;
asset: TransactionAssetMap<TransactionOperations.TRANSFER>; asset: TransactionAssetMap<TransactionOperations.TRANSFER, { id: string }>;
operation: TransactionOperations.TRANSFER; operation: TransactionOperations.TRANSFER;
} }
@ -96,22 +97,22 @@ interface TxTemplate {
version: '2.0'; version: '2.0';
} }
declare type DelegateSignFunction = ( export type DelegateSignFunction = (
serializedTransaction: string, serializedTransaction: string,
input: TransactionInput, input: TransactionInput,
index?: number index?: number
) => string; ) => string;
declare type DelegateSignFunctionAsync = ( export type DelegateSignFunctionAsync = (
serializedTransaction: string, serializedTransaction: string,
input: TransactionInput, input: TransactionInput,
index?: number index?: number
) => Promise<string>; ) => Promise<string>;
export default class Transaction { export default class Transaction {
static serializeTransactionIntoCanonicalString<O = TransactionOperations>( static serializeTransactionIntoCanonicalString<
transaction: TransactionCommon<O> O extends TransactionOperations = TransactionOperations
): string; >(transaction: TransactionCommon<O>): string;
static serializeTransactionIntoCanonicalString( static serializeTransactionIntoCanonicalString(
transaction: CreateTransaction | TransferTransaction transaction: CreateTransaction | TransferTransaction
@ -185,7 +186,7 @@ export default class Transaction {
static makeTransactionTemplate(): TxTemplate; static makeTransactionTemplate(): TxTemplate;
static makeTransaction< static makeTransaction<
O extends keyof TransactionOperations, O extends TransactionOperations,
A = Record<string, any>, A = Record<string, any>,
M = Record<string, any> M = Record<string, any>
>( >(
@ -212,18 +213,23 @@ export default class Transaction {
metadata: M metadata: M
): TransferTransaction<M>; ): TransferTransaction<M>;
static signTransaction<O = TransactionOperations.CREATE>( static signTransaction<
O extends TransactionOperations = TransactionOperations.CREATE
>(
transaction: TransactionCommon<O>, transaction: TransactionCommon<O>,
...privateKeys: string[] ...privateKeys: string[]
): TransactionCommonSigned<O>; ): TransactionCommonSigned<O>;
static delegateSignTransaction<
static delegateSignTransaction<O = TransactionOperations.CREATE>( O extends TransactionOperations = TransactionOperations.CREATE
>(
transaction: TransactionCommon<O>, transaction: TransactionCommon<O>,
signFn: DelegateSignFunction signFn: DelegateSignFunction
): TransactionCommonSigned<O>; ): TransactionCommonSigned<O>;
static delegateSignTransactionAsync<O = TransactionOperations.CREATE>( static delegateSignTransactionAsync<
O extends TransactionOperations = TransactionOperations.CREATE
>(
transaction: TransactionCommon<O>, transaction: TransactionCommon<O>,
signFn: DelegateSignFunctionAsync signFn: DelegateSignFunctionAsync
): Promise<TransactionCommonSigned<O>>; ): Promise<TransactionCommonSigned<O>>;