From af90b97460e9b0f724ab1d7be082f6b10bee21d2 Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:20:32 +0100 Subject: [PATCH] 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;