Skip to content

Commit 9f447bc

Browse files
committed
Import / Export for EdKeypair
1 parent a49d5c9 commit 9f447bc

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

packages/default-plugins/src/ed25519/keypair.ts

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export class EdKeypair implements DidableKey, ExportableKey {
5252
return uint8arrays.toString(this.secretKey, format)
5353
}
5454

55+
static async import(secretKey: string, params?: { exportable: boolean }): Promise<EdKeypair> {
56+
const { exportable = false } = params || {}
57+
return EdKeypair.fromSecretKey(secretKey, { exportable })
58+
}
5559
}
5660

5761

packages/default-plugins/tests/ecdsa.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ describe("import and exporting a key", () => {
101101
signedMessage = await newKey.sign(msg)
102102
expect(await p256Plugin.verifySignature(exportableKeypair.did(), msg, signedMessage)).toBe(true)
103103
})
104-
})
104+
})

packages/default-plugins/tests/ed25519.test.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ed25519Plugin } from "../src/ed25519/plugin.js"
2-
import EdwardsKey from "../src/ed25519/keypair.js"
2+
import EdwardsKey, { EdKeypair } from "../src/ed25519/keypair.js"
33

44
describe("ed25519", () => {
55

@@ -25,3 +25,39 @@ describe("ed25519", () => {
2525
})
2626

2727
})
28+
29+
describe("Import / Export", () => {
30+
let exportableKey: EdKeypair
31+
let nonExportableKey: EdKeypair
32+
33+
beforeAll(async () => {
34+
exportableKey = await EdKeypair.create({ exportable: true })
35+
nonExportableKey = await EdKeypair.create({ exportable: false })
36+
})
37+
38+
it("Will export a key that is exportable", async () => {
39+
const exported = exportableKey.export()
40+
expect(exported).not.toBe(null)
41+
})
42+
43+
it("Will not export a key that is not exportable", async () => {
44+
await expect(nonExportableKey.export())
45+
.rejects
46+
.toThrow("Key is not exportable")
47+
})
48+
49+
it("Will import an exported key", async () => {
50+
const exported = await exportableKey.export()
51+
const newKey = await EdKeypair.import(exported)
52+
53+
expect(newKey.did()).toEqual(exportableKey.did())
54+
55+
// Sign and verify
56+
const msg = new Uint8Array(Buffer.from("test signing", "utf-8"))
57+
let signed = await exportableKey.sign(msg)
58+
expect(await ed25519Plugin.verifySignature(await newKey.did(), msg, signed)).toBe(true)
59+
60+
signed = await newKey.sign(msg)
61+
expect(await ed25519Plugin.verifySignature(await exportableKey.did(), msg, signed)).toBe(true)
62+
})
63+
})

0 commit comments

Comments
 (0)