1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-27 07:07:50 +01:00

fix: refine Crypoconditions parsers types

This commit is contained in:
getlarge 2021-03-10 13:20:32 +01:00
parent 84bd4efe03
commit 90a2cb2608
No known key found for this signature in database
GPG Key ID: E4E13243600F9566
2 changed files with 66 additions and 27 deletions

View File

@ -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;

View File

@ -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<T = TypeId.Ed25519Sha256>(
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;