Skip to content

Commit

Permalink
fix: refine types definitions
Browse files Browse the repository at this point in the history
Signed-off-by: getlarge <ed@getlarge.eu>
  • Loading branch information
getlarge committed Mar 10, 2021
1 parent 858acf2 commit b177ca0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 42 deletions.
26 changes: 13 additions & 13 deletions types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,57 +106,57 @@ export default class Connection {

getBlock(
blockHeight: number | string
): Promise<EndpointsUrl[Endpoints.blocksDetail]>;
): Promise<EndpointsResponse[Endpoints.blocksDetail]>;

getTransaction<O = TransactionOperations.CREATE>(
transactionId: string
): Promise<EndpointsUrl<O>[Endpoints.transactionsDetail]>;
): Promise<EndpointsResponse<O>[Endpoints.transactionsDetail]>;

listBlocks(transactionId: string): Promise<EndpointsUrl[Endpoints.blocks]>;
listBlocks(transactionId: string): Promise<EndpointsResponse[Endpoints.blocks]>;

listOutputs(
publicKey: string,
spent?: boolean
): Promise<EndpointsUrl[Endpoints.outputs]>;
): Promise<EndpointsResponse[Endpoints.outputs]>;

listTransactions<O = TransactionOperations.CREATE>(
listTransactions(
assetId: string,
operation: O
): Promise<EndpointsUrl<O>[Endpoints.transactions]>;
operation?: TransactionOperations
): Promise<EndpointsResponse<typeof operation>[Endpoints.transactions]>;

postTransaction<
O = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
transaction: TransactionCommon<O>
): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsCommit]>;
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsCommit]>;

postTransactionSync<
O = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
transaction: TransactionCommon<O>
): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsSync]>;
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsSync]>;

postTransactionAsync<
O = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
transaction: TransactionCommon<O>
): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsAsync]>;
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsAsync]>;

postTransactionCommit<
O = TransactionOperations.CREATE,
A = Record<string, any>,
M = Record<string, any>
>(
transaction: TransactionCommon<O>
): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsCommit]>;
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsCommit]>;

searchAssets(search: string): Promise<EndpointsUrl[Endpoints.assets]>;
searchAssets(search: string): Promise<EndpointsResponse[Endpoints.assets]>;

searchMetadata(search: string): Promise<EndpointsUrl[Endpoints.metadata]>;
searchMetadata(search: string): Promise<EndpointsResponse[Endpoints.metadata]>;
}
57 changes: 34 additions & 23 deletions types/transaction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import type {
PreimageSha256,
ThresholdSha256,
} from 'crypto-conditions';
import { TypeId } from 'crypto-conditions/types/types';
import {
Ed25519Sha256JSONCondition,
JSONCondition,
JSONConditions,
JSONConditionUnion,
PreimageSha256JSONCondition,
ThresholdSha256JSONCondition,
} from './utils/ccJsonify';
Expand All @@ -19,7 +21,6 @@ export interface TransactionInput {
} | null;
owners_before: string[];
}

export interface TransactionOutput {
amount: string;
// TODO: specifiy JSON conditions
Expand Down Expand Up @@ -81,6 +82,11 @@ export interface TransferTransaction<M = Record<string, unknown>>
operation: TransactionOperations.TRANSFER;
}

export interface TransactionUnspentOutput {
tx: TransactionCommon;
output_index: number;
}

