11import crypto from 'crypto'
2- import { promisify } from 'util'
32import { concat as uint8arrayConcat } from 'uint8arrays/concat'
43import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
54import { toString as uint8arrayToString } from 'uint8arrays/to-string'
65import type { Uint8ArrayKeyPair } from './interface.js'
76import type { Uint8ArrayList } from 'uint8arraylist'
87
9- const keypair = promisify ( crypto . generateKeyPair )
8+ const keypair = crypto . generateKeyPairSync
109
1110const PUBLIC_KEY_BYTE_LENGTH = 32
1211const PRIVATE_KEY_BYTE_LENGTH = 64 // private key is actually 32 bytes but for historical reasons we concat private and public keys
@@ -37,8 +36,8 @@ function derivePublicKey (privateKey: Uint8Array): Uint8Array {
3736 return uint8arrayFromString ( jwk . x , 'base64url' )
3837}
3938
40- export async function generateKey ( ) : Promise < Uint8ArrayKeyPair > {
41- const key = await keypair ( 'ed25519' , {
39+ export function generateKey ( ) : Uint8ArrayKeyPair {
40+ const key = keypair ( 'ed25519' , {
4241 publicKeyEncoding : { type : 'spki' , format : 'jwk' } ,
4342 privateKeyEncoding : { type : 'pkcs8' , format : 'jwk' }
4443 } )
@@ -57,7 +56,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
5756/**
5857 * Generate keypair from a 32 byte uint8array
5958 */
60- export async function generateKeyFromSeed ( seed : Uint8Array ) : Promise < Uint8ArrayKeyPair > {
59+ export function generateKeyFromSeed ( seed : Uint8Array ) : Uint8ArrayKeyPair {
6160 if ( seed . length !== KEYS_BYTE_LENGTH ) {
6261 throw new TypeError ( '"seed" must be 32 bytes in length.' )
6362 } else if ( ! ( seed instanceof Uint8Array ) ) {
@@ -73,7 +72,7 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
7372 }
7473}
7574
76- export async function hashAndSign ( key : Uint8Array , msg : Uint8Array | Uint8ArrayList ) : Promise < Buffer > {
75+ export function hashAndSign ( key : Uint8Array , msg : Uint8Array | Uint8ArrayList ) : Buffer {
7776 if ( ! ( key instanceof Uint8Array ) ) {
7877 throw new TypeError ( '"key" must be a node.js Buffer, or Uint8Array.' )
7978 }
@@ -104,7 +103,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8Array
104103 return crypto . sign ( null , msg instanceof Uint8Array ? msg : msg . subarray ( ) , obj )
105104}
106105
107- export async function hashAndVerify ( key : Uint8Array , sig : Uint8Array , msg : Uint8Array | Uint8ArrayList ) : Promise < boolean > {
106+ export function hashAndVerify ( key : Uint8Array , sig : Uint8Array , msg : Uint8Array | Uint8ArrayList ) : boolean {
108107 if ( key . byteLength !== PUBLIC_KEY_BYTE_LENGTH ) {
109108 throw new TypeError ( '"key" must be 32 bytes in length.' )
110109 } else if ( ! ( key instanceof Uint8Array ) ) {
0 commit comments