Skip to content

Commit 73fe05a

Browse files
committed
chore: format with deno
1 parent e090940 commit 73fe05a

File tree

6 files changed

+185
-54
lines changed

6 files changed

+185
-54
lines changed

typescript/packages/common-identity/src/ed25519/index.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import {
2-
KeyPairRaw,
3-
isInsecureCryptoKeyPair,
2+
AsBytes,
3+
DIDKey,
44
isCryptoKeyPair,
5+
isInsecureCryptoKeyPair,
6+
KeyPairRaw,
57
Signer,
68
Verifier,
7-
DIDKey,
8-
AsBytes,
99
} from "../interface.ts";
10-
import { NativeEd25519Signer, NativeEd25519Verifier, isNativeEd25519Supported } from "./native.ts";
10+
import {
11+
isNativeEd25519Supported,
12+
NativeEd25519Signer,
13+
NativeEd25519Verifier,
14+
} from "./native.ts";
1115
import { NobleEd25519Signer, NobleEd25519Verifier } from "./noble.ts";
1216
import * as bip39 from "@scure/bip39";
1317
import { wordlist } from "@scure/bip39/wordlists/english";
@@ -41,7 +45,9 @@ export class Ed25519Signer<ID extends DIDKey> implements Signer<ID> {
4145
return this.impl.sign(payload);
4246
}
4347

