Skip to content

Commit

Permalink
fix: refine Crypoconditions parsers types
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Mar 10, 2021
1 parent 84bd4ef commit 90a2cb2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
33 changes: 27 additions & 6 deletions types/utils/ccJsonLoad.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = TypeId.Ed25519Sha256>(
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;
56 changes: 37 additions & 19 deletions types/utils/ccJsonify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -36,17 +43,28 @@ export interface ThresholdSha256JSONCondition extends BaseJSONCondition {
};
}

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

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

export interface JSONConditions {
[TypeId.ThresholdSha256]: ThresholdSha256JSONCondition;
[TypeId.PreimageSha256]: PreimageSha256JSONCondition;
[TypeId.Ed25519Sha256]: 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 function ccJsonify<T = TypeId.Ed25519Sha256>(
fulfillment: Fulfillment | Condition
): JSONConditions[T];
export default ccJsonify;

0 comments on commit 90a2cb2

Please sign in to comment.