From cd5c52932472404b3be3e6e7e53aebfc93fc4788 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 08:48:13 +0100 Subject: [PATCH 01/13] fix: module exports --- src/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 5723214..ce37222 100644 --- a/src/index.js +++ b/src/index.js @@ -2,9 +2,12 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -export Ed25519Keypair from './Ed25519Keypair' +import Ed25519Keypair from './Ed25519Keypair' +import Connection from './connection' +import Transaction from './transaction' +import ccJsonLoad from './utils/ccJsonLoad' +import ccJsonify from './utils/ccJsonify' -export Connection from './connection' -export Transaction from './transaction' -export ccJsonLoad from './utils/ccJsonLoad' -export ccJsonify from './utils/ccJsonify' +export { + ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction +} From 5f6bef65c55f81ea32fab489f897d5cdaa7d3fa5 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 08:48:34 +0100 Subject: [PATCH 02/13] fix: add type definitions --- package.json | 1 + types/Ed25519Keypair.d.ts | 10 ++ types/baseRequest.d.ts | 31 ++++++ types/connection.d.ts | 162 ++++++++++++++++++++++++++++ types/index.d.ts | 11 ++ types/request.d.ts | 32 ++++++ types/sanitize.d.ts | 25 +++++ types/transaction.d.ts | 206 ++++++++++++++++++++++++++++++++++++ types/transport.d.ts | 21 ++++ types/utils/ccJsonLoad.d.ts | 11 ++ types/utils/ccJsonify.d.ts | 48 +++++++++ 11 files changed, 558 insertions(+) create mode 100644 types/Ed25519Keypair.d.ts create mode 100644 types/baseRequest.d.ts create mode 100644 types/connection.d.ts create mode 100644 types/index.d.ts create mode 100644 types/request.d.ts create mode 100644 types/sanitize.d.ts create mode 100644 types/transaction.d.ts create mode 100644 types/transport.d.ts create mode 100644 types/utils/ccJsonLoad.d.ts create mode 100644 types/utils/ccJsonify.d.ts diff --git a/package.json b/package.json index 7aa2b68..e40b201 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ ], "main": "./dist/node/index.js", "browser": "./dist/browser/bigchaindb-driver.cjs2.min.js", + "types": "./types/index.d.ts", "sideEffects": false, "scripts": { "lint": "eslint .", diff --git a/types/Ed25519Keypair.d.ts b/types/Ed25519Keypair.d.ts new file mode 100644 index 0000000..71fcdbc --- /dev/null +++ b/types/Ed25519Keypair.d.ts @@ -0,0 +1,10 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +export default class Ed25519Keypair { + publicKey: string; + privateKey: string; + + constructor(seed?: Buffer); +} diff --git a/types/baseRequest.d.ts b/types/baseRequest.d.ts new file mode 100644 index 0000000..177506d --- /dev/null +++ b/types/baseRequest.d.ts @@ -0,0 +1,31 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +export interface RequestConfig { + headers?: Record; + jsonBody?: Record; + query?: Record; + method?: 'GET' | ' POST' | 'PUT'; + urlTemplateSpec?: any[] | Record; + [key: string]: any; +} + +export function ResponseError( + message: string, + status?: number, + requestURI?: string +): void; + +declare function timeout( + ms: number, + promise: Promise +): Promise; + +declare function handleResponse(res: Response): Response; + +export default function baseRequest( + url: string, + config: RequestConfig = {}, + requestTimeout?: number +): Promise; diff --git a/types/connection.d.ts b/types/connection.d.ts new file mode 100644 index 0000000..47e08b2 --- /dev/null +++ b/types/connection.d.ts @@ -0,0 +1,162 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { RequestConfig } from './baseRequest'; +import type { Node } from './request'; +import type Transport from './transport'; +import type { + CreateTransaction, + TransactionOperations, + TransferTransaction, + TransactionCommon, +} from './transaction'; + +declare const HEADER_BLACKLIST = ['content-type']; +declare const DEFAULT_NODE = 'http://localhost:9984/api/v1/'; +declare const DEFAULT_TIMEOUT = 20000; // The default value is 20 seconds + +export interface InputNode { + endpoint: string; +} + +export enum Endpoints { + blocks = 'blocks', + blocksDetail = 'blocksDetail', + outputs = 'outputs', + transactions = 'transactions', + transactionsSync = 'transactionsSync', + transactionsAsync = 'transactionsAsync', + transactionsCommit = 'transactionsCommit', + transactionsDetail = 'transactionsDetail', + assets = 'assets', + metadata = 'metadata', +} + +export interface EndpointsUrl { + [Endpoints.blocks]: 'blocks'; + [Endpoints.blocksDetail]: 'blocks/%(blockHeight)s'; + [Endpoints.outputs]: 'outputs'; + [Endpoints.transactions]: 'transactions'; + [Endpoints.transactionsSync]: 'transactions?mode=sync'; + [Endpoints.transactionsAsync]: 'transactions?mode=async'; + [Endpoints.transactionsCommit]: 'transactions?mode=commit'; + [Endpoints.transactionsDetail]: 'transactions/%(transactionId)s'; + [Endpoints.assets]: 'assets'; + [Endpoints.metadata]: 'metadata'; +} + +export interface EndpointsResponse< + O = TransactionOperations.CREATE, + A = Record, + M = Record +> { + [Endpoints.blocks]: number[]; + [Endpoints.blocksDetail]: { + height: number; + transactions: (CreateTransaction | TransferTransaction)[]; + }; + [Endpoints.outputs]: { + transaction_id: string; + output_index: number; + }[]; + [Endpoints.transactions]: O extends TransactionOperations.CREATE + ? CreateTransaction[] + : O extends TransactionOperations.TRANSFER + ? TransferTransaction[] + : (CreateTransaction | TransferTransaction)[]; + [Endpoints.transactionsSync]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.transactionsAsync]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.transactionsCommit]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.transactionsDetail]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.assets]: { id: string; data: Record }[]; + [Endpoints.metadata]: { id: string; metadata: Record }[]; +} + +export default class Connection { + private transport: Transport; + private normalizedNodes: Node[]; + private headers: Record; + + constructor( + nodes: string | InputNode | (string | InputNode)[], + headers: Record = {}, + timeout?: number + ); + + static normalizeNode( + node: string | InputNode, + headers: Record + ): Node; + + static getApiUrls(endpoint: E): EndpointsUrl[E]; + + private _req>( + path: EndpointsUrl[E], + options: RequestConfig = {} + ): Promise; + + getBlock( + blockHeight: number | string + ): Promise; + + getTransaction( + transactionId: string + ): Promise[Endpoints.transactionsDetail]>; + + listBlocks(transactionId: string): Promise; + + listOutputs( + publicKey: string, + spent?: boolean + ): Promise; + + listTransactions( + assetId: string, + operation: O + ): Promise[Endpoints.transactions]>; + + postTransaction< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsCommit]>; + + postTransactionSync< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsSync]>; + + postTransactionAsync< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsAsync]>; + + postTransactionCommit< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsCommit]>; + + searchAssets(search: string): Promise; + + searchMetadata(search: string): Promise; +} diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..a10316a --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,11 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import Ed25519Keypair from './Ed25519Keypair' +import Connection from './connection' +import Transaction from './transaction' +import ccJsonLoad from './utils/ccJsonLoad' +import ccJsonify from './utils/ccJsonify' + +export { ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction } diff --git a/types/request.d.ts b/types/request.d.ts new file mode 100644 index 0000000..9f4a649 --- /dev/null +++ b/types/request.d.ts @@ -0,0 +1,32 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { RequestConfig } from './baseRequest'; + +export interface Node { + endpoint: string; + headers: Record; +} + +export default class Request { + private node: Node; + private backoffTime: number; + private retries: number; + private connectionError?: Error; + + constructor(node: Node); + + async request>( + urlPath: string, + config: RequestConfig = {}, + timeout?: number, + maxBackoffTime?: number + ): Promise; + + updateBackoffTime(maxBackoffTime: number): void; + + getBackoffTimedelta(): number; + + static sleep(ms: number): void; +} diff --git a/types/sanitize.d.ts b/types/sanitize.d.ts new file mode 100644 index 0000000..48ab058 --- /dev/null +++ b/types/sanitize.d.ts @@ -0,0 +1,25 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +declare type FilterFn = (val: any, key?: string) => void; + +declare function filterFromObject>( + obj: I, + filter: Array | FilterFn, + conf: { isInclusion?: boolean } = {} +): Partial; + +declare function applyFilterOnObject>( + obj: I, + filterFn?: FilterFn +): Partial; + +declare function selectFromObject>( + obj: I, + filter: Array | FilterFn +): Partial; + +export default function sanitize>( + obj: I +): Partial | I; diff --git a/types/transaction.d.ts b/types/transaction.d.ts new file mode 100644 index 0000000..0f3f711 --- /dev/null +++ b/types/transaction.d.ts @@ -0,0 +1,206 @@ +import type { + Ed25519Sha256, + Fulfillment, + PreimageSha256, + ThresholdSha256, +} from 'crypto-conditions'; +import { + Ed25519Sha256JSONCondition, + JSONCondition, + PreimageSha256JSONCondition, + ThresholdSha256JSONCondition, +} from './utils/ccJsonify'; + +export interface TransactionInput { + fulfillment: string; + fulfills: { + output_index: number; + transaction_id: string; + } | null; + owners_before: string[]; +} + +export interface TransactionOutput { + amount: string; + // TODO: specifiy JSON conditions + condition: any[]; + public_keys: string[]; +} + +export enum TransactionOperations { + CREATE = 'CREATE', + TRANSFER = 'TRANSFER', +} + +export interface TransactionCommon< + O = TransactionOperations, + A = Record, + M = Record +> { + id?: string; + inputs: TransactionInput[]; + outputs: TransactionOutput[]; + version: string; + metadata: M; + operation: O; + asset: TransactionAssetMap; +} + +export interface TransactionCommonSigned< + O = TransactionOperations, + A = Record, + M = Record +> extends Omit, 'id'> { + id: string; +} + +export type TransactionAssetMap< + Operation, + A = Record +> = Operation extends TransactionOperations.CREATE + ? { + data: A; + } + : { + id: string; + }; + +export interface CreateTransaction< + A = Record, + M = Record +> extends TransactionCommon { + id: string; + asset: TransactionAssetMap; + operation: TransactionOperations.CREATE; +} + +export interface TransferTransaction> + extends TransactionCommon { + id: string; + asset: TransactionAssetMap; + operation: TransactionOperations.TRANSFER; +} + +interface TxTemplate { + id: null; + operation: null; + outputs: []; + inputs: []; + metadata: null; + asset: null; + version: '2.0'; +} + +declare function DelegateSignFunction( + serializedTransaction: string, + input: TransactionInput, + index?: number +): string; + +export default class Transaction { + static serializeTransactionIntoCanonicalString( + transaction: CreateTransaction | TransferTransaction + ): string; + + static makeEd25519Condition( + publicKey: string, + json = true + ): Ed25519Sha256JSONCondition; + + static makeEd25519Condition(publicKey: string, json = false): Ed25519Sha256; + + // static makeEd25519Condition(publicKey: string): Ed25519Sha256JSONCondition; + + static makeEd25519Condition( + publicKey: string, + json?: boolean + ): Ed25519Sha256 | Ed25519Sha256JSONCondition; + + static makeSha256Condition( + preimage: string, + json = true + ): PreimageSha256JSONCondition; + + static makeSha256Condition(preimage: string, json = false): PreimageSha256; + + // static makeSha256Condition(preimage: string): PreimageSha256JSONCondition; + + static makeSha256Condition( + preimage: string, + json?: boolean + ): PreimageSha256 | PreimageSha256JSONCondition; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json = true + ): ThresholdSha256JSONCondition; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json = false + ): ThresholdSha256; + + // static makeThresholdCondition( + // threshold: number, + // subconditions: (string | Fulfillment)[] + // ): ThresholdSha256JSONCondition; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json?: boolean + ): ThresholdSha256 | ThresholdSha256JSONCondition; + + static makeInputTemplate( + publicKeys: string[], + fulfills?: TransactionInput['fulfills'], + fulfillment?: TransactionInput['fulfillment'] + ): TransactionInput; + + static makeOutput( + condition: JSONCondition, + amount: string + ): TransactionOutput; + + static makeTransactionTemplate(): TxTemplate; + + static makeTransaction< + O extends keyof TransactionOperations, + A = Record, + M = Record + >( + operation: O, + asset: A, + metadata: M, + outputs: TransactionOutput[], + inputs: TransactionInput[] + ): TransactionCommon; + + static makeCreateTransaction< + A = Record, + M = Record + >( + asset: A, + metadata: M, + outputs: TransactionOutput[], + ...issuers: string[] + ): CreateTransaction; + + static makeTransferTransaction>( + unspentOutputs: TransactionOutput[], + outputs: TransactionOutput[], + metadata: M + ): TransferTransaction; + + static signTransaction( + transaction: TransactionCommon, + ...privateKeys: string[] + ): TransactionCommonSigned; + + static delegateSignTransaction( + transaction: TransactionCommon, + signFn: DelegateSignFunction + ): TransactionCommonSigned; +} diff --git a/types/transport.d.ts b/types/transport.d.ts new file mode 100644 index 0000000..3119057 --- /dev/null +++ b/types/transport.d.ts @@ -0,0 +1,21 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import Request, { Node } from './request'; +import type { RequestConfig } from './baseRequest'; + +export default class Transport { + private connectionPool: Request[]; + private timeout: number; + private maxBackoffTime: number; + + constructor(nodes: Node[], timeout: number); + + pickConnection(): Request; + + async forwardRequest>( + path: string, + config: RequestConfig + ): Promise; +} diff --git a/types/utils/ccJsonLoad.d.ts b/types/utils/ccJsonLoad.d.ts new file mode 100644 index 0000000..cd12c8c --- /dev/null +++ b/types/utils/ccJsonLoad.d.ts @@ -0,0 +1,11 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { Condition, Fulfillment } from 'crypto-conditions'; +import type { JSONCondition } from './ccJsonify'; + +// TODO: improve returned type accuracy +export default function ccJsonLoad( + conditionJson: JSONCondition[T] +): Condition | Fulfillment; diff --git a/types/utils/ccJsonify.d.ts b/types/utils/ccJsonify.d.ts new file mode 100644 index 0000000..fe7eab2 --- /dev/null +++ b/types/utils/ccJsonify.d.ts @@ -0,0 +1,48 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { Condition, Fulfillment } from 'crypto-conditions'; +import type { TypeId } from 'crypto-conditions/types/types'; + +interface BaseJSONCondition { + details: { + type: TypeName; + hash?: string; + max_fulfillment_length?: number; + type?: 'fulfillement' | 'condition'; + [key: string]: any; + }; + uri: string; +} + +interface Ed25519Sha256JSONCondition extends BaseJSONCondition { + details: { type: TypeName.Ed25519Sha256; publicKey?: string }; +} + +interface PreimageSha256JSONCondition extends BaseJSONCondition { + details: { + type: TypeName.PreimageSha256; + type_id: 0; + bitmask: 3; + preimage?: string; + type?: 'fulfillement'; + }; +} + +interface ThresholdSha256JSONCondition extends BaseJSONCondition { + details: { + type: TypeName.ThresholdSha256; + subConditions: (Ed25519Sha256JSONCondition | PreimageSha256JSONCondition)[]; + }; +} + +export interface JSONCondition { + [TypeId.ThresholdSha256]: ThresholdSha256JSONCondition; + [TypeId.PreimageSha256]: PreimageSha256JSONCondition; + [TypeId.Ed25519Sha256]: Ed25519Sha256JSONCondition; +} + +export default function ccJsonify( + fulfillment: Fulfillment | Condition +): JSONCondition[T]; From 84bd4efe03475b006305c94a2d7e6ad02cd51781 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 11:03:53 +0100 Subject: [PATCH 03/13] fix: refine types definitions --- types/connection.d.ts | 26 ++++++++-------- types/transaction.d.ts | 61 ++++++++++++++++++++++---------------- types/utils/ccJsonify.d.ts | 16 ++++++---- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/types/connection.d.ts b/types/connection.d.ts index 47e08b2..336f8f4 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -106,23 +106,23 @@ export default class Connection { getBlock( blockHeight: number | string - ): Promise; + ): Promise; getTransaction( transactionId: string - ): Promise[Endpoints.transactionsDetail]>; + ): Promise[Endpoints.transactionsDetail]>; - listBlocks(transactionId: string): Promise; + listBlocks(transactionId: string): Promise; listOutputs( publicKey: string, spent?: boolean - ): Promise; + ): Promise; - listTransactions( + listTransactions( assetId: string, - operation: O - ): Promise[Endpoints.transactions]>; + operation?: TransactionOperations + ): Promise[Endpoints.transactions]>; postTransaction< O = TransactionOperations.CREATE, @@ -130,7 +130,7 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsCommit]>; + ): Promise[Endpoints.transactionsCommit]>; postTransactionSync< O = TransactionOperations.CREATE, @@ -138,7 +138,7 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsSync]>; + ): Promise[Endpoints.transactionsSync]>; postTransactionAsync< O = TransactionOperations.CREATE, @@ -146,7 +146,7 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsAsync]>; + ): Promise[Endpoints.transactionsAsync]>; postTransactionCommit< O = TransactionOperations.CREATE, @@ -154,9 +154,9 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsCommit]>; + ): Promise[Endpoints.transactionsCommit]>; - searchAssets(search: string): Promise; + searchAssets(search: string): Promise; - searchMetadata(search: string): Promise; + searchMetadata(search: string): Promise; } diff --git a/types/transaction.d.ts b/types/transaction.d.ts index 0f3f711..752cc3c 100644 --- a/types/transaction.d.ts +++ b/types/transaction.d.ts @@ -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'; @@ -19,7 +21,6 @@ export interface TransactionInput { } | null; owners_before: string[]; } - export interface TransactionOutput { amount: string; // TODO: specifiy JSON conditions @@ -81,6 +82,11 @@ export interface TransferTransaction> operation: TransactionOperations.TRANSFER; } +export interface TransactionUnspentOutput { + tx: TransactionCommon; + output_index: number; +} + interface TxTemplate { id: null; operation: null; @@ -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)[], - json = true + subconditions: (string | Fulfillment)[] ): ThresholdSha256JSONCondition; static makeThresholdCondition( threshold: number, subconditions: (string | Fulfillment)[], - json = false - ): ThresholdSha256; - - // static makeThresholdCondition( - // threshold: number, - // subconditions: (string | Fulfillment)[] - // ): ThresholdSha256JSONCondition; + json: true + ): ThresholdSha256JSONCondition; static makeThresholdCondition( threshold: number, subconditions: (string | Fulfillment)[], - json?: boolean + json: false + ): ThresholdSha256; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json: boolean = true ): ThresholdSha256 | ThresholdSha256JSONCondition; static makeInputTemplate( @@ -160,8 +166,13 @@ export default class Transaction { ): TransactionInput; static makeOutput( - condition: JSONCondition, - amount: string + condition: JSONConditionUnion, + amount: string = '1' + ): TransactionOutput; + + static makeOutput( + condition: JSONConditions[T], + amount: string = '1' ): TransactionOutput; static makeTransactionTemplate(): TxTemplate; @@ -189,7 +200,7 @@ export default class Transaction { ): CreateTransaction; static makeTransferTransaction>( - unspentOutputs: TransactionOutput[], + unspentOutputs: TransactionUnspentOutput[], outputs: TransactionOutput[], metadata: M ): TransferTransaction; diff --git a/types/utils/ccJsonify.d.ts b/types/utils/ccJsonify.d.ts index fe7eab2..717341a 100644 --- a/types/utils/ccJsonify.d.ts +++ b/types/utils/ccJsonify.d.ts @@ -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; @@ -30,14 +29,19 @@ 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; @@ -45,4 +49,4 @@ export interface JSONCondition { export default function ccJsonify( fulfillment: Fulfillment | Condition -): JSONCondition[T]; +): JSONConditions[T]; From 90a2cb2608c6c1bc82b3ecf287dc8c85bb4edafd Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:20:32 +0100 Subject: [PATCH 04/13] fix: refine Crypoconditions parsers types --- types/utils/ccJsonLoad.d.ts | 33 ++++++++++++++++---- types/utils/ccJsonify.d.ts | 60 ++++++++++++++++++++++++------------- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/types/utils/ccJsonLoad.d.ts b/types/utils/ccJsonLoad.d.ts index cd12c8c..16053b2 100644 --- a/types/utils/ccJsonLoad.d.ts +++ b/types/utils/ccJsonLoad.d.ts @@ -2,10 +2,31 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import type { Condition, Fulfillment } from 'crypto-conditions'; -import type { JSONCondition } from './ccJsonify'; +import type { + Condition, + Ed25519Sha256, + PreimageSha256, + ThresholdSha256, +} from 'crypto-conditions'; +import type { + Ed25519Sha256JSONCondition, + JSONCondition, + PreimageSha256JSONCondition, + ThresholdSha256JSONCondition, +} from './ccJsonify'; -// TODO: improve returned type accuracy -export default function ccJsonLoad( - conditionJson: JSONCondition[T] -): Condition | Fulfillment; +declare function ccJsonLoad( + conditionJson: PreimageSha256JSONCondition +): PreimageSha256; + +declare function ccJsonLoad( + conditionJson: ThresholdSha256JSONCondition +): ThresholdSha256; + +declare function ccJsonLoad( + conditionJson: Ed25519Sha256JSONCondition +): Ed25519Sha256; + +declare function ccJsonLoad(conditionJson: JSONCondition): Condition; + +export default ccJsonLoad; diff --git a/types/utils/ccJsonify.d.ts b/types/utils/ccJsonify.d.ts index 717341a..2e6cbe0 100644 --- a/types/utils/ccJsonify.d.ts +++ b/types/utils/ccJsonify.d.ts @@ -2,27 +2,34 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import type { Condition, Fulfillment } from 'crypto-conditions'; +import type { + Condition, + Ed25519Sha256, + PreimageSha256, + ThresholdSha256, +} from 'crypto-conditions'; import type { TypeId } from 'crypto-conditions/types/types'; interface BaseJSONCondition { details: { - type: TypeName; - hash?: string; - max_fulfillment_length?: number; - type?: 'fulfillement' | 'condition'; [key: string]: any; }; uri: string; } -export interface Ed25519Sha256JSONCondition extends BaseJSONCondition { - details: { type: TypeName.Ed25519Sha256; publicKey?: string }; +export interface JSONCondition extends BaseJSONCondition { + details: { + type_id: TypeId; + bitmask: number; + type: 'condition'; + hash: string; + max_fulfillment_length: number; + }; } + export interface PreimageSha256JSONCondition extends BaseJSONCondition { details: { - type: TypeName.PreimageSha256; - type_id: 0; + type_id: TypeId.PreimageSha256; bitmask: 3; preimage?: string; type?: 'fulfillement'; @@ -36,17 +43,28 @@ export interface ThresholdSha256JSONCondition extends BaseJSONCondition { }; } -export type JSONConditionUnion = - | PreimageSha256JSONCondition - | Ed25519Sha256JSONCondition - | ThresholdSha256JSONCondition; - -export interface JSONConditions { - [TypeId.ThresholdSha256]: ThresholdSha256JSONCondition; - [TypeId.PreimageSha256]: PreimageSha256JSONCondition; - [TypeId.Ed25519Sha256]: Ed25519Sha256JSONCondition; +export interface Ed25519Sha256JSONCondition extends BaseJSONCondition { + details: { type: TypeName.Ed25519Sha256; publicKey?: string }; } -export default function ccJsonify( - fulfillment: Fulfillment | Condition -): JSONConditions[T]; +export type JSONConditionUnion = + | JSONCondition + | PreimageSha256JSONCondition + | ThresholdSha256JSONCondition + | Ed25519Sha256JSONCondition; + +declare function ccJsonify( + fulfillment: PreimageSha256 +): PreimageSha256JSONCondition; + +declare function ccJsonify( + fulfillment: ThresholdSha256 +): ThresholdSha256JSONCondition; + +declare function ccJsonify( + fulfillment: Ed25519Sha256 +): Ed25519Sha256JSONCondition; + +declare function ccJsonify(fulfillment: Condition): JSONCondition; + +export default ccJsonify; From 2a104eb86bb7efc2c400e38a7637aed94cd7f795 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:36:00 +0100 Subject: [PATCH 05/13] fix: Crypto conditions parsers --- src/utils/ccJsonLoad.js | 18 +++++++++--------- src/utils/ccJsonify.js | 14 +++++++------- test/transaction/test_cryptoconditions.js | 11 +++++++---- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/ccJsonLoad.js b/src/utils/ccJsonLoad.js index 7dc4ec4..a169394 100644 --- a/src/utils/ccJsonLoad.js +++ b/src/utils/ccJsonLoad.js @@ -2,9 +2,8 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import { Buffer } from 'buffer' import base58 from 'bs58' -import cc from 'crypto-conditions' +import { Condition, Ed25519Sha256, ThresholdSha256 } from 'crypto-conditions' /** * Loads a crypto-condition class (Fulfillment or Condition) from a BigchainDB JSON object @@ -13,17 +12,18 @@ import cc from 'crypto-conditions' */ export default function ccJsonLoad(conditionJson) { if ('hash' in conditionJson) { - const condition = new cc.Condition() - condition.type = conditionJson.type_id - condition.bitmask = conditionJson.bitmask - condition.hash = Buffer.from(base58.decode(conditionJson.hash)) + const condition = new Condition() + condition.setTypeId(conditionJson.type_id) + condition.setSubtypes(conditionJson.bitmask) + condition.setHash(base58.decode(conditionJson.hash)) + // TODO: fix this, maxFulfillmentLength cannot be set in CryptoCondition lib condition.maxFulfillmentLength = parseInt(conditionJson.max_fulfillment_length, 10) return condition } else { let fulfillment if (conditionJson.type === 'threshold-sha-256') { - fulfillment = new cc.ThresholdSha256() + fulfillment = new ThresholdSha256() fulfillment.threshold = conditionJson.threshold conditionJson.subconditions.forEach((subconditionJson) => { const subcondition = ccJsonLoad(subconditionJson) @@ -36,8 +36,8 @@ export default function ccJsonLoad(conditionJson) { } if (conditionJson.type === 'ed25519-sha-256') { - fulfillment = new cc.Ed25519Sha256() - fulfillment.publicKey = Buffer.from(base58.decode(conditionJson.public_key)) + fulfillment = new Ed25519Sha256() + fulfillment.setPublicKey(base58.decode(conditionJson.public_key)) } return fulfillment } diff --git a/src/utils/ccJsonify.js b/src/utils/ccJsonify.js index 52f3ddb..9abb47b 100644 --- a/src/utils/ccJsonify.js +++ b/src/utils/ccJsonify.js @@ -19,8 +19,8 @@ export default function ccJsonify(fulfillment) { } const jsonBody = { - 'details': {}, - 'uri': conditionUri, + details: {}, + uri: conditionUri, } if (fulfillment.getTypeId() === 0) { @@ -35,15 +35,15 @@ export default function ccJsonify(fulfillment) { if (fulfillment.getTypeId() === 2) { return { - 'details': { - 'type': 'threshold-sha-256', - 'threshold': fulfillment.threshold, - 'subconditions': fulfillment.subconditions.map((subcondition) => { + details: { + type: 'threshold-sha-256', + threshold: fulfillment.threshold, + subconditions: fulfillment.subconditions.map((subcondition) => { const subconditionJson = ccJsonify(subcondition.body) return subconditionJson.details }) }, - 'uri': conditionUri, + uri: conditionUri, } } diff --git a/test/transaction/test_cryptoconditions.js b/test/transaction/test_cryptoconditions.js index d9a102e..4fedc29 100644 --- a/test/transaction/test_cryptoconditions.js +++ b/test/transaction/test_cryptoconditions.js @@ -2,8 +2,10 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 +import { createHash } from 'crypto' +import { validateFulfillment } from 'crypto-conditions' import test from 'ava' -import cc from 'crypto-conditions' +import base58 from 'bs58' import { Ed25519Keypair, Transaction, ccJsonLoad } from '../../src' import { delegatedSignTransaction } from '../constants' import sha256Hash from '../../src/sha256Hash' @@ -89,7 +91,7 @@ test('Fulfillment correctly formed', t => { .concat(txTransfer.inputs[0].fulfills.output_index) : msg const msgHash = sha256Hash(msgUniqueFulfillment) - t.truthy(cc.validateFulfillment( + t.truthy(validateFulfillment( txSigned.inputs[0].fulfillment, txCreate.outputs[0].condition.uri, Buffer.from(msgHash, 'hex') )) @@ -114,15 +116,16 @@ test('Delegated signature is correct', t => { }) test('CryptoConditions JSON load', t => { + const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const cond = ccJsonLoad({ type: 'threshold-sha-256', threshold: 1, subconditions: [{ type: 'ed25519-sha-256', - public_key: 'a' + public_key: publicKey }, { - hash: 'a' + hash: base58.encode(createHash('sha256').update('a').digest()) }], }) t.truthy(cond.subconditions.length === 2) From c98cc8e4998ace2cc935209e6187283cbd20a0b1 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:58:38 +0100 Subject: [PATCH 06/13] fix: makeoutput input type --- types/transaction.d.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/types/transaction.d.ts b/types/transaction.d.ts index 752cc3c..a3681dd 100644 --- a/types/transaction.d.ts +++ b/types/transaction.d.ts @@ -4,11 +4,8 @@ import type { PreimageSha256, ThresholdSha256, } from 'crypto-conditions'; -import { TypeId } from 'crypto-conditions/types/types'; import { Ed25519Sha256JSONCondition, - JSONConditions, - JSONConditionUnion, PreimageSha256JSONCondition, ThresholdSha256JSONCondition, } from './utils/ccJsonify'; @@ -166,12 +163,10 @@ export default class Transaction { ): TransactionInput; static makeOutput( - condition: JSONConditionUnion, - amount: string = '1' - ): TransactionOutput; - - static makeOutput( - condition: JSONConditions[T], + condition: + | PreimageSha256JSONCondition + | ThresholdSha256JSONCondition + | Ed25519Sha256JSONCondition, amount: string = '1' ): TransactionOutput; From d26f667feb7d77a71e0c5818ccffe4fc4baedad2 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 08:48:13 +0100 Subject: [PATCH 07/13] fix: module exports Signed-off-by: getlarge --- src/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 5723214..ce37222 100644 --- a/src/index.js +++ b/src/index.js @@ -2,9 +2,12 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -export Ed25519Keypair from './Ed25519Keypair' +import Ed25519Keypair from './Ed25519Keypair' +import Connection from './connection' +import Transaction from './transaction' +import ccJsonLoad from './utils/ccJsonLoad' +import ccJsonify from './utils/ccJsonify' -export Connection from './connection' -export Transaction from './transaction' -export ccJsonLoad from './utils/ccJsonLoad' -export ccJsonify from './utils/ccJsonify' +export { + ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction +} From 858acf26934be53df1e88fe5c8890662034a0486 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 08:48:34 +0100 Subject: [PATCH 08/13] fix: add type definitions Signed-off-by: getlarge --- package.json | 1 + types/Ed25519Keypair.d.ts | 10 ++ types/baseRequest.d.ts | 31 ++++++ types/connection.d.ts | 162 ++++++++++++++++++++++++++++ types/index.d.ts | 11 ++ types/request.d.ts | 32 ++++++ types/sanitize.d.ts | 25 +++++ types/transaction.d.ts | 206 ++++++++++++++++++++++++++++++++++++ types/transport.d.ts | 21 ++++ types/utils/ccJsonLoad.d.ts | 11 ++ types/utils/ccJsonify.d.ts | 48 +++++++++ 11 files changed, 558 insertions(+) create mode 100644 types/Ed25519Keypair.d.ts create mode 100644 types/baseRequest.d.ts create mode 100644 types/connection.d.ts create mode 100644 types/index.d.ts create mode 100644 types/request.d.ts create mode 100644 types/sanitize.d.ts create mode 100644 types/transaction.d.ts create mode 100644 types/transport.d.ts create mode 100644 types/utils/ccJsonLoad.d.ts create mode 100644 types/utils/ccJsonify.d.ts diff --git a/package.json b/package.json index 7aa2b68..e40b201 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ ], "main": "./dist/node/index.js", "browser": "./dist/browser/bigchaindb-driver.cjs2.min.js", + "types": "./types/index.d.ts", "sideEffects": false, "scripts": { "lint": "eslint .", diff --git a/types/Ed25519Keypair.d.ts b/types/Ed25519Keypair.d.ts new file mode 100644 index 0000000..71fcdbc --- /dev/null +++ b/types/Ed25519Keypair.d.ts @@ -0,0 +1,10 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +export default class Ed25519Keypair { + publicKey: string; + privateKey: string; + + constructor(seed?: Buffer); +} diff --git a/types/baseRequest.d.ts b/types/baseRequest.d.ts new file mode 100644 index 0000000..177506d --- /dev/null +++ b/types/baseRequest.d.ts @@ -0,0 +1,31 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +export interface RequestConfig { + headers?: Record; + jsonBody?: Record; + query?: Record; + method?: 'GET' | ' POST' | 'PUT'; + urlTemplateSpec?: any[] | Record; + [key: string]: any; +} + +export function ResponseError( + message: string, + status?: number, + requestURI?: string +): void; + +declare function timeout( + ms: number, + promise: Promise +): Promise; + +declare function handleResponse(res: Response): Response; + +export default function baseRequest( + url: string, + config: RequestConfig = {}, + requestTimeout?: number +): Promise; diff --git a/types/connection.d.ts b/types/connection.d.ts new file mode 100644 index 0000000..47e08b2 --- /dev/null +++ b/types/connection.d.ts @@ -0,0 +1,162 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { RequestConfig } from './baseRequest'; +import type { Node } from './request'; +import type Transport from './transport'; +import type { + CreateTransaction, + TransactionOperations, + TransferTransaction, + TransactionCommon, +} from './transaction'; + +declare const HEADER_BLACKLIST = ['content-type']; +declare const DEFAULT_NODE = 'http://localhost:9984/api/v1/'; +declare const DEFAULT_TIMEOUT = 20000; // The default value is 20 seconds + +export interface InputNode { + endpoint: string; +} + +export enum Endpoints { + blocks = 'blocks', + blocksDetail = 'blocksDetail', + outputs = 'outputs', + transactions = 'transactions', + transactionsSync = 'transactionsSync', + transactionsAsync = 'transactionsAsync', + transactionsCommit = 'transactionsCommit', + transactionsDetail = 'transactionsDetail', + assets = 'assets', + metadata = 'metadata', +} + +export interface EndpointsUrl { + [Endpoints.blocks]: 'blocks'; + [Endpoints.blocksDetail]: 'blocks/%(blockHeight)s'; + [Endpoints.outputs]: 'outputs'; + [Endpoints.transactions]: 'transactions'; + [Endpoints.transactionsSync]: 'transactions?mode=sync'; + [Endpoints.transactionsAsync]: 'transactions?mode=async'; + [Endpoints.transactionsCommit]: 'transactions?mode=commit'; + [Endpoints.transactionsDetail]: 'transactions/%(transactionId)s'; + [Endpoints.assets]: 'assets'; + [Endpoints.metadata]: 'metadata'; +} + +export interface EndpointsResponse< + O = TransactionOperations.CREATE, + A = Record, + M = Record +> { + [Endpoints.blocks]: number[]; + [Endpoints.blocksDetail]: { + height: number; + transactions: (CreateTransaction | TransferTransaction)[]; + }; + [Endpoints.outputs]: { + transaction_id: string; + output_index: number; + }[]; + [Endpoints.transactions]: O extends TransactionOperations.CREATE + ? CreateTransaction[] + : O extends TransactionOperations.TRANSFER + ? TransferTransaction[] + : (CreateTransaction | TransferTransaction)[]; + [Endpoints.transactionsSync]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.transactionsAsync]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.transactionsCommit]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.transactionsDetail]: O extends TransactionOperations.CREATE + ? CreateTransaction + : TransferTransaction; + [Endpoints.assets]: { id: string; data: Record }[]; + [Endpoints.metadata]: { id: string; metadata: Record }[]; +} + +export default class Connection { + private transport: Transport; + private normalizedNodes: Node[]; + private headers: Record; + + constructor( + nodes: string | InputNode | (string | InputNode)[], + headers: Record = {}, + timeout?: number + ); + + static normalizeNode( + node: string | InputNode, + headers: Record + ): Node; + + static getApiUrls(endpoint: E): EndpointsUrl[E]; + + private _req>( + path: EndpointsUrl[E], + options: RequestConfig = {} + ): Promise; + + getBlock( + blockHeight: number | string + ): Promise; + + getTransaction( + transactionId: string + ): Promise[Endpoints.transactionsDetail]>; + + listBlocks(transactionId: string): Promise; + + listOutputs( + publicKey: string, + spent?: boolean + ): Promise; + + listTransactions( + assetId: string, + operation: O + ): Promise[Endpoints.transactions]>; + + postTransaction< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsCommit]>; + + postTransactionSync< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsSync]>; + + postTransactionAsync< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsAsync]>; + + postTransactionCommit< + O = TransactionOperations.CREATE, + A = Record, + M = Record + >( + transaction: TransactionCommon + ): Promise[Endpoints.transactionsCommit]>; + + searchAssets(search: string): Promise; + + searchMetadata(search: string): Promise; +} diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..a10316a --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,11 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import Ed25519Keypair from './Ed25519Keypair' +import Connection from './connection' +import Transaction from './transaction' +import ccJsonLoad from './utils/ccJsonLoad' +import ccJsonify from './utils/ccJsonify' + +export { ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction } diff --git a/types/request.d.ts b/types/request.d.ts new file mode 100644 index 0000000..9f4a649 --- /dev/null +++ b/types/request.d.ts @@ -0,0 +1,32 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { RequestConfig } from './baseRequest'; + +export interface Node { + endpoint: string; + headers: Record; +} + +export default class Request { + private node: Node; + private backoffTime: number; + private retries: number; + private connectionError?: Error; + + constructor(node: Node); + + async request>( + urlPath: string, + config: RequestConfig = {}, + timeout?: number, + maxBackoffTime?: number + ): Promise; + + updateBackoffTime(maxBackoffTime: number): void; + + getBackoffTimedelta(): number; + + static sleep(ms: number): void; +} diff --git a/types/sanitize.d.ts b/types/sanitize.d.ts new file mode 100644 index 0000000..48ab058 --- /dev/null +++ b/types/sanitize.d.ts @@ -0,0 +1,25 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +declare type FilterFn = (val: any, key?: string) => void; + +declare function filterFromObject>( + obj: I, + filter: Array | FilterFn, + conf: { isInclusion?: boolean } = {} +): Partial; + +declare function applyFilterOnObject>( + obj: I, + filterFn?: FilterFn +): Partial; + +declare function selectFromObject>( + obj: I, + filter: Array | FilterFn +): Partial; + +export default function sanitize>( + obj: I +): Partial | I; diff --git a/types/transaction.d.ts b/types/transaction.d.ts new file mode 100644 index 0000000..0f3f711 --- /dev/null +++ b/types/transaction.d.ts @@ -0,0 +1,206 @@ +import type { + Ed25519Sha256, + Fulfillment, + PreimageSha256, + ThresholdSha256, +} from 'crypto-conditions'; +import { + Ed25519Sha256JSONCondition, + JSONCondition, + PreimageSha256JSONCondition, + ThresholdSha256JSONCondition, +} from './utils/ccJsonify'; + +export interface TransactionInput { + fulfillment: string; + fulfills: { + output_index: number; + transaction_id: string; + } | null; + owners_before: string[]; +} + +export interface TransactionOutput { + amount: string; + // TODO: specifiy JSON conditions + condition: any[]; + public_keys: string[]; +} + +export enum TransactionOperations { + CREATE = 'CREATE', + TRANSFER = 'TRANSFER', +} + +export interface TransactionCommon< + O = TransactionOperations, + A = Record, + M = Record +> { + id?: string; + inputs: TransactionInput[]; + outputs: TransactionOutput[]; + version: string; + metadata: M; + operation: O; + asset: TransactionAssetMap; +} + +export interface TransactionCommonSigned< + O = TransactionOperations, + A = Record, + M = Record +> extends Omit, 'id'> { + id: string; +} + +export type TransactionAssetMap< + Operation, + A = Record +> = Operation extends TransactionOperations.CREATE + ? { + data: A; + } + : { + id: string; + }; + +export interface CreateTransaction< + A = Record, + M = Record +> extends TransactionCommon { + id: string; + asset: TransactionAssetMap; + operation: TransactionOperations.CREATE; +} + +export interface TransferTransaction> + extends TransactionCommon { + id: string; + asset: TransactionAssetMap; + operation: TransactionOperations.TRANSFER; +} + +interface TxTemplate { + id: null; + operation: null; + outputs: []; + inputs: []; + metadata: null; + asset: null; + version: '2.0'; +} + +declare function DelegateSignFunction( + serializedTransaction: string, + input: TransactionInput, + index?: number +): string; + +export default class Transaction { + static serializeTransactionIntoCanonicalString( + transaction: CreateTransaction | TransferTransaction + ): string; + + static makeEd25519Condition( + publicKey: string, + json = true + ): Ed25519Sha256JSONCondition; + + static makeEd25519Condition(publicKey: string, json = false): Ed25519Sha256; + + // static makeEd25519Condition(publicKey: string): Ed25519Sha256JSONCondition; + + static makeEd25519Condition( + publicKey: string, + json?: boolean + ): Ed25519Sha256 | Ed25519Sha256JSONCondition; + + static makeSha256Condition( + preimage: string, + json = true + ): PreimageSha256JSONCondition; + + static makeSha256Condition(preimage: string, json = false): PreimageSha256; + + // static makeSha256Condition(preimage: string): PreimageSha256JSONCondition; + + static makeSha256Condition( + preimage: string, + json?: boolean + ): PreimageSha256 | PreimageSha256JSONCondition; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json = true + ): ThresholdSha256JSONCondition; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json = false + ): ThresholdSha256; + + // static makeThresholdCondition( + // threshold: number, + // subconditions: (string | Fulfillment)[] + // ): ThresholdSha256JSONCondition; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json?: boolean + ): ThresholdSha256 | ThresholdSha256JSONCondition; + + static makeInputTemplate( + publicKeys: string[], + fulfills?: TransactionInput['fulfills'], + fulfillment?: TransactionInput['fulfillment'] + ): TransactionInput; + + static makeOutput( + condition: JSONCondition, + amount: string + ): TransactionOutput; + + static makeTransactionTemplate(): TxTemplate; + + static makeTransaction< + O extends keyof TransactionOperations, + A = Record, + M = Record + >( + operation: O, + asset: A, + metadata: M, + outputs: TransactionOutput[], + inputs: TransactionInput[] + ): TransactionCommon; + + static makeCreateTransaction< + A = Record, + M = Record + >( + asset: A, + metadata: M, + outputs: TransactionOutput[], + ...issuers: string[] + ): CreateTransaction; + + static makeTransferTransaction>( + unspentOutputs: TransactionOutput[], + outputs: TransactionOutput[], + metadata: M + ): TransferTransaction; + + static signTransaction( + transaction: TransactionCommon, + ...privateKeys: string[] + ): TransactionCommonSigned; + + static delegateSignTransaction( + transaction: TransactionCommon, + signFn: DelegateSignFunction + ): TransactionCommonSigned; +} diff --git a/types/transport.d.ts b/types/transport.d.ts new file mode 100644 index 0000000..3119057 --- /dev/null +++ b/types/transport.d.ts @@ -0,0 +1,21 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import Request, { Node } from './request'; +import type { RequestConfig } from './baseRequest'; + +export default class Transport { + private connectionPool: Request[]; + private timeout: number; + private maxBackoffTime: number; + + constructor(nodes: Node[], timeout: number); + + pickConnection(): Request; + + async forwardRequest>( + path: string, + config: RequestConfig + ): Promise; +} diff --git a/types/utils/ccJsonLoad.d.ts b/types/utils/ccJsonLoad.d.ts new file mode 100644 index 0000000..cd12c8c --- /dev/null +++ b/types/utils/ccJsonLoad.d.ts @@ -0,0 +1,11 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { Condition, Fulfillment } from 'crypto-conditions'; +import type { JSONCondition } from './ccJsonify'; + +// TODO: improve returned type accuracy +export default function ccJsonLoad( + conditionJson: JSONCondition[T] +): Condition | Fulfillment; diff --git a/types/utils/ccJsonify.d.ts b/types/utils/ccJsonify.d.ts new file mode 100644 index 0000000..fe7eab2 --- /dev/null +++ b/types/utils/ccJsonify.d.ts @@ -0,0 +1,48 @@ +// Copyright BigchainDB GmbH and BigchainDB contributors +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) +// Code is Apache-2.0 and docs are CC-BY-4.0 + +import type { Condition, Fulfillment } from 'crypto-conditions'; +import type { TypeId } from 'crypto-conditions/types/types'; + +interface BaseJSONCondition { + details: { + type: TypeName; + hash?: string; + max_fulfillment_length?: number; + type?: 'fulfillement' | 'condition'; + [key: string]: any; + }; + uri: string; +} + +interface Ed25519Sha256JSONCondition extends BaseJSONCondition { + details: { type: TypeName.Ed25519Sha256; publicKey?: string }; +} + +interface PreimageSha256JSONCondition extends BaseJSONCondition { + details: { + type: TypeName.PreimageSha256; + type_id: 0; + bitmask: 3; + preimage?: string; + type?: 'fulfillement'; + }; +} + +interface ThresholdSha256JSONCondition extends BaseJSONCondition { + details: { + type: TypeName.ThresholdSha256; + subConditions: (Ed25519Sha256JSONCondition | PreimageSha256JSONCondition)[]; + }; +} + +export interface JSONCondition { + [TypeId.ThresholdSha256]: ThresholdSha256JSONCondition; + [TypeId.PreimageSha256]: PreimageSha256JSONCondition; + [TypeId.Ed25519Sha256]: Ed25519Sha256JSONCondition; +} + +export default function ccJsonify( + fulfillment: Fulfillment | Condition +): JSONCondition[T]; From b177ca0de4fecca92d127f8bcb5d17f76ac5826e Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 11:03:53 +0100 Subject: [PATCH 09/13] fix: refine types definitions Signed-off-by: getlarge --- types/connection.d.ts | 26 ++++++++-------- types/transaction.d.ts | 61 ++++++++++++++++++++++---------------- types/utils/ccJsonify.d.ts | 16 ++++++---- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/types/connection.d.ts b/types/connection.d.ts index 47e08b2..336f8f4 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -106,23 +106,23 @@ export default class Connection { getBlock( blockHeight: number | string - ): Promise; + ): Promise; getTransaction( transactionId: string - ): Promise[Endpoints.transactionsDetail]>; + ): Promise[Endpoints.transactionsDetail]>; - listBlocks(transactionId: string): Promise; + listBlocks(transactionId: string): Promise; listOutputs( publicKey: string, spent?: boolean - ): Promise; + ): Promise; - listTransactions( + listTransactions( assetId: string, - operation: O - ): Promise[Endpoints.transactions]>; + operation?: TransactionOperations + ): Promise[Endpoints.transactions]>; postTransaction< O = TransactionOperations.CREATE, @@ -130,7 +130,7 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsCommit]>; + ): Promise[Endpoints.transactionsCommit]>; postTransactionSync< O = TransactionOperations.CREATE, @@ -138,7 +138,7 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsSync]>; + ): Promise[Endpoints.transactionsSync]>; postTransactionAsync< O = TransactionOperations.CREATE, @@ -146,7 +146,7 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsAsync]>; + ): Promise[Endpoints.transactionsAsync]>; postTransactionCommit< O = TransactionOperations.CREATE, @@ -154,9 +154,9 @@ export default class Connection { M = Record >( transaction: TransactionCommon - ): Promise[Endpoints.transactionsCommit]>; + ): Promise[Endpoints.transactionsCommit]>; - searchAssets(search: string): Promise; + searchAssets(search: string): Promise; - searchMetadata(search: string): Promise; + searchMetadata(search: string): Promise; } diff --git a/types/transaction.d.ts b/types/transaction.d.ts index 0f3f711..752cc3c 100644 --- a/types/transaction.d.ts +++ b/types/transaction.d.ts @@ -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'; @@ -19,7 +21,6 @@ export interface TransactionInput { } | null; owners_before: string[]; } - export interface TransactionOutput { amount: string; // TODO: specifiy JSON conditions @@ -81,6 +82,11 @@ export interface TransferTransaction> operation: TransactionOperations.TRANSFER; } +export interface TransactionUnspentOutput { + tx: TransactionCommon; + output_index: number; +} + interface TxTemplate { id: null; operation: null; @@ -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)[], - json = true + subconditions: (string | Fulfillment)[] ): ThresholdSha256JSONCondition; static makeThresholdCondition( threshold: number, subconditions: (string | Fulfillment)[], - json = false - ): ThresholdSha256; - - // static makeThresholdCondition( - // threshold: number, - // subconditions: (string | Fulfillment)[] - // ): ThresholdSha256JSONCondition; + json: true + ): ThresholdSha256JSONCondition; static makeThresholdCondition( threshold: number, subconditions: (string | Fulfillment)[], - json?: boolean + json: false + ): ThresholdSha256; + + static makeThresholdCondition( + threshold: number, + subconditions: (string | Fulfillment)[], + json: boolean = true ): ThresholdSha256 | ThresholdSha256JSONCondition; static makeInputTemplate( @@ -160,8 +166,13 @@ export default class Transaction { ): TransactionInput; static makeOutput( - condition: JSONCondition, - amount: string + condition: JSONConditionUnion, + amount: string = '1' + ): TransactionOutput; + + static makeOutput( + condition: JSONConditions[T], + amount: string = '1' ): TransactionOutput; static makeTransactionTemplate(): TxTemplate; @@ -189,7 +200,7 @@ export default class Transaction { ): CreateTransaction; static makeTransferTransaction>( - unspentOutputs: TransactionOutput[], + unspentOutputs: TransactionUnspentOutput[], outputs: TransactionOutput[], metadata: M ): TransferTransaction; diff --git a/types/utils/ccJsonify.d.ts b/types/utils/ccJsonify.d.ts index fe7eab2..717341a 100644 --- a/types/utils/ccJsonify.d.ts +++ b/types/utils/ccJsonify.d.ts @@ -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; @@ -30,14 +29,19 @@ 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; @@ -45,4 +49,4 @@ export interface JSONCondition { export default function ccJsonify( fulfillment: Fulfillment | Condition -): JSONCondition[T]; +): JSONConditions[T]; From af90b97460e9b0f724ab1d7be082f6b10bee21d2 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:20:32 +0100 Subject: [PATCH 10/13] fix: refine Crypoconditions parsers types Signed-off-by: getlarge --- types/utils/ccJsonLoad.d.ts | 33 ++++++++++++++++---- types/utils/ccJsonify.d.ts | 60 ++++++++++++++++++++++++------------- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/types/utils/ccJsonLoad.d.ts b/types/utils/ccJsonLoad.d.ts index cd12c8c..16053b2 100644 --- a/types/utils/ccJsonLoad.d.ts +++ b/types/utils/ccJsonLoad.d.ts @@ -2,10 +2,31 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import type { Condition, Fulfillment } from 'crypto-conditions'; -import type { JSONCondition } from './ccJsonify'; +import type { + Condition, + Ed25519Sha256, + PreimageSha256, + ThresholdSha256, +} from 'crypto-conditions'; +import type { + Ed25519Sha256JSONCondition, + JSONCondition, + PreimageSha256JSONCondition, + ThresholdSha256JSONCondition, +} from './ccJsonify'; -// TODO: improve returned type accuracy -export default function ccJsonLoad( - conditionJson: JSONCondition[T] -): Condition | Fulfillment; +declare function ccJsonLoad( + conditionJson: PreimageSha256JSONCondition +): PreimageSha256; + +declare function ccJsonLoad( + conditionJson: ThresholdSha256JSONCondition +): ThresholdSha256; + +declare function ccJsonLoad( + conditionJson: Ed25519Sha256JSONCondition +): Ed25519Sha256; + +declare function ccJsonLoad(conditionJson: JSONCondition): Condition; + +export default ccJsonLoad; diff --git a/types/utils/ccJsonify.d.ts b/types/utils/ccJsonify.d.ts index 717341a..2e6cbe0 100644 --- a/types/utils/ccJsonify.d.ts +++ b/types/utils/ccJsonify.d.ts @@ -2,27 +2,34 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import type { Condition, Fulfillment } from 'crypto-conditions'; +import type { + Condition, + Ed25519Sha256, + PreimageSha256, + ThresholdSha256, +} from 'crypto-conditions'; import type { TypeId } from 'crypto-conditions/types/types'; interface BaseJSONCondition { details: { - type: TypeName; - hash?: string; - max_fulfillment_length?: number; - type?: 'fulfillement' | 'condition'; [key: string]: any; }; uri: string; } -export interface Ed25519Sha256JSONCondition extends BaseJSONCondition { - details: { type: TypeName.Ed25519Sha256; publicKey?: string }; +export interface JSONCondition extends BaseJSONCondition { + details: { + type_id: TypeId; + bitmask: number; + type: 'condition'; + hash: string; + max_fulfillment_length: number; + }; } + export interface PreimageSha256JSONCondition extends BaseJSONCondition { details: { - type: TypeName.PreimageSha256; - type_id: 0; + type_id: TypeId.PreimageSha256; bitmask: 3; preimage?: string; type?: 'fulfillement'; @@ -36,17 +43,28 @@ export interface ThresholdSha256JSONCondition extends BaseJSONCondition { }; } -export type JSONConditionUnion = - | PreimageSha256JSONCondition - | Ed25519Sha256JSONCondition - | ThresholdSha256JSONCondition; - -export interface JSONConditions { - [TypeId.ThresholdSha256]: ThresholdSha256JSONCondition; - [TypeId.PreimageSha256]: PreimageSha256JSONCondition; - [TypeId.Ed25519Sha256]: Ed25519Sha256JSONCondition; +export interface Ed25519Sha256JSONCondition extends BaseJSONCondition { + details: { type: TypeName.Ed25519Sha256; publicKey?: string }; } -export default function ccJsonify( - fulfillment: Fulfillment | Condition -): JSONConditions[T]; +export type JSONConditionUnion = + | JSONCondition + | PreimageSha256JSONCondition + | ThresholdSha256JSONCondition + | Ed25519Sha256JSONCondition; + +declare function ccJsonify( + fulfillment: PreimageSha256 +): PreimageSha256JSONCondition; + +declare function ccJsonify( + fulfillment: ThresholdSha256 +): ThresholdSha256JSONCondition; + +declare function ccJsonify( + fulfillment: Ed25519Sha256 +): Ed25519Sha256JSONCondition; + +declare function ccJsonify(fulfillment: Condition): JSONCondition; + +export default ccJsonify; From 71fe66c1f51f50df0919236962ba8806aa14b5ef Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:36:00 +0100 Subject: [PATCH 11/13] fix: Crypto conditions parsers Signed-off-by: getlarge --- src/utils/ccJsonLoad.js | 18 +++++++++--------- src/utils/ccJsonify.js | 14 +++++++------- test/transaction/test_cryptoconditions.js | 11 +++++++---- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/ccJsonLoad.js b/src/utils/ccJsonLoad.js index 7dc4ec4..a169394 100644 --- a/src/utils/ccJsonLoad.js +++ b/src/utils/ccJsonLoad.js @@ -2,9 +2,8 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import { Buffer } from 'buffer' import base58 from 'bs58' -import cc from 'crypto-conditions' +import { Condition, Ed25519Sha256, ThresholdSha256 } from 'crypto-conditions' /** * Loads a crypto-condition class (Fulfillment or Condition) from a BigchainDB JSON object @@ -13,17 +12,18 @@ import cc from 'crypto-conditions' */ export default function ccJsonLoad(conditionJson) { if ('hash' in conditionJson) { - const condition = new cc.Condition() - condition.type = conditionJson.type_id - condition.bitmask = conditionJson.bitmask - condition.hash = Buffer.from(base58.decode(conditionJson.hash)) + const condition = new Condition() + condition.setTypeId(conditionJson.type_id) + condition.setSubtypes(conditionJson.bitmask) + condition.setHash(base58.decode(conditionJson.hash)) + // TODO: fix this, maxFulfillmentLength cannot be set in CryptoCondition lib condition.maxFulfillmentLength = parseInt(conditionJson.max_fulfillment_length, 10) return condition } else { let fulfillment if (conditionJson.type === 'threshold-sha-256') { - fulfillment = new cc.ThresholdSha256() + fulfillment = new ThresholdSha256() fulfillment.threshold = conditionJson.threshold conditionJson.subconditions.forEach((subconditionJson) => { const subcondition = ccJsonLoad(subconditionJson) @@ -36,8 +36,8 @@ export default function ccJsonLoad(conditionJson) { } if (conditionJson.type === 'ed25519-sha-256') { - fulfillment = new cc.Ed25519Sha256() - fulfillment.publicKey = Buffer.from(base58.decode(conditionJson.public_key)) + fulfillment = new Ed25519Sha256() + fulfillment.setPublicKey(base58.decode(conditionJson.public_key)) } return fulfillment } diff --git a/src/utils/ccJsonify.js b/src/utils/ccJsonify.js index 52f3ddb..9abb47b 100644 --- a/src/utils/ccJsonify.js +++ b/src/utils/ccJsonify.js @@ -19,8 +19,8 @@ export default function ccJsonify(fulfillment) { } const jsonBody = { - 'details': {}, - 'uri': conditionUri, + details: {}, + uri: conditionUri, } if (fulfillment.getTypeId() === 0) { @@ -35,15 +35,15 @@ export default function ccJsonify(fulfillment) { if (fulfillment.getTypeId() === 2) { return { - 'details': { - 'type': 'threshold-sha-256', - 'threshold': fulfillment.threshold, - 'subconditions': fulfillment.subconditions.map((subcondition) => { + details: { + type: 'threshold-sha-256', + threshold: fulfillment.threshold, + subconditions: fulfillment.subconditions.map((subcondition) => { const subconditionJson = ccJsonify(subcondition.body) return subconditionJson.details }) }, - 'uri': conditionUri, + uri: conditionUri, } } diff --git a/test/transaction/test_cryptoconditions.js b/test/transaction/test_cryptoconditions.js index d9a102e..4fedc29 100644 --- a/test/transaction/test_cryptoconditions.js +++ b/test/transaction/test_cryptoconditions.js @@ -2,8 +2,10 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 +import { createHash } from 'crypto' +import { validateFulfillment } from 'crypto-conditions' import test from 'ava' -import cc from 'crypto-conditions' +import base58 from 'bs58' import { Ed25519Keypair, Transaction, ccJsonLoad } from '../../src' import { delegatedSignTransaction } from '../constants' import sha256Hash from '../../src/sha256Hash' @@ -89,7 +91,7 @@ test('Fulfillment correctly formed', t => { .concat(txTransfer.inputs[0].fulfills.output_index) : msg const msgHash = sha256Hash(msgUniqueFulfillment) - t.truthy(cc.validateFulfillment( + t.truthy(validateFulfillment( txSigned.inputs[0].fulfillment, txCreate.outputs[0].condition.uri, Buffer.from(msgHash, 'hex') )) @@ -114,15 +116,16 @@ test('Delegated signature is correct', t => { }) test('CryptoConditions JSON load', t => { + const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const cond = ccJsonLoad({ type: 'threshold-sha-256', threshold: 1, subconditions: [{ type: 'ed25519-sha-256', - public_key: 'a' + public_key: publicKey }, { - hash: 'a' + hash: base58.encode(createHash('sha256').update('a').digest()) }], }) t.truthy(cond.subconditions.length === 2) From d5fd300cfc22e0579bbd479bad1cff1181485fb1 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:58:38 +0100 Subject: [PATCH 12/13] fix: makeoutput input type Signed-off-by: getlarge --- types/transaction.d.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/types/transaction.d.ts b/types/transaction.d.ts index 752cc3c..a3681dd 100644 --- a/types/transaction.d.ts +++ b/types/transaction.d.ts @@ -4,11 +4,8 @@ import type { PreimageSha256, ThresholdSha256, } from 'crypto-conditions'; -import { TypeId } from 'crypto-conditions/types/types'; import { Ed25519Sha256JSONCondition, - JSONConditions, - JSONConditionUnion, PreimageSha256JSONCondition, ThresholdSha256JSONCondition, } from './utils/ccJsonify'; @@ -166,12 +163,10 @@ export default class Transaction { ): TransactionInput; static makeOutput( - condition: JSONConditionUnion, - amount: string = '1' - ): TransactionOutput; - - static makeOutput( - condition: JSONConditions[T], + condition: + | PreimageSha256JSONCondition + | ThresholdSha256JSONCondition + | Ed25519Sha256JSONCondition, amount: string = '1' ): TransactionOutput; From 1779f6eef44c653e1d5959d9e05abc3feefcca28 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 15:37:09 +0100 Subject: [PATCH 13/13] fix: add type for TransactionOutput.condition Signed-off-by: getlarge --- types/transaction.d.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/types/transaction.d.ts b/types/transaction.d.ts index a3681dd..ff0011e 100644 --- a/types/transaction.d.ts +++ b/types/transaction.d.ts @@ -20,8 +20,10 @@ export interface TransactionInput { } export interface TransactionOutput { amount: string; - // TODO: specifiy JSON conditions - condition: any[]; + condition: + | PreimageSha256JSONCondition + | ThresholdSha256JSONCondition + | Ed25519Sha256JSONCondition; public_keys: string[]; }