interface TxTemplate {
id: null;
operation: null;
Expand All @@ -102,55 +108,55 @@ export default class Transaction {
transaction: CreateTransaction | TransferTransaction
): string;

static makeEd25519Condition(publicKey: string): Ed25519Sha256JSONCondition;

static makeEd25519Condition(
publicKey: string,
json = true
json: true
): Ed25519Sha256JSONCondition;

static makeEd25519Condition(publicKey: string, json = false): Ed25519Sha256;

// static makeEd25519Condition(publicKey: string): Ed25519Sha256JSONCondition;
static makeEd25519Condition(publicKey: string, json: false): Ed25519Sha256;

static makeEd25519Condition(
publicKey: string,
json?: boolean
json: boolean = true
): Ed25519Sha256 | Ed25519Sha256JSONCondition;

static makeSha256Condition(preimage: string): PreimageSha256JSONCondition;

static makeSha256Condition(
preimage: string,
json = true
json: true
): PreimageSha256JSONCondition;

static makeSha256Condition(preimage: string, json = false): PreimageSha256;

// static makeSha256Condition(preimage: string): PreimageSha256JSONCondition;
static makeSha256Condition(preimage: string, json: false): PreimageSha256;

static makeSha256Condition(
preimage: string,
json?: boolean
json: boolean = true
): PreimageSha256 | PreimageSha256JSONCondition;

static makeThresholdCondition(
threshold: number,
subconditions: (string | Fulfillment)[]
): ThresholdSha256JSONCondition;

static makeThresholdCondition(
threshold: number,
subconditions: (string | Fulfillment)[],
json = true
json: true
): ThresholdSha256JSONCondition;

static makeThresholdCondition(
threshold: number,
subconditions: (string | Fulfillment)[],
json = false
json: false
): ThresholdSha256;

// static makeThresholdCondition(
// threshold: number,
// subconditions: (string | Fulfillment)[]
// ): ThresholdSha256JSONCondition;

static makeThresholdCondition(
threshold: number,
subconditions: (string | Fulfillment)[],
json?: boolean
json: boolean = true
): ThresholdSha256 | ThresholdSha256JSONCondition;

static makeInputTemplate(
Expand All @@ -160,8 +166,13 @@ export default class Transaction {
): TransactionInput;

static makeOutput(
condition: JSONCondition,
amount: string
condition: JSONConditionUnion,
amount: string = '1'
): TransactionOutput;

static makeOutput<T = TypeId.Ed25519Sha256>(
condition: JSONConditions[T],
amount: string = '1'
): TransactionOutput;

static makeTransactionTemplate(): TxTemplate;
Expand Down Expand Up @@ -189,7 +200,7 @@ export default class Transaction {
): CreateTransaction<A, M>;

static makeTransferTransaction<M = Record<string, any>>(
unspentOutputs: TransactionOutput[],
unspentOutputs: TransactionUnspentOutput[],
outputs: TransactionOutput[],
metadata: M
): TransferTransaction<M>;
Expand Down
16 changes: 10 additions & 6 deletions types/utils/ccJsonify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ interface BaseJSONCondition {
uri: string;
}

interface Ed25519Sha256JSONCondition extends BaseJSONCondition {
export interface Ed25519Sha256JSONCondition extends BaseJSONCondition {
details: { type: TypeName.Ed25519Sha256; publicKey?: string };
}

interface PreimageSha256JSONCondition extends BaseJSONCondition {
export interface PreimageSha256JSONCondition extends BaseJSONCondition {
details: {
type: TypeName.PreimageSha256;
type_id: 0;
Expand All @@ -30,19 +29,24 @@ interface PreimageSha256JSONCondition extends BaseJSONCondition {
};
}

interface ThresholdSha256JSONCondition extends BaseJSONCondition {
export interface ThresholdSha256JSONCondition extends BaseJSONCondition {
details: {
type: TypeName.ThresholdSha256;
subConditions: (Ed25519Sha256JSONCondition | PreimageSha256JSONCondition)[];
};
}

export interface JSONCondition {
export type JSONConditionUnion =
| PreimageSha256JSONCondition
| Ed25519Sha256JSONCondition
| ThresholdSha256JSONCondition;

export interface JSONConditions {
[TypeId.ThresholdSha256]: ThresholdSha256JSONCondition;
[TypeId.PreimageSha256]: PreimageSha256JSONCondition;
[TypeId.Ed25519Sha256]: Ed25519Sha256JSONCondition;
}

export default function ccJsonify<T = TypeId.Ed25519Sha256>(
fulfillment: Fulfillment | Condition
): JSONCondition[T];
): JSONConditions[T];

0 comments on commit b177ca0

Please sign in to comment.