Skip to content

Commit 77b96a2

Browse files
committed
refactor: ed25519 key methods are synchronous
1 parent c130a0c commit 77b96a2

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

packages/crypto/src/keys/ed25519-browser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const KEYS_BYTE_LENGTH = 32
88
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
99
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
1010

11-
export async function generateKey (): Promise<Uint8ArrayKeyPair> {
11+
export function generateKey (): Uint8ArrayKeyPair {
1212
// the actual private key (32 bytes)
1313
const privateKeyRaw = ed.utils.randomPrivateKey()
1414
const publicKey = ed.getPublicKey(privateKeyRaw)
@@ -25,7 +25,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
2525
/**
2626
* Generate keypair from a 32 byte uint8array
2727
*/
28-
export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8ArrayKeyPair> {
28+
export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
2929
if (seed.length !== KEYS_BYTE_LENGTH) {
3030
throw new TypeError('"seed" must be 32 bytes in length.')
3131
} else if (!(seed instanceof Uint8Array)) {
@@ -44,13 +44,13 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
4444
}
4545
}
4646

47-
export async function hashAndSign (privateKey: Uint8Array, msg: Uint8Array): Promise<Uint8Array> {
47+
export function hashAndSign (privateKey: Uint8Array, msg: Uint8Array): Uint8Array {
4848
const privateKeyRaw = privateKey.subarray(0, KEYS_BYTE_LENGTH)
4949

5050
return ed.sign(msg, privateKeyRaw)
5151
}
5252

53-
export async function hashAndVerify (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
53+
export function hashAndVerify (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array): boolean {
5454
return ed.verify(sig, msg, publicKey)
5555
}
5656

packages/crypto/src/keys/ed25519-class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ export function unmarshalEd25519PublicKey (bytes: Uint8Array): Ed25519PublicKey
128128
}
129129

130130
export async function generateKeyPair (): Promise<Ed25519PrivateKey> {
131-
const { privateKey, publicKey } = await crypto.generateKey()
131+
const { privateKey, publicKey } = crypto.generateKey()
132132
return new Ed25519PrivateKey(privateKey, publicKey)
133133
}
134134

135135
export async function generateKeyPairFromSeed (seed: Uint8Array): Promise<Ed25519PrivateKey> {
136-
const { privateKey, publicKey } = await crypto.generateKeyFromSeed(seed)
136+
const { privateKey, publicKey } = crypto.generateKeyFromSeed(seed)
137137
return new Ed25519PrivateKey(privateKey, publicKey)
138138
}
139139

packages/crypto/src/keys/ed25519.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import crypto from 'crypto'
2-
import { promisify } from 'util'
32
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
43
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
54
import type { Uint8ArrayKeyPair } from './interface.js'
65

7-
const keypair = promisify(crypto.generateKeyPair)
6+
const keypair = crypto.generateKeyPairSync
87

98
const PUBLIC_KEY_BYTE_LENGTH = 32
109
const PRIVATE_KEY_BYTE_LENGTH = 64 // private key is actually 32 bytes but for historical reasons we concat private and public keys
@@ -35,8 +34,8 @@ function derivePublicKey (privateKey: Uint8Array): Uint8Array {
3534
return uint8arrayFromString(jwk.x, 'base64url')
3635
}
3736

38-
export async function generateKey (): Promise<Uint8ArrayKeyPair> {
39-
const key = await keypair('ed25519', {
37+
export function generateKey (): Uint8ArrayKeyPair {
38+
const key = keypair('ed25519', {
4039
publicKeyEncoding: { type: 'spki', format: 'jwk' },
4140
privateKeyEncoding: { type: 'pkcs8', format: 'jwk' }
4241
})
@@ -55,7 +54,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
5554
/**
5655
* Generate keypair from a 32 byte uint8array
5756
*/
58-
export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8ArrayKeyPair> {
57+
export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
5958
if (seed.length !== KEYS_BYTE_LENGTH) {
6059
throw new TypeError('"seed" must be 32 bytes in length.')
6160
} else if (!(seed instanceof Uint8Array)) {
@@ -71,7 +70,7 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
7170
}
7271
}
7372

74-
export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Buffer> {
73+
export function hashAndSign (key: Uint8Array, msg: Uint8Array): Buffer {
7574
if (!(key instanceof Uint8Array)) {
7675
throw new TypeError('"key" must be a node.js Buffer, or Uint8Array.')
7776
}
@@ -102,7 +101,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Bu
102101
return crypto.sign(null, msg, obj)
103102
}
104103

105-
export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
104+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array): boolean {
106105
if (key.byteLength !== PUBLIC_KEY_BYTE_LENGTH) {
107106
throw new TypeError('"key" must be 32 bytes in length.')
108107
} else if (!(key instanceof Uint8Array)) {

0 commit comments

Comments
 (0)