@libp2p/crypto 2.0.8-7877a50e0 → 2.0.8-8bb6d5333
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +9 -9
- package/dist/src/keys/ed25519-class.d.ts +3 -3
- package/dist/src/keys/ed25519-class.d.ts.map +1 -1
- package/dist/src/keys/ed25519-class.js +17 -6
- package/dist/src/keys/ed25519-class.js.map +1 -1
- package/dist/src/keys/rsa-class.d.ts +5 -5
- package/dist/src/keys/rsa-class.d.ts.map +1 -1
- package/dist/src/keys/rsa-class.js +15 -8
- package/dist/src/keys/rsa-class.js.map +1 -1
- package/dist/src/keys/secp256k1-browser.d.ts +2 -2
- package/dist/src/keys/secp256k1-browser.d.ts.map +1 -1
- package/dist/src/keys/secp256k1-browser.js +19 -7
- package/dist/src/keys/secp256k1-browser.js.map +1 -1
- package/dist/src/keys/secp256k1-class.d.ts +3 -3
- package/dist/src/keys/secp256k1-class.d.ts.map +1 -1
- package/dist/src/keys/secp256k1-class.js +17 -6
- package/dist/src/keys/secp256k1-class.js.map +1 -1
- package/dist/src/keys/secp256k1.d.ts +2 -2
- package/dist/src/keys/secp256k1.d.ts.map +1 -1
- package/dist/src/keys/secp256k1.js +2 -2
- package/dist/src/keys/secp256k1.js.map +1 -1
- package/dist/src/util.d.ts +1 -0
- package/dist/src/util.d.ts.map +1 -1
- package/dist/src/util.js +8 -0
- package/dist/src/util.js.map +1 -1
- package/package.json +2 -2
- package/src/keys/ed25519-class.ts +18 -6
- package/src/keys/rsa-class.ts +18 -9
- package/src/keys/secp256k1-browser.ts +23 -7
- package/src/keys/secp256k1-class.ts +18 -6
- package/src/keys/secp256k1.ts +2 -2
- package/src/util.ts +10 -0
package/src/keys/rsa-class.ts
CHANGED
|
@@ -5,6 +5,7 @@ import forge from 'node-forge/lib/forge.js'
|
|
|
5
5
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
6
6
|
import 'node-forge/lib/sha512.js'
|
|
7
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
|
+
import { isPromise } from '../util.js'
|
|
8
9
|
import { exporter } from './exporter.js'
|
|
9
10
|
import * as pbm from './keys.js'
|
|
10
11
|
import * as crypto from './rsa.js'
|
|
@@ -20,7 +21,7 @@ export class RsaPublicKey {
|
|
|
20
21
|
this._key = key
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean> {
|
|
24
25
|
return crypto.hashAndVerify(this._key, sig, data)
|
|
25
26
|
}
|
|
26
27
|
|
|
@@ -39,14 +40,18 @@ export class RsaPublicKey {
|
|
|
39
40
|
return crypto.encrypt(this._key, bytes)
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
equals (key: any): boolean {
|
|
43
|
+
equals (key: any): boolean | boolean {
|
|
43
44
|
return uint8ArrayEquals(this.bytes, key.bytes)
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
const
|
|
47
|
+
hash (): Uint8Array | Promise<Uint8Array> {
|
|
48
|
+
const p = sha256.digest(this.bytes)
|
|
49
|
+
|
|
50
|
+
if (isPromise(p)) {
|
|
51
|
+
return p.then(({ bytes }) => bytes)
|
|
52
|
+
}
|
|
48
53
|
|
|
49
|
-
return bytes
|
|
54
|
+
return p.bytes
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
|
|
@@ -63,7 +68,7 @@ export class RsaPrivateKey {
|
|
|
63
68
|
return crypto.getRandomValues(16)
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
|
|
71
|
+
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
67
72
|
return crypto.hashAndSign(this._key, message)
|
|
68
73
|
}
|
|
69
74
|
|
|
@@ -94,10 +99,14 @@ export class RsaPrivateKey {
|
|
|
94
99
|
return uint8ArrayEquals(this.bytes, key.bytes)
|
|
95
100
|
}
|
|
96
101
|
|
|
97
|
-
|
|
98
|
-
const
|
|
102
|
+
hash (): Uint8Array | Promise<Uint8Array> {
|
|
103
|
+
const p = sha256.digest(this.bytes)
|
|
104
|
+
|
|
105
|
+
if (isPromise(p)) {
|
|
106
|
+
return p.then(({ bytes }) => bytes)
|
|
107
|
+
}
|
|
99
108
|
|
|
100
|
-
return bytes
|
|
109
|
+
return p.bytes
|
|
101
110
|
}
|
|
102
111
|
|
|
103
112
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
3
3
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
4
|
+
import { isPromise } from '../util.js'
|
|
4
5
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
5
6
|
|
|
6
7
|
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
@@ -14,11 +15,18 @@ export function generateKey (): Uint8Array {
|
|
|
14
15
|
/**
|
|
15
16
|
* Hash and sign message with private key
|
|
16
17
|
*/
|
|
17
|
-
export
|
|
18
|
-
const
|
|
18
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
19
|
+
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
20
|
+
|
|
21
|
+
if (isPromise(p)) {
|
|
22
|
+
return p.then(({ digest }) => secp.sign(digest, key).toDERRawBytes())
|
|
23
|
+
.catch(err => {
|
|
24
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
19
28
|
try {
|
|
20
|
-
|
|
21
|
-
return signature.toDERRawBytes()
|
|
29
|
+
return secp.sign(p.digest, key).toDERRawBytes()
|
|
22
30
|
} catch (err) {
|
|
23
31
|
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
24
32
|
}
|
|
@@ -27,10 +35,18 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8Array
|
|
|
27
35
|
/**
|
|
28
36
|
* Hash message and verify signature with public key
|
|
29
37
|
*/
|
|
30
|
-
export
|
|
38
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean | Promise<boolean> {
|
|
39
|
+
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
40
|
+
|
|
41
|
+
if (isPromise(p)) {
|
|
42
|
+
return p.then(({ digest }) => secp.verify(sig, digest, key))
|
|
43
|
+
.catch(err => {
|
|
44
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
31
48
|
try {
|
|
32
|
-
|
|
33
|
-
return secp.verify(sig, digest, key)
|
|
49
|
+
return secp.verify(sig, p.digest, key)
|
|
34
50
|
} catch (err) {
|
|
35
51
|
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
36
52
|
}
|
|
@@ -2,6 +2,7 @@ import { CodeError } from '@libp2p/interface/errors'
|
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
3
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
4
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
5
|
+
import { isPromise } from '../util.js'
|
|
5
6
|
import { exporter } from './exporter.js'
|
|
6
7
|
import * as keysProtobuf from './keys.js'
|
|
7
8
|
import * as crypto from './secp256k1.js'
|
|
@@ -16,7 +17,7 @@ export class Secp256k1PublicKey {
|
|
|
16
17
|
this._key = key
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean {
|
|
20
21
|
return crypto.hashAndVerify(this._key, sig, data)
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -36,7 +37,14 @@ export class Secp256k1PublicKey {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
async hash (): Promise<Uint8Array> {
|
|
39
|
-
const
|
|
40
|
+
const p = sha256.digest(this.bytes)
|
|
41
|
+
let bytes: Uint8Array
|
|
42
|
+
|
|
43
|
+
if (isPromise(p)) {
|
|
44
|
+
({ bytes } = await p)
|
|
45
|
+
} else {
|
|
46
|
+
bytes = p.bytes
|
|
47
|
+
}
|
|
40
48
|
|
|
41
49
|
return bytes
|
|
42
50
|
}
|
|
@@ -53,7 +61,7 @@ export class Secp256k1PrivateKey {
|
|
|
53
61
|
crypto.validatePublicKey(this._publicKey)
|
|
54
62
|
}
|
|
55
63
|
|
|
56
|
-
|
|
64
|
+
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
57
65
|
return crypto.hashAndSign(this._key, message)
|
|
58
66
|
}
|
|
59
67
|
|
|
@@ -76,10 +84,14 @@ export class Secp256k1PrivateKey {
|
|
|
76
84
|
return uint8ArrayEquals(this.bytes, key.bytes)
|
|
77
85
|
}
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
const
|
|
87
|
+
hash (): Uint8Array | Promise<Uint8Array> {
|
|
88
|
+
const p = sha256.digest(this.bytes)
|
|
81
89
|
|
|
82
|
-
|
|
90
|
+
if (isPromise(p)) {
|
|
91
|
+
return p.then(({ bytes }) => bytes)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return p.bytes
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
/**
|
package/src/keys/secp256k1.ts
CHANGED
|
@@ -14,7 +14,7 @@ export function generateKey (): Uint8Array {
|
|
|
14
14
|
/**
|
|
15
15
|
* Hash and sign message with private key
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
17
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
18
18
|
const hash = crypto.createHash('sha256')
|
|
19
19
|
|
|
20
20
|
if (msg instanceof Uint8Array) {
|
|
@@ -38,7 +38,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8Array
|
|
|
38
38
|
/**
|
|
39
39
|
* Hash message and verify signature with public key
|
|
40
40
|
*/
|
|
41
|
-
export
|
|
41
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
|
|
42
42
|
const hash = crypto.createHash('sha256')
|
|
43
43
|
|
|
44
44
|
if (msg instanceof Uint8Array) {
|
package/src/util.ts
CHANGED
|
@@ -40,3 +40,13 @@ export function base64urlToBuffer (str: string, len?: number): Uint8Array {
|
|
|
40
40
|
|
|
41
41
|
return buf
|
|
42
42
|
}
|
|
43
|
+
|
|
44
|
+
export function isPromise <T = unknown> (thing: any): thing is Promise<T> {
|
|
45
|
+
if (thing == null) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return typeof thing.then === 'function' &&
|
|
50
|
+
typeof thing.catch === 'function' &&
|
|
51
|
+
typeof thing.finally === 'function'
|
|
52
|
+
}
|