44-
static async fromRaw<ID extends DIDKey>(rawPrivateKey: Uint8Array): Promise<Ed25519Signer<ID>> {
48+
static async fromRaw<ID extends DIDKey>(
49+
rawPrivateKey: Uint8Array,
50+
): Promise<Ed25519Signer<ID>> {
4551
return new Ed25519Signer(
4652
(await isNativeEd25519Supported())
4753
? await NativeEd25519Signer.fromRaw(rawPrivateKey)
@@ -57,19 +63,27 @@ export class Ed25519Signer<ID extends DIDKey> implements Signer<ID> {
5763
);
5864
}
5965

60-
static async generateMnemonic<ID extends DIDKey>(): Promise<[Ed25519Signer<ID>, string]> {
66+
static async generateMnemonic<ID extends DIDKey>(): Promise<
67+
[Ed25519Signer<ID>, string]
68+
> {
6169
let mnemonic = bip39.generateMnemonic(wordlist, 256);
6270
return [await Ed25519Signer.fromMnemonic(mnemonic), mnemonic];
6371
}
6472

65-
static async fromMnemonic<ID extends DIDKey>(mnemonic: string): Promise<Ed25519Signer<ID>> {
73+
static async fromMnemonic<ID extends DIDKey>(
74+
mnemonic: string,
75+
): Promise<Ed25519Signer<ID>> {
6676
let bytes = bip39.mnemonicToEntropy(mnemonic, wordlist);
6777
return await Ed25519Signer.fromRaw(bytes);
6878
}
6979

70-
static async deserialize<ID extends DIDKey>(input: KeyPairRaw): Promise<Ed25519Signer<ID>> {
80+
static async deserialize<ID extends DIDKey>(
81+
input: KeyPairRaw,
82+
): Promise<Ed25519Signer<ID>> {
7183
if (isCryptoKeyPair(input)) {
72-
return new Ed25519Signer(await NativeEd25519Signer.deserialize<ID>(input));
84+
return new Ed25519Signer(
85+
await NativeEd25519Signer.deserialize<ID>(input),
86+
);
7387
} else if (isInsecureCryptoKeyPair(input)) {
7488
return new Ed25519Signer(await NobleEd25519Signer.deserialize(input));
7589
} else {
@@ -92,15 +106,19 @@ export class Ed25519Verifier<ID extends DIDKey> implements Verifier<ID> {
92106
return this.impl.did();
93107
}
94108

95-
static async fromDid<ID extends DIDKey>(did: ID): Promise<Ed25519Verifier<ID>> {
109+
static async fromDid<ID extends DIDKey>(
110+
did: ID,
111+
): Promise<Ed25519Verifier<ID>> {
96112
return new Ed25519Verifier(
97113
(await isNativeEd25519Supported())
98114
? await NativeEd25519Verifier.fromDid(did)
99115
: await NobleEd25519Verifier.fromDid(did),
100116
);
101117
}
102118

103-
static async fromRaw<ID extends DIDKey>(rawPublicKey: Uint8Array): Promise<Ed25519Verifier<ID>> {
119+
static async fromRaw<ID extends DIDKey>(
120+
rawPublicKey: Uint8Array,
121+
): Promise<Ed25519Verifier<ID>> {
104122
return new Ed25519Verifier(
105123
(await isNativeEd25519Supported())
106124
? await NativeEd25519Verifier.fromRaw(rawPublicKey)

typescript/packages/common-identity/src/ed25519/native.ts

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import * as ed25519 from "@noble/ed25519";
2-
import { ED25519_ALG, bytesToDid, didToBytes, AuthorizationError } from "./utils.ts";
3-
import { AsBytes, DIDKey, Signature, Signer, Verifier, Result } from "../interface.ts";
2+
import {
3+
AuthorizationError,
4+
bytesToDid,
5+
didToBytes,
6+
ED25519_ALG,
7+
} from "./utils.ts";
8+
import {
9+
AsBytes,
10+
DIDKey,
11+
Result,
12+
Signature,
13+
Signer,
14+
Verifier,
15+
} from "../interface.ts";
416
import { clone } from "../utils.ts";
517

618
// WebCrypto Key formats for Ed25519
@@ -27,9 +39,15 @@ export const isNativeEd25519Supported = (() => {
2739
}
2840
let dummyKey = new Uint8Array(32);
2941
try {
30-
let key = await window.crypto.subtle.importKey("raw", dummyKey, ED25519_ALG, false, [
31-
"verify",
32-
]);
42+
let key = await window.crypto.subtle.importKey(
43+
"raw",
44+
dummyKey,
45+
ED25519_ALG,
46+
false,
47+
[
48+
"verify",
49+
],
50+
);
3351
await clone(key);
3452
isSupported = true;
3553
} catch (e) {
@@ -54,7 +72,10 @@ export class NativeEd25519Signer<ID extends DIDKey> implements Signer<ID> {
5472

5573
get verifier(): Verifier<ID> {
5674
if (!this.#verifier) {
57-
this.#verifier = new NativeEd25519Verifier<ID>(this.keypair.publicKey, this._did);
75+
this.#verifier = new NativeEd25519Verifier<ID>(
76+
this.keypair.publicKey,
77+
this._did,
78+
);
5879
}
5980
return this.#verifier;
6081
}
@@ -66,7 +87,11 @@ export class NativeEd25519Signer<ID extends DIDKey> implements Signer<ID> {
6687
async sign<T>(payload: AsBytes<T>): Promise<Result<Signature<T>, Error>> {
6788
try {
6889
const signature = new Uint8Array(
69-
await window.crypto.subtle.sign(ED25519_ALG, this.keypair.privateKey, payload),
90+
await window.crypto.subtle.sign(
91+
ED25519_ALG,
92+
this.keypair.privateKey,
93+
payload,
94+
),
7095
);
7196

7297
return { ok: signature as Signature<T> };
@@ -88,17 +113,26 @@ export class NativeEd25519Signer<ID extends DIDKey> implements Signer<ID> {
88113
["sign"],
89114
);
90115
// Set the public key to be extractable for DID generation.
91-
const publicKey = await window.crypto.subtle.importKey("raw", rawPublic, ED25519_ALG, true, [
92-
"verify",
93-
]);
116+
const publicKey = await window.crypto.subtle.importKey(
117+
"raw",
118+
rawPublic,
119+
ED25519_ALG,
120+
true,
121+
[
122+
"verify",
123+
],
124+
);
94125
let did = bytesToDid(new Uint8Array(rawPublic));
95126
return new NativeEd25519Signer({ publicKey, privateKey }, did as ID);
96127
}
97128

98129
static async generate<ID extends DIDKey>(): Promise<NativeEd25519Signer<ID>> {
99130
// This notably sets only the public key as extractable, ideal as we need
100131
// access to the public key for DID generation.
101-
let keypair = await window.crypto.subtle.generateKey(ED25519_ALG, false, ["sign", "verify"]);
132+
let keypair = await window.crypto.subtle.generateKey(ED25519_ALG, false, [
133+
"sign",
134+
"verify",
135+
]);
102136
let did = await didFromPublicKey(keypair.publicKey);
103137
return new NativeEd25519Signer(keypair, did as ID);
104138
}
@@ -121,15 +155,26 @@ export class NativeEd25519Verifier<ID extends DIDKey> implements Verifier<ID> {
121155
return this._did;
122156
}
123157

124-
async verify({ signature, payload }: { payload: Uint8Array; signature: Uint8Array }) {
125-
if (await window.crypto.subtle.verify(ED25519_ALG, this.publicKey, signature, payload)) {
158+
async verify(
159+
{ signature, payload }: { payload: Uint8Array; signature: Uint8Array },
160+
) {
161+
if (
162+
await window.crypto.subtle.verify(
163+
ED25519_ALG,
164+
this.publicKey,
165+
signature,
166+
payload,
167+
)
168+
) {
126169
return { ok: {} };
127170
} else {
128171
return { error: new AuthorizationError("Invalid signature") };
129172
}
130173
}
131174

132-
static async fromDid<ID extends DIDKey>(did: ID): Promise<NativeEd25519Verifier<ID>> {
175+
static async fromDid<ID extends DIDKey>(
176+
did: ID,
177+
): Promise<NativeEd25519Verifier<ID>> {
133178
let bytes = didToBytes(did);
134179
return await NativeEd25519Verifier.fromRaw(bytes);
135180
}
@@ -139,16 +184,39 @@ export class NativeEd25519Verifier<ID extends DIDKey> implements Verifier<ID> {
139184
): Promise<NativeEd25519Verifier<ID>> {
140185
let did = bytesToDid(new Uint8Array(rawPublicKey)) as ID;
141186
// Set the public key to be extractable for DID generation.
142-
const publicKey = await window.crypto.subtle.importKey("raw", rawPublicKey, ED25519_ALG, true, [
143-
"verify",
144-
]);
187+
const publicKey = await window.crypto.subtle.importKey(
188+
"raw",
189+
rawPublicKey,
190+
ED25519_ALG,
191+
true,
192+
[
193+
"verify",
194+
],
195+
);
145196
return new NativeEd25519Verifier(publicKey, did);
146197
}
147198
}
148199

149200
// 0x302e020100300506032b657004220420
150201
// via https://stackoverflow.com/a/79135112
151-
const PKCS8_PREFIX = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
202+
const PKCS8_PREFIX = new Uint8Array([
203+
48,
204+
46,
205+
2,
206+
1,
207+
0,
208+
48,
209+
5,
210+
6,
211+
3,
212+
43,
213+
101,
214+
112,
215+
4,
216+
34,
217+
4,
218+
32,
219+
]);
152220

153221
// Signer Ed25519 keys cannot be imported into Subtle Crypto in "raw" format.
154222
// Convert to "pkcs8" before doing so.

typescript/packages/common-identity/src/ed25519/noble.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as ed25519 from "@noble/ed25519";
22
import {
3-
InsecureCryptoKeyPair,
4-
Verifier,
5-
Signer,
6-
DIDKey,
73
AsBytes,
8-
Signature,
4+
DIDKey,
5+
InsecureCryptoKeyPair,
96
Result,
7+
Signature,
8+
Signer,
9+
Verifier,
1010
} from "../interface.ts";
11-
import { bytesToDid, didToBytes, AuthorizationError } from "./utils.ts";
11+
import { AuthorizationError, bytesToDid, didToBytes } from "./utils.ts";
1212

1313
export class NobleEd25519Signer<ID extends DIDKey> implements Signer<ID> {
1414
private keypair: InsecureCryptoKeyPair;
@@ -33,15 +33,20 @@ export class NobleEd25519Signer<ID extends DIDKey> implements Signer<ID> {
3333

3434
async sign<T>(payload: AsBytes<T>): Promise<Result<Signature<T>, Error>> {
3535
try {
36-
const signature = await ed25519.signAsync(payload, this.keypair.privateKey);
36+
const signature = await ed25519.signAsync(
37+
payload,
38+
this.keypair.privateKey,
39+
);
3740

3841
return { ok: signature as Signature<T> };
3942
} catch (cause) {
4043
return { error: cause as Error };
4144
}
4245
}
4346

44-
static async fromRaw<ID extends DIDKey>(privateKey: Uint8Array): Promise<NobleEd25519Signer<ID>> {
47+
static async fromRaw<ID extends DIDKey>(
48+
privateKey: Uint8Array,
49+
): Promise<NobleEd25519Signer<ID>> {
4550
const publicKey = await ed25519.getPublicKeyAsync(privateKey);
4651
return new NobleEd25519Signer({ publicKey, privateKey });
4752
}
@@ -64,7 +69,9 @@ export class NobleEd25519Verifier<ID extends DIDKey> implements Verifier<ID> {
6469
this._did = bytesToDid(publicKey) as ID;
6570
}
6671

67-
async verify({ signature, payload }: { payload: Uint8Array; signature: Uint8Array }) {
72+
async verify(
73+
{ signature, payload }: { payload: Uint8Array; signature: Uint8Array },
74+
) {
6875
if (await ed25519.verifyAsync(signature, payload, this.publicKey)) {
6976
return { ok: {} };
7077
} else {
@@ -76,7 +83,9 @@ export class NobleEd25519Verifier<ID extends DIDKey> implements Verifier<ID> {
7683
return this._did;
7784
}
7885

79-
static async fromDid<ID extends DIDKey>(did: ID): Promise<NobleEd25519Verifier<ID>> {
86+
static async fromDid<ID extends DIDKey>(
87+
did: ID,
88+
): Promise<NobleEd25519Verifier<ID>> {
8089
let bytes = didToBytes(did);
8190
return await NobleEd25519Verifier.fromRaw(bytes);
8291
}

typescript/packages/common-identity/src/ed25519/utils.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,31 @@ export const ED25519_ALG = "Ed25519";
66
const ED25519_CODE = 0xed;
77
const ED25519_PUB_KEY_RAW_SIZE = 32;
88
const ED25519_PUB_KEY_TAG_SIZE = varint.encodingLength(ED25519_CODE);
9-
const ED25519_PUB_KEY_TAGGED_SIZE = ED25519_PUB_KEY_RAW_SIZE + ED25519_PUB_KEY_TAG_SIZE;
9+
const ED25519_PUB_KEY_TAGGED_SIZE = ED25519_PUB_KEY_RAW_SIZE +
10+
ED25519_PUB_KEY_TAG_SIZE;
1011
const DID_KEY_PREFIX = `did:key:`;
1112
const DID_KEY_PREFIX_SIZE = DID_KEY_PREFIX.length;
1213

1314
// 0x302e020100300506032b657004220420
1415
// via https://stackoverflow.com/a/79135112
15-
const PKCS8_PREFIX = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
16+
const PKCS8_PREFIX = new Uint8Array([
17+
48,
18+
46,
19+
2,
20+
1,
21+
0,
22+
48,
23+
5,
24+
6,
25+
3,
26+
43,
27+
101,
28+
112,
29+
4,
30+
34,
31+
4,
32+
32,
33+
]);
1634

1735
// Private Ed25519 keys cannot be imported into Subtle Crypto in "raw" format.
1836
// Convert to "pkcs8" before doing so.
@@ -37,9 +55,11 @@ export function didToBytes(did: DIDKey): Uint8Array {
3755
const [code] = varint.decode(bytes);
3856
if (code !== ED25519_CODE) {
3957
throw new RangeError(
40-
`Unsupported key algorithm expected 0x${ED25519_CODE.toString(
41-
16,
42-
)}, instead of 0x${code.toString(16)}`,
58+
`Unsupported key algorithm expected 0x${
59+
ED25519_CODE.toString(
60+
16,
61+
)
62+
}, instead of 0x${code.toString(16)}`,
4363
);
4464
}
4565
if (bytes.length !== ED25519_PUB_KEY_TAGGED_SIZE) {

0 commit comments

Comments
 (0)