1- import secp256k1 from "bcrypto/lib/secp256k1.js" ;
21import { KeyType } from "@libp2p/interface" ;
32import { AbstractKeypair , IKeypair , IKeypairClass } from "./types.js" ;
43import { ERR_INVALID_KEYPAIR_TYPE } from "./constants.js" ;
4+ import { getDiscv5Crypto } from "../util/crypto.js" ;
5+ import { toBuffer } from "../util/index.js" ;
56
67export function secp256k1PublicKeyToCompressed ( publicKey : Buffer ) : Buffer {
78 if ( publicKey . length === 64 ) {
89 publicKey = Buffer . concat ( [ Buffer . from ( [ 4 ] ) , publicKey ] ) ;
910 }
10- return secp256k1 . publicKeyConvert ( publicKey , true ) ;
11- }
12-
13- export function secp256k1PublicKeyToFull ( publicKey : Buffer ) : Buffer {
14- if ( publicKey . length === 64 ) {
15- return Buffer . concat ( [ Buffer . from ( [ 4 ] ) , publicKey ] ) ;
16- }
17- return secp256k1 . publicKeyConvert ( publicKey , false ) ;
11+ return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyConvert ( publicKey , true ) ) ;
1812}
1913
2014export function secp256k1PublicKeyToRaw ( publicKey : Buffer ) : Buffer {
21- return secp256k1 . publicKeyConvert ( publicKey , false ) . slice ( 1 ) ;
15+ return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyConvert ( publicKey , false ) ) ;
2216}
2317
2418export const Secp256k1Keypair : IKeypairClass = class Secp256k1Keypair extends AbstractKeypair implements IKeypair {
2519 readonly type : KeyType ;
2620
2721 constructor ( privateKey ?: Buffer , publicKey ?: Buffer ) {
28- let pub = publicKey ?? secp256k1 . publicKeyCreate ( privateKey ! ) ;
22+ let pub = publicKey ?? toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyCreate ( privateKey ! ) ) ;
2923 if ( pub ) {
3024 pub = secp256k1PublicKeyToCompressed ( pub ) ;
3125 }
@@ -34,33 +28,33 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends Ab
3428 }
3529
3630 static generate ( ) : Secp256k1Keypair {
37- const privateKey = secp256k1 . privateKeyGenerate ( ) ;
38- const publicKey = secp256k1 . publicKeyCreate ( privateKey ) ;
31+ const privateKey = toBuffer ( getDiscv5Crypto ( ) . secp256k1 . generatePrivateKey ( ) ) ;
32+ const publicKey = toBuffer ( getDiscv5Crypto ( ) . secp256k1 . publicKeyCreate ( privateKey ) ) ;
3933 return new Secp256k1Keypair ( privateKey , publicKey ) ;
4034 }
4135
4236 privateKeyVerify ( key = this . _privateKey ) : boolean {
4337 if ( key ) {
44- return secp256k1 . privateKeyVerify ( key ) ;
38+ return getDiscv5Crypto ( ) . secp256k1 . privateKeyVerify ( key ) ;
4539 }
4640 return true ;
4741 }
4842 publicKeyVerify ( key = this . _publicKey ) : boolean {
4943 if ( key ) {
50- return secp256k1 . publicKeyVerify ( key ) ;
44+ return getDiscv5Crypto ( ) . secp256k1 . publicKeyVerify ( key ) ;
5145 }
5246 return true ;
5347 }
5448 sign ( msg : Buffer ) : Buffer {
55- return secp256k1 . sign ( msg , this . privateKey ) ;
49+ return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . sign ( msg , this . privateKey ) ) ;
5650 }
5751 verify ( msg : Buffer , sig : Buffer ) : boolean {
58- return secp256k1 . verify ( msg , sig , this . publicKey ) ;
52+ return getDiscv5Crypto ( ) . secp256k1 . verify ( this . publicKey , msg , sig ) ;
5953 }
6054 deriveSecret ( keypair : IKeypair ) : Buffer {
6155 if ( keypair . type !== this . type ) {
6256 throw new Error ( ERR_INVALID_KEYPAIR_TYPE ) ;
6357 }
64- return secp256k1 . derive ( keypair . publicKey , this . privateKey ) ;
58+ return toBuffer ( getDiscv5Crypto ( ) . secp256k1 . deriveSecret ( this . privateKey , keypair . publicKey ) ) ;
6559 }
6660} ;
0 commit comments