Skip to content

Commit 526fd1d

Browse files
authored
Merge pull request #10 from bigchaindb/fix-typedefs
Fix typedefs
2 parents b0c13c8 + 7cd3f47 commit 526fd1d

File tree

10 files changed

+53
-38
lines changed

10 files changed

+53
-38
lines changed

src/types/ed25519-sha256.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* @module types
55
*/
66

7-
const nacl = require('tweetnacl')
7+
const { sign } = require('tweetnacl')
88
const BaseSha256 = require('./base-sha256')
99
const MissingDataError = require('../errors/missing-data-error')
1010
const ValidationError = require('../errors/validation-error')
11+
const bufferToUint8Array = require('../util/buffer-to-uint-array')
1112
const Asn1Ed25519FingerprintContents = require('../schemas/fingerprint').Ed25519FingerprintContents
1213

1314
let ed25519
@@ -102,9 +103,9 @@ class Ed25519Sha256 extends BaseSha256 {
102103
this.setPublicKey(keyPair.publicKey)
103104
this.signature = ed25519.Sign(message, keyPair)
104105
} else {
105-
const keyPair = nacl.sign.keyPair.fromSeed(privateKey)
106+
const keyPair = sign.keyPair.fromSeed(bufferToUint8Array(privateKey))
106107
this.setPublicKey(Buffer.from(keyPair.publicKey))
107-
this.signature = Buffer.from(nacl.sign.detached(message, keyPair.secretKey))
108+
this.signature = Buffer.from(sign.detached(bufferToUint8Array(message), keyPair.secretKey))
108109
}
109110
}
110111

