@@ -2,24 +2,23 @@ import { KeyType } from "@libp2p/interface";
22import { AbstractKeypair , IKeypair , IKeypairClass } from "./types.js" ;
33import { ERR_INVALID_KEYPAIR_TYPE } from "./constants.js" ;
44import { getDiscv5Crypto } from "../util/crypto.js" ;
5- import { toBuffer } from "../util/index.js" ;
6-
7- export function secp256k1PublicKeyToCompressed ( publicKey : Buffer ) : Buffer {
5+ import { concatBytes } from "@noble/hashes/utils" ;
6+ export function secp256k1PublicKeyToCompressed ( publicKey : Uint8Array ) : Uint8Array {
87 if ( publicKey . length === 64 ) {
9- publicKey = Buffer . concat ( [ Buffer . from ( [ 4 ] ) , publicKey ] ) ;
8+ publicKey = concatBytes ( Uint8Array . from ( [ 4 ] ) , publicKey ) ;
109 }
11- return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyConvert ( publicKey , true ) ) ;
10+ return getDiscv5Crypto ( ) . secp256k1 . publicKeyConvert ( publicKey , true ) ;
1211}
1312
14- export function secp256k1PublicKeyToRaw ( publicKey : Buffer ) : Buffer {
15- return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyConvert ( publicKey , false ) ) ;
13+ export function secp256k1PublicKeyToRaw ( publicKey : Uint8Array ) : Uint8Array {
14+ return getDiscv5Crypto ( ) . secp256k1 . publicKeyConvert ( publicKey , false ) ;
1615}
1716
1817export const Secp256k1Keypair : IKeypairClass = class Secp256k1Keypair extends AbstractKeypair implements IKeypair {
1918 readonly type : KeyType ;
2019
21- constructor ( privateKey ?: Buffer , publicKey ?: Buffer ) {
22- let pub = publicKey ?? toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyCreate ( privateKey ! ) ) ;
20+ constructor ( privateKey ?: Uint8Array , publicKey ?: Uint8Array ) {
21+ let pub = publicKey ?? getDiscv5Crypto ( ) . secp256k1 . publicKeyCreate ( privateKey ! ) ;
2322 if ( pub ) {
2423 pub = secp256k1PublicKeyToCompressed ( pub ) ;
2524 }
@@ -28,8 +27,8 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends Ab
2827 }
2928
3029 static generate ( ) : Secp256k1Keypair {
31- const privateKey = toBuffer ( getDiscv5Crypto ( ) . secp256k1 . generatePrivateKey ( ) ) ;
32- const publicKey = toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyCreate ( privateKey ) ) ;
30+ const privateKey = getDiscv5Crypto ( ) . secp256k1 . generatePrivateKey ( ) ;
31+ const publicKey = getDiscv5Crypto ( ) . secp256k1 . publicKeyCreate ( privateKey ) ;
3332 return new Secp256k1Keypair ( privateKey , publicKey ) ;
3433 }
3534
@@ -45,16 +44,16 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends Ab
4544 }
4645 return true ;
4746 }
48- sign ( msg : Buffer ) : Buffer {
49- return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . sign ( msg , this . privateKey ) ) ;
47+ sign ( msg : Uint8Array ) : Uint8Array {
48+ return getDiscv5Crypto ( ) . secp256k1 . sign ( msg , this . privateKey ) ;
5049 }
51- verify ( msg : Buffer , sig : Buffer ) : boolean {
50+ verify ( msg : Uint8Array , sig : Uint8Array ) : boolean {
5251 return getDiscv5Crypto ( ) . secp256k1 . verify ( this . publicKey , msg , sig ) ;
5352 }
54- deriveSecret ( keypair : IKeypair ) : Buffer {
53+ deriveSecret ( keypair : IKeypair ) : Uint8Array {
5554 if ( keypair . type !== this . type ) {
5655 throw new Error ( ERR_INVALID_KEYPAIR_TYPE ) ;
5756 }
58- return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . deriveSecret ( this . privateKey , keypair . publicKey ) ) ;
57+ return getDiscv5Crypto ( ) . secp256k1 . deriveSecret ( this . privateKey , keypair . publicKey ) ;
5958 }
6059} ;
0 commit comments