1
1
import crypto from 'crypto'
2
- import { promisify } from 'util'
3
2
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
4
3
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
5
4
import type { Uint8ArrayKeyPair } from './interface.js'
6
5
7
- const keypair = promisify ( crypto . generateKeyPair )
6
+ const keypair = crypto . generateKeyPairSync
8
7
9
8
const PUBLIC_KEY_BYTE_LENGTH = 32
10
9
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 {
35
34
return uint8arrayFromString ( jwk . x , 'base64url' )
36
35
}
37
36
38
- export async function generateKey ( ) : Promise < Uint8ArrayKeyPair > {
39
- const key = await keypair ( 'ed25519' , {
37
+ export function generateKey ( ) : Uint8ArrayKeyPair {
38
+ const key = keypair ( 'ed25519' , {
40
39
publicKeyEncoding : { type : 'spki' , format : 'jwk' } ,
41
40
privateKeyEncoding : { type : 'pkcs8' , format : 'jwk' }
42
41
} )
@@ -55,7 +54,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
55
54
/**
56
55
* Generate keypair from a 32 byte uint8array
57
56
*/
58
- export async function generateKeyFromSeed ( seed : Uint8Array ) : Promise < Uint8ArrayKeyPair > {
57
+ export function generateKeyFromSeed ( seed : Uint8Array ) : Uint8ArrayKeyPair {
59
58
if ( seed . length !== KEYS_BYTE_LENGTH ) {
60
59
throw new TypeError ( '"seed" must be 32 bytes in length.' )
61
60
} else if ( ! ( seed instanceof Uint8Array ) ) {
@@ -71,7 +70,7 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
71
70
}
72
71
}
73
72
74
- export async function hashAndSign ( key : Uint8Array , msg : Uint8Array ) : Promise < Buffer > {
73
+ export function hashAndSign ( key : Uint8Array , msg : Uint8Array ) : Buffer {
75
74
if ( ! ( key instanceof Uint8Array ) ) {
76
75
throw new TypeError ( '"key" must be a node.js Buffer, or Uint8Array.' )
77
76
}
@@ -102,7 +101,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Bu
102
101
return crypto . sign ( null , msg , obj )
103
102
}
104
103
105
- export async function hashAndVerify ( key : Uint8Array , sig : Uint8Array , msg : Uint8Array ) : Promise < boolean > {
104
+ export function hashAndVerify ( key : Uint8Array , sig : Uint8Array , msg : Uint8Array ) : boolean {
106
105
if ( key . byteLength !== PUBLIC_KEY_BYTE_LENGTH ) {
107
106
throw new TypeError ( '"key" must be 32 bytes in length.' )
108
107
} else if ( ! ( key instanceof Uint8Array ) ) {
0 commit comments