Skip to content

Commit

Permalink
fix: resolve type definition issues
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Mar 9, 2021
1 parent b0c13c8 commit af3bdc1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 34 deletions.
20 changes: 10 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type Condition from './lib/condition';
import type Fulfillment from './lib/fulfillment';
import type TypeRegistry from './lib/type-registry';
import type {
import Condition from './lib/condition';
import Fulfillment from './lib/fulfillment';
import TypeRegistry from './lib/type-registry';
import {
Ed25519Sha256Json,
PrefixSha256Json,
PreimageSha256Json,
RsaSha256Json,
ThresholdSha256Json,
} from './types';
import type PreimageSha256 from './types/preimage-sha256';
import type PrefixSha256 from './types/prefix-sha256';
import type ThresholdSha256 from './types/threshold-sha256';
import type RsaSha256 from './types/rsa-sha256';
import type Ed25519Sha256 from './types/ed25519-sha256';
import type base64url from './util/base64url';
import PreimageSha256 from './types/preimage-sha256';
import PrefixSha256 from './types/prefix-sha256';
import ThresholdSha256 from './types/threshold-sha256';
import RsaSha256 from './types/rsa-sha256';
import Ed25519Sha256 from './types/ed25519-sha256';
import base64url from './util/base64url';

export { base64url, Condition, Fulfillment, TypeRegistry };

Expand Down
31 changes: 26 additions & 5 deletions types/lib/fulfillment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ interface FulfillmentAsn1JsonValueMap {
[TypeAsn1Fulfillment.Ed25519Sha256]: Ed25519Sha256Json;
}

export interface FulfillmentAsn1Json<T = TypeAsn1Fulfillment> {
export interface FulfillmentAsn1Json<
T extends keyof FulfillmentAsn1JsonValueMap
> {
type: T;
value: FulfillmentAsn1JsonValueMap[T];
}
Expand All @@ -28,10 +30,21 @@ export default class Fulfillment {

static fromBinary(data: Buffer): Fulfillment;

static fromAsn1Json(json: FulfillmentAsn1Json): Fulfillment;
static fromAsn1Json(
json: FulfillmentAsn1Json<keyof FulfillmentAsn1JsonValueMap>
): Fulfillment;

static fromAsn1Json<T extends keyof FulfillmentAsn1JsonValueMap>(
json: FulfillmentAsn1Json<T>
): Fulfillment;

static fromJson(
json: PreimageSha256Json | PrefixSha256Json | ThresholdSha256Json | RsaSha256Json | Ed25519Sha256Json
json:
| PreimageSha256Json
| PrefixSha256Json
| ThresholdSha256Json
| RsaSha256Json
| Ed25519Sha256Json
): Fulfillment;

getTypeId(): TypeId;
Expand All @@ -51,12 +64,20 @@ export default class Fulfillment {
private calculateCost(): number;

parseAsn1JsonPayload(
json: PreimageSha256Json | PrefixSha256Json | ThresholdSha256Json | RsaSha256Json | Ed25519Sha256Json
json: FulfillmentAsn1Json<keyof FulfillmentAsn1JsonValueMap>['value']
): void;

parseAsn1JsonPayload<T extends keyof FulfillmentAsn1JsonValueMap>(
json: FulfillmentAsn1JsonValueMap[T]
): void;

serializeUri(): string;

getAsn1Json(): FulfillmentAsn1Json;
getAsn1Json(): FulfillmentAsn1Json<keyof FulfillmentAsn1JsonValueMap>;

getAsn1Json<
T extends keyof FulfillmentAsn1JsonValueMap
>(): FulfillmentAsn1Json<T>;

serializeBinary(): Buffer;

Expand Down
2 changes: 1 addition & 1 deletion types/lib/type-registry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface RegisteredType {
}

export default class TypeRegistry {
private registeredTypes: RegisteredType[] = [];
private registeredTypes: RegisteredType[];

static findByTypeId(typeId: TypeId): RegisteredType['Class'];

Expand Down
3 changes: 0 additions & 3 deletions types/types/ed25519-sha256.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const TYPE_ASN1_CONDITION: TypeAsn1Condition.Ed25519Sha256;
export const TYPE_ASN1_FULFILLMENT: TypeAsn1Fulfillment.Ed25519Sha256;
export const TYPE_CATEGORY: TypeCategory.Ed25519Sha256;

export const TYPE_ID = TypeId.Ed25519Sha256;
export const CONSTANT_COST = 131072;
export default class Ed25519Sha256 extends BaseSha256 {
private publicKey: Buffer;
Expand Down Expand Up @@ -43,7 +42,5 @@ export default class Ed25519Sha256 extends BaseSha256 {

getAsn1JsonPayload(): Ed25519Sha256Asn1Json;

private calculateCost(): number;

validate(message: Buffer): boolean;
}
6 changes: 2 additions & 4 deletions types/types/prefix-sha256.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
TypeName,
} from '.';
import type Condition from '../lib/condition';
import type Fulfillment from '../lib/fulfillment';
import Fulfillment, { FulfillmentAsn1JsonValueMap } from '../lib/fulfillment';
import type BaseSha256 from './base-sha256';

export const TYPE_ID = TypeId.PrefixSha256;
Expand Down Expand Up @@ -52,9 +52,7 @@ export default class PrefixSha256 extends BaseSha256 {

parseJson(json: PrefixSha256Json): void;

parseAsn1JsonPayload(json: PrefixSha256Asn1Json): void;

private calculateCost(): number;
parseAsn1JsonPayload(json: FulfillmentAsn1JsonValueMap[TypeAsn1Fulfillment.PrefixSha256]): void;

validate(message: Buffer): boolean;
}
2 changes: 0 additions & 2 deletions types/types/preimage-sha256.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ export default class PreimageSha256 extends BaseSha256 {

getAsn1JsonPayload(): PreimageSha256Asn1Json;

private calculateCost(): number;

validate(message: Buffer): boolean;
}
3 changes: 1 addition & 2 deletions types/types/rsa-sha256.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
RsaSha256Asn1Json,
RsaSha256Json,
TypeAsn1Condition,
TypeAsn1Fulfillment,
Expand Down Expand Up @@ -42,7 +43,5 @@ export default class RsaSha256 extends BaseSha256 {

sign(message: Buffer, privateKey: string): void;

private calculateCost(): number;

validate(message: Buffer): boolean;
}
12 changes: 5 additions & 7 deletions types/types/threshold-sha256.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
TypeName,
} from '.';
import type Condition from '../lib/condition';
import type Fulfillment from '../lib/fulfillment';
import Fulfillment, { FulfillmentAsn1JsonValueMap } from '../lib/fulfillment';
import type BaseSha256 from './base-sha256';

export const CONDITION = 'condition';
Expand All @@ -19,7 +19,7 @@ interface SubConditionBodyMap {
fulfillment: Fulfillment;
}

export type SubCondition<T = 'condition' | 'fulfillment'> = {
export type SubCondition<T extends keyof SubConditionBodyMap> = {
type: T;
body: SubConditionBodyMap[T];
};
Expand All @@ -32,7 +32,7 @@ export const TYPE_CATEGORY = TypeCategory.ThresholdSha256;

export default class ThresholdSha256 extends BaseSha256 {
private threshold: number;
private subconditions: SubCondition[];
private subconditions: SubCondition<keyof SubConditionBodyMap>[];

static TYPE_ID: TypeId.ThresholdSha256;
static TYPE_NAME: TypeName.ThresholdSha256;
Expand All @@ -54,9 +54,7 @@ export default class ThresholdSha256 extends BaseSha256 {

private getFingerprintContents(): Buffer;

private calculateCost(): number;

static getSubconditionCost(cond: SubCondition): number;
static getSubconditionCost<T extends keyof SubConditionBodyMap>(cond: SubCondition<T>): number;

private static calculateWorstCaseLength(
threshold: number,
Expand All @@ -65,7 +63,7 @@ export default class ThresholdSha256 extends BaseSha256 {

parseJson(json: ThresholdSha256Json): void;

parseAsn1JsonPayload(json: ThresholdSha256Asn1Json): void;
parseAsn1JsonPayload(json: FulfillmentAsn1JsonValueMap[TypeAsn1Fulfillment.ThresholdSha256]): void;

getAsn1JsonPayload(): ThresholdSha256Asn1Json;

Expand Down

0 comments on commit af3bdc1

Please sign in to comment.