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