@@ -170,7 +171,7 @@ class Ed25519Sha256 extends BaseSha256 {
170171
if (ed25519) {
171172
result = ed25519.Verify(message, this.signature, this.publicKey)
172173
} else {
173-
result = nacl.sign.detached.verify(message, this.signature, this.publicKey)
174+
result = sign.detached.verify(bufferToUint8Array(message), bufferToUint8Array(this.signature), bufferToUint8Array(this.publicKey))
174175
}
175176

176177
if (result !== true) {

src/util/buffer-to-uint-array.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function bufferToUint8Array (buffer) {
2+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT)
3+
}

types/index.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import type Condition from './lib/condition';
2-
import type Fulfillment from './lib/fulfillment';
3-
import type TypeRegistry from './lib/type-registry';
4-
import type {
1+
import Condition from './lib/condition';
2+
import Fulfillment from './lib/fulfillment';
3+
import TypeRegistry from './lib/type-registry';
4+
import {
55
Ed25519Sha256Json,
66
PrefixSha256Json,
77
PreimageSha256Json,
88
RsaSha256Json,
99
ThresholdSha256Json,
1010
} from './types';
11-
import type PreimageSha256 from './types/preimage-sha256';
12-
import type PrefixSha256 from './types/prefix-sha256';
13-
import type ThresholdSha256 from './types/threshold-sha256';
14-
import type RsaSha256 from './types/rsa-sha256';
15-
import type Ed25519Sha256 from './types/ed25519-sha256';
16-
import type base64url from './util/base64url';
11+
import PreimageSha256 from './types/preimage-sha256';
12+
import PrefixSha256 from './types/prefix-sha256';
13+
import ThresholdSha256 from './types/threshold-sha256';
14+
import RsaSha256 from './types/rsa-sha256';
15+
import Ed25519Sha256 from './types/ed25519-sha256';
16+
import base64url from './util/base64url';
1717

1818
export { base64url, Condition, Fulfillment, TypeRegistry };
1919

types/lib/fulfillment.d.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ interface FulfillmentAsn1JsonValueMap {
1818
[TypeAsn1Fulfillment.Ed25519Sha256]: Ed25519Sha256Json;
1919
}
2020

21-
export interface FulfillmentAsn1Json<T = TypeAsn1Fulfillment> {
21+
export interface FulfillmentAsn1Json<
22+
T extends keyof FulfillmentAsn1JsonValueMap
23+
> {
2224
type: T;
2325
value: FulfillmentAsn1JsonValueMap[T];
2426
}
@@ -28,10 +30,21 @@ export default class Fulfillment {
2830

2931
static fromBinary(data: Buffer): Fulfillment;
3032

31-
static fromAsn1Json(json: FulfillmentAsn1Json): Fulfillment;
33+
static fromAsn1Json(
34+
json: FulfillmentAsn1Json<keyof FulfillmentAsn1JsonValueMap>
35+
): Fulfillment;
36+
37+
static fromAsn1Json<T extends keyof FulfillmentAsn1JsonValueMap>(
38+
json: FulfillmentAsn1Json<T>
39+
): Fulfillment;
3240

3341
static fromJson(
34-
json: PreimageSha256Json | PrefixSha256Json | ThresholdSha256Json | RsaSha256Json | Ed25519Sha256Json
42+
json:
43+
| PreimageSha256Json
44+
| PrefixSha256Json
45+
| ThresholdSha256Json
46+
| RsaSha256Json
47+
| Ed25519Sha256Json
3548
): Fulfillment;
3649

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

5366
parseAsn1JsonPayload(
54-
json: PreimageSha256Json | PrefixSha256Json | ThresholdSha256Json | RsaSha256Json | Ed25519Sha256Json
67+
json: FulfillmentAsn1Json<keyof FulfillmentAsn1JsonValueMap>['value']
68+
): void;
69+
70+
parseAsn1JsonPayload<T extends keyof FulfillmentAsn1JsonValueMap>(
71+
json: FulfillmentAsn1JsonValueMap[T]
5572
): void;
5673

5774
serializeUri(): string;
5875

59-
getAsn1Json(): FulfillmentAsn1Json;
76+
getAsn1Json(): FulfillmentAsn1Json<keyof FulfillmentAsn1JsonValueMap>;
77+
78+
getAsn1Json<
79+
T extends keyof FulfillmentAsn1JsonValueMap
80+
>(): FulfillmentAsn1Json<T>;
6081

6182
serializeBinary(): Buffer;
6283

types/lib/type-registry.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface RegisteredType {
2424
}
2525

2626
export default class TypeRegistry {
27-
private registeredTypes: RegisteredType[] = [];
27+
private registeredTypes: RegisteredType[];
2828

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

types/types/ed25519-sha256.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export const TYPE_ASN1_CONDITION: TypeAsn1Condition.Ed25519Sha256;
1515
export const TYPE_ASN1_FULFILLMENT: TypeAsn1Fulfillment.Ed25519Sha256;
1616
export const TYPE_CATEGORY: TypeCategory.Ed25519Sha256;
1717

18-
export const TYPE_ID = TypeId.Ed25519Sha256;
1918
export const CONSTANT_COST = 131072;
2019
export default class Ed25519Sha256 extends BaseSha256 {
2120
private publicKey: Buffer;
@@ -43,7 +42,5 @@ export default class Ed25519Sha256 extends BaseSha256 {
4342

4443
getAsn1JsonPayload(): Ed25519Sha256Asn1Json;
4544

46-
private calculateCost(): number;
47-
4845
validate(message: Buffer): boolean;
4946
}

types/types/prefix-sha256.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
TypeName,
99
} from '.';
1010
import type Condition from '../lib/condition';
11-
import type Fulfillment from '../lib/fulfillment';
11+
import Fulfillment, { FulfillmentAsn1JsonValueMap } from '../lib/fulfillment';
1212
import type BaseSha256 from './base-sha256';
1313

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

5353
parseJson(json: PrefixSha256Json): void;
5454

55-
parseAsn1JsonPayload(json: PrefixSha256Asn1Json): void;
56-
57-
private calculateCost(): number;
55+
parseAsn1JsonPayload(json: FulfillmentAsn1JsonValueMap[TypeAsn1Fulfillment.PrefixSha256]): void;
5856

5957
validate(message: Buffer): boolean;
6058
}

types/types/preimage-sha256.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ export default class PreimageSha256 extends BaseSha256 {
3232

3333
getAsn1JsonPayload(): PreimageSha256Asn1Json;
3434

35-
private calculateCost(): number;
36-
3735
validate(message: Buffer): boolean;
3836
}

types/types/rsa-sha256.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
RsaSha256Asn1Json,
23
RsaSha256Json,
34
TypeAsn1Condition,
45
TypeAsn1Fulfillment,
@@ -42,7 +43,5 @@ export default class RsaSha256 extends BaseSha256 {
4243

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

45-
private calculateCost(): number;
46-
4746
validate(message: Buffer): boolean;
4847
}

types/types/threshold-sha256.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
TypeName,
99
} from '.';
1010
import type Condition from '../lib/condition';
11-
import type Fulfillment from '../lib/fulfillment';
11+
import Fulfillment, { FulfillmentAsn1JsonValueMap } from '../lib/fulfillment';
1212
import type BaseSha256 from './base-sha256';
1313

1414
export const CONDITION = 'condition';
@@ -19,7 +19,7 @@ interface SubConditionBodyMap {
1919
fulfillment: Fulfillment;
2020
}
2121

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

3333
export default class ThresholdSha256 extends BaseSha256 {
3434
private threshold: number;
35-
private subconditions: SubCondition[];
35+
private subconditions: SubCondition<keyof SubConditionBodyMap>[];
3636

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

5555
private getFingerprintContents(): Buffer;
5656

57-
private calculateCost(): number;
58-
59-
static getSubconditionCost(cond: SubCondition): number;
57+
static getSubconditionCost<T extends keyof SubConditionBodyMap>(cond: SubCondition<T>): number;
6058

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

6664
parseJson(json: ThresholdSha256Json): void;
6765

68-
parseAsn1JsonPayload(json: ThresholdSha256Asn1Json): void;
66+
parseAsn1JsonPayload(json: FulfillmentAsn1JsonValueMap[TypeAsn1Fulfillment.ThresholdSha256]): void;
6967

7068
getAsn1JsonPayload(): ThresholdSha256Asn1Json;
7169

0 commit comments

Comments
 (0)