Skip to content

Commit 3a85770

Browse files
authored
chore: Use fallback ed25519 implementation in order to serialize keys for BG worker (#922)
chore: Use fallback ed25519 implementation in order to serialize keys for background charm service workers.
1 parent 3d794a0 commit 3a85770

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

background-charm-service/src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ export async function getIdentity(identityPath?: string, operatorPass?: string):
5959
console.log(`Using identity at ${identityPath}`);
6060
try {
6161
const pkcs8Key = await Deno.readFile(identityPath);
62-
return await Identity.fromPkcs8(pkcs8Key);
62+
// Deno does not support serializing `CryptoKey`, safely
63+
// passing keys to workers. Explicitly use the fallback implementation,
64+
// which makes key material available to the JS context, in order
65+
// to transfer key material to workers.
66+
// https://github.com/denoland/deno/issues/12067#issuecomment-1975001079
67+
return await Identity.fromPkcs8FallbackImplementation(pkcs8Key);
6368
} catch (e) {
6469
throw new Error(`Could not read key at ${identityPath}.`);
6570
}

deno.jsonc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
"@codemirror/lang-markdown": "npm:@codemirror/lang-markdown@^6.3.2",
6161
"@hono/hono": "npm:hono@^4.7.0",
6262
"@luca/esbuild-deno-loader": "jsr:@luca/esbuild-deno-loader",
63-
"@noble/ed25519": "npm:@noble/ed25519@^2.2.3",
64-
"@scure/bip39": "npm:@scure/bip39@^1.5.4",
6563
"@std/assert": "jsr:@std/assert@^1",
6664
"@std/async": "jsr:@std/async@^1",
6765
"@std/cli": "jsr:@std/cli@^1",

deno.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

identity/deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"exports": "./src/index.ts",
77
"imports": {
8+
"@noble/ed25519": "npm:@noble/ed25519@^2.2.3",
89
"@scure/bip39": "npm:@scure/bip39@^1.5.4"
910
}
1011
}

identity/src/ed25519/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ export class Ed25519Signer<ID extends DIDKey> implements Signer<ID> {
6161
);
6262
}
6363

64+
// Like `fromRaw` but forces the usage of `@noble/ed25519`
65+
// implementation, making private key material available to the context.
66+
static async fromRawFallbackImplementation<ID extends DIDKey>(
67+
rawPrivateKey: Uint8Array,
68+
): Promise<Ed25519Signer<ID>> {
69+
return new Ed25519Signer(await NobleEd25519Signer.fromRaw(rawPrivateKey));
70+
}
71+
6472
static async generate<ID extends DIDKey>(): Promise<Ed25519Signer<ID>> {
6573
return new Ed25519Signer(
6674
(await isNativeEd25519Supported())
@@ -87,6 +95,15 @@ export class Ed25519Signer<ID extends DIDKey> implements Signer<ID> {
8795
return await Ed25519Signer.fromRaw(raw);
8896
}
8997

98+
// Like `fromPkcs8` but forces the usage of `@noble/ed25519`
99+
// implementation, making private key material available to the context.
100+
static async fromPkcs8FallbackImplementation<ID extends DIDKey>(
101+
pkcs8: Uint8Array,
102+
): Promise<Ed25519Signer<ID>> {
103+
const raw = pkcs8ToEd25519Raw(fromPEM(pkcs8));
104+
return await Ed25519Signer.fromRawFallbackImplementation(raw);
105+
}
106+
90107
static async fromMnemonic<ID extends DIDKey>(
91108
mnemonic: string,
92109
): Promise<Ed25519Signer<ID>> {

identity/src/identity.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ export class Identity<ID extends DIDKey = DIDKey> implements Signer<ID> {
8686
return new Identity(signer);
8787
}
8888

89+
// Like `fromPkcs8` but forces the usage of `@noble/ed25519`
90+
// implementation, making private key material available to the context.
91+
static async fromPkcs8FallbackImplementation<ID extends DIDKey>(
92+
pkcs8: Uint8Array,
93+
): Promise<Identity<ID>> {
94+
const signer = await Ed25519Signer.fromPkcs8FallbackImplementation<ID>(
95+
pkcs8,
96+
);
97+
return new Identity(signer);
98+
}
99+
89100
static async fromMnemonic<ID extends DIDKey>(
90101
mnemonic: string,
91102
): Promise<Identity<ID>> {

0 commit comments

Comments
 (0)