13
13
14
14
> Support for secp256k1 keys in js-libp2p-crypto
15
15
16
- This repo contains a [ js-libp2p-crypto] ( https://github.com/libp2p/js-libp2p-crypto ) -compatible
17
- implementation of cryptographic signature generation and verification using the
18
- [ secp256k1 elliptic curve] ( https://en.bitcoin.it/wiki/Secp256k1 ) popularized by Bitcoin and other
16
+ This repo contains a [ js-libp2p-crypto] ( https://github.com/libp2p/js-libp2p-crypto ) -compatible
17
+ implementation of cryptographic signature generation and verification using the
18
+ [ secp256k1 elliptic curve] ( https://en.bitcoin.it/wiki/Secp256k1 ) popularized by Bitcoin and other
19
19
crypto currencies.
20
20
21
21
## Lead Captain
@@ -28,14 +28,14 @@ crypto currencies.
28
28
- [ Usage] ( #usage )
29
29
- [ Example] ( #example )
30
30
- [ API] ( #api )
31
- - [ ` generateKeyPair([bits,] callback ) ` ] ( #generatekeypairbits-callback )
31
+ - [ ` generateKeyPair([bits] ) ` ] ( #generatekeypairbits )
32
32
- [ ` unmarshalSecp256k1PublicKey(bytes) ` ] ( #unmarshalsecp256k1publickeybytes )
33
- - [ ` unmarshalSecp256k1PrivateKey(bytes, callback ) ` ] ( #unmarshalsecp256k1privatekeybytes-callback )
33
+ - [ ` unmarshalSecp256k1PrivateKey(bytes) ` ] ( #unmarshalsecp256k1privatekeybytes )
34
34
- [ ` Secp256k1PublicKey ` ] ( #secp256k1publickey )
35
- - [ ` .verify(data, sig, callback ) ` ] ( #verifydata-sig-callback )
35
+ - [ ` .verify(data, sig) ` ] ( #verifydata-sig )
36
36
- [ ` Secp256k1PrivateKey ` ] ( #secp256k1privatekey )
37
37
- [ ` .public ` ] ( #public )
38
- - [ ` .sign(data, callback ) ` ] ( #signdata-callback )
38
+ - [ ` .sign(data) ` ] ( #signdata )
39
39
- [ Contribute] ( #contribute )
40
40
- [ License] ( #license )
41
41
@@ -57,63 +57,65 @@ instances of the `Secp256k1PublicKey` or `Secp256k1PrivateKey` classes provided
57
57
58
58
``` js
59
59
const crypto = require (' libp2p-crypto' )
60
-
61
60
const msg = Buffer .from (' Hello World' )
62
61
63
- crypto .generateKeyPair (' secp256k1' , 256 , (err , key ) => {
64
- // assuming no error, key will be an instance of Secp256k1PrivateKey
65
- // the public key is available as key.public
66
- key .sign (msg, (err , sig ) => {
67
- key .public .verify (msg, sig, (err , valid ) => {
68
- assert (valid, ' Something went horribly wrong' )
69
- })
70
- })
71
- })
62
+ const key = await crypto .generateKeyPair (' secp256k1' , 256 )
63
+ // assuming no error, key will be an instance of Secp256k1PrivateKey
64
+ // the public key is available as key.public
65
+ const sig = await key .sign (msg)
66
+
67
+ const valid = await key .public .verify (msg, sig)
68
+ assert (valid, ' Something went horribly wrong' )
72
69
```
73
70
74
71
## API
75
72
76
73
The functions below are the public API of this module.
77
- For usage within libp2p-crypto, see the [ libp2p-crypto API documentation] ( https://github.com/libp2p/js-libp2p-crypto#api ) .
74
+ For usage within ` libp2p-crypto ` , see the [ ` libp2p-crypto ` API documentation] ( https://github.com/libp2p/js-libp2p-crypto#api ) .
78
75
79
- ### ` generateKeyPair([bits, ] callback ) `
76
+ ### ` generateKeyPair([bits] ) `
80
77
- ` bits: Number ` - Optional, included for compatibility with js-libp2p-crypto. Ignored if present; private keys will always be 256 bits.
81
- - ` callback: Function `
78
+
79
+ Returns ` Promise<Secp256k1PrivateKey> `
82
80
83
81
### ` unmarshalSecp256k1PublicKey(bytes) `
84
82
- ` bytes: Buffer `
85
83
86
84
Converts a serialized secp256k1 public key into an instance of ` Secp256k1PublicKey ` and returns it
87
85
88
- ### ` unmarshalSecp256k1PrivateKey(bytes, callback ) `
86
+ ### ` unmarshalSecp256k1PrivateKey(bytes) `
89
87
- ` bytes: Buffer `
90
- - ` callback: Function `
91
88
92
- Converts a serialized secp256k1 private key into an instance of ` Secp256k1PrivateKey ` , passing it to ` callback ` on success
89
+ Returns ` Promise<Secp256k1PrivateKey> `
90
+
91
+ Converts a serialized secp256k1 private key into an instance of ` Secp256k1PrivateKey ` .
93
92
94
93
### ` Secp256k1PublicKey `
95
94
96
- #### ` .verify(data, sig, callback ) `
95
+ #### ` .verify(data, sig) `
97
96
- ` data: Buffer `
98
97
- ` sig: Buffer `
99
- - ` callback: Function `
100
98
101
- Calculates the SHA-256 hash of ` data ` , and verifies the DER-encoded signature in ` sig ` , passing the result to ` callback `
99
+ Returns ` Promise<Boolean> `
100
+
101
+ Calculates the SHA-256 hash of ` data ` , and verifies the DER-encoded signature in ` sig ` .
102
102
103
103
### ` Secp256k1PrivateKey `
104
104
105
105
#### ` .public `
106
106
107
107
Accessor for the ` Secp256k1PublicKey ` associated with this private key.
108
108
109
- #### ` .sign(data, callback ) `
109
+ #### ` .sign(data) `
110
110
- ` data: Buffer `
111
111
112
- Calculates the SHA-256 hash of ` data ` and signs it, passing the DER-encoded signature to ` callback `
112
+ Returns ` Promise<Buffer> `
113
+
114
+ Calculates the SHA-256 hash of ` data ` and signs it, resolves with the DER-encoded signature.
113
115
114
116
## Contribute
115
117
116
- Feel free to join in. All welcome. Open an [ issue] ( https://github.com/libp2p/js-libp2p-crypto/issues ) !
118
+ Feel free to join in. All welcome. Open an [ issue] ( https://github.com/libp2p/js-libp2p-crypto-secp256k1 /issues ) !
117
119
118
120
This repository falls under the IPFS [ Code of Conduct] ( https://github.com/ipfs/community/blob/master/code-of-conduct.md ) .
119
121
0 commit comments