1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 09:46:58 +01:00

fix: refine Crypoconditions parsers types

Signed-off-by: getlarge <ed@getlarge.eu>
This commit is contained in:
getlarge 2021-03-10 13:20:32 +01:00
parent b177ca0de4
commit af90b97460
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) // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are 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 {
import type { JSONCondition } from './ccJsonify'; Condition,
Ed25519Sha256,
PreimageSha256,
ThresholdSha256,
} from 'crypto-conditions';
import type {
Ed25519Sha256JSONCondition,
JSONCondition,
PreimageSha256JSONCondition,
ThresholdSha256JSONCondition,
} from './ccJsonify';
// TODO: improve returned type accuracy declare function ccJsonLoad(
export default function ccJsonLoad<T = TypeId.Ed25519Sha256>( conditionJson: PreimageSha256JSONCondition
conditionJson: JSONCondition[T] ): PreimageSha256;
): Condition | Fulfillment;
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) // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are 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'; import type { TypeId } from 'crypto-conditions/types/types';
interface BaseJSONCondition { interface BaseJSONCondition {
details: { details: {
type: TypeName;
hash?: string;
max_fulfillment_length?: number;
type?: 'fulfillement' | 'condition';
[key: string]: any; [key: string]: any;
}; };
uri: string; uri: string;
} }
export interface Ed25519Sha256JSONCondition extends BaseJSONCondition { export interface JSONCondition extends BaseJSONCondition {
details: { type: TypeName.Ed25519Sha256; publicKey?: string }; details: {
type_id: TypeId;
bitmask: number;
type: 'condition';
hash: string;
max_fulfillment_length: number;
};
} }
export interface PreimageSha256JSONCondition extends BaseJSONCondition { export interface PreimageSha256JSONCondition extends BaseJSONCondition {
details: { details: {
type: TypeName.PreimageSha256; type_id: TypeId.PreimageSha256;
type_id: 0;
bitmask: 3; bitmask: 3;
preimage?: string; preimage?: string;
type?: 'fulfillement'; type?: 'fulfillement';
@ -36,17 +43,28 @@ export interface ThresholdSha256JSONCondition extends BaseJSONCondition {
}; };
} }
export type JSONConditionUnion = export interface Ed25519Sha256JSONCondition extends BaseJSONCondition {
| PreimageSha256JSONCondition details: { type: TypeName.Ed25519Sha256; publicKey?: string };
| Ed25519Sha256JSONCondition
| ThresholdSha256JSONCondition;
export interface JSONConditions {
[TypeId.ThresholdSha256]: ThresholdSha256JSONCondition;
[TypeId.PreimageSha256]: PreimageSha256JSONCondition;
[TypeId.Ed25519Sha256]: Ed25519Sha256JSONCondition;
} }
export default function ccJsonify<T = TypeId.Ed25519Sha256>( export type JSONConditionUnion =
fulfillment: Fulfillment | Condition | JSONCondition
): JSONConditions[T]; | 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;