@libp2p/crypto 2.0.8-d8f5bc211 → 2.0.8-e2267d437
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 +3 -3
- package/dist/src/aes/cipher-mode.js.map +1 -1
- package/dist/src/aes/index.d.ts +1 -1
- package/dist/src/aes/index.d.ts.map +1 -1
- package/dist/src/aes/index.js +1 -1
- package/dist/src/aes/index.js.map +1 -1
- package/dist/src/ciphers/aes-gcm.browser.js.map +1 -1
- package/dist/src/ciphers/aes-gcm.js +3 -3
- package/dist/src/ciphers/aes-gcm.js.map +1 -1
- package/dist/src/keys/ecdh-browser.js +1 -1
- package/dist/src/keys/ecdh-browser.js.map +1 -1
- package/dist/src/keys/ecdh.js.map +1 -1
- package/dist/src/keys/ed25519-browser.d.ts +5 -4
- package/dist/src/keys/ed25519-browser.d.ts.map +1 -1
- package/dist/src/keys/ed25519-browser.js +6 -6
- package/dist/src/keys/ed25519-browser.js.map +1 -1
- package/dist/src/keys/ed25519-class.d.ts +3 -2
- package/dist/src/keys/ed25519-class.d.ts.map +1 -1
- package/dist/src/keys/ed25519-class.js +2 -2
- package/dist/src/keys/ed25519-class.js.map +1 -1
- package/dist/src/keys/ed25519.d.ts +5 -4
- package/dist/src/keys/ed25519.d.ts.map +1 -1
- package/dist/src/keys/ed25519.js +11 -19
- package/dist/src/keys/ed25519.js.map +1 -1
- package/dist/src/keys/index.js.map +1 -1
- package/dist/src/keys/key-stretcher.js.map +1 -1
- package/dist/src/keys/keys.js.map +1 -1
- package/dist/src/keys/rsa-browser.d.ts +5 -4
- package/dist/src/keys/rsa-browser.d.ts.map +1 -1
- package/dist/src/keys/rsa-browser.js +3 -3
- package/dist/src/keys/rsa-browser.js.map +1 -1
- package/dist/src/keys/rsa-class.d.ts +5 -4
- package/dist/src/keys/rsa-class.d.ts.map +1 -1
- package/dist/src/keys/rsa-class.js.map +1 -1
- package/dist/src/keys/rsa-utils.js.map +1 -1
- package/dist/src/keys/rsa.d.ts +5 -4
- package/dist/src/keys/rsa.d.ts.map +1 -1
- package/dist/src/keys/rsa.js +38 -12
- package/dist/src/keys/rsa.js.map +1 -1
- package/dist/src/keys/secp256k1-browser.d.ts +18 -0
- package/dist/src/keys/secp256k1-browser.d.ts.map +1 -0
- package/dist/src/keys/secp256k1-browser.js +66 -0
- package/dist/src/keys/secp256k1-browser.js.map +1 -0
- package/dist/src/keys/secp256k1-class.d.ts +3 -2
- package/dist/src/keys/secp256k1-class.d.ts.map +1 -1
- package/dist/src/keys/secp256k1-class.js.map +1 -1
- package/dist/src/keys/secp256k1.d.ts +3 -2
- package/dist/src/keys/secp256k1.d.ts.map +1 -1
- package/dist/src/keys/secp256k1.js +21 -3
- package/dist/src/keys/secp256k1.js.map +1 -1
- package/dist/src/pbkdf2.js.map +1 -1
- package/dist/src/random-bytes.js.map +1 -1
- package/dist/src/util.js.map +1 -1
- package/dist/src/webcrypto.js.map +1 -1
- package/package.json +4 -3
- package/src/aes/index.ts +1 -1
- package/src/ciphers/aes-gcm.ts +3 -3
- package/src/keys/ed25519-browser.ts +7 -6
- package/src/keys/ed25519-class.ts +5 -4
- package/src/keys/ed25519.ts +12 -20
- package/src/keys/rsa-browser.ts +9 -8
- package/src/keys/rsa-class.ts +5 -4
- package/src/keys/rsa.ts +43 -16
- package/src/keys/secp256k1-browser.ts +71 -0
- package/src/keys/secp256k1-class.ts +3 -2
- package/src/keys/secp256k1.ts +28 -5
package/src/keys/ed25519.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import crypto from 'crypto'
|
|
2
|
-
import {
|
|
2
|
+
import { concat as uint8arrayConcat } from 'uint8arrays/concat'
|
|
3
3
|
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
|
|
4
4
|
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
|
|
5
5
|
import type { Uint8ArrayKeyPair } from './interface.js'
|
|
6
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
7
|
|
|
7
|
-
const keypair =
|
|
8
|
+
const keypair = crypto.generateKeyPairSync
|
|
8
9
|
|
|
9
10
|
const PUBLIC_KEY_BYTE_LENGTH = 32
|
|
10
11
|
const PRIVATE_KEY_BYTE_LENGTH = 64 // private key is actually 32 bytes but for historical reasons we concat private and public keys
|
|
@@ -35,8 +36,8 @@ function derivePublicKey (privateKey: Uint8Array): Uint8Array {
|
|
|
35
36
|
return uint8arrayFromString(jwk.x, 'base64url')
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
export
|
|
39
|
-
const key =
|
|
39
|
+
export function generateKey (): Uint8ArrayKeyPair {
|
|
40
|
+
const key = keypair('ed25519', {
|
|
40
41
|
publicKeyEncoding: { type: 'spki', format: 'jwk' },
|
|
41
42
|
privateKeyEncoding: { type: 'pkcs8', format: 'jwk' }
|
|
42
43
|
})
|
|
@@ -47,7 +48,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
|
|
|
47
48
|
const publicKeyRaw = uint8arrayFromString(key.privateKey.x, 'base64url')
|
|
48
49
|
|
|
49
50
|
return {
|
|
50
|
-
privateKey:
|
|
51
|
+
privateKey: uint8arrayConcat([privateKeyRaw, publicKeyRaw], privateKeyRaw.byteLength + publicKeyRaw.byteLength),
|
|
51
52
|
publicKey: publicKeyRaw
|
|
52
53
|
}
|
|
53
54
|
}
|
|
@@ -55,7 +56,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
|
|
|
55
56
|
/**
|
|
56
57
|
* Generate keypair from a 32 byte uint8array
|
|
57
58
|
*/
|
|
58
|
-
export
|
|
59
|
+
export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
|
|
59
60
|
if (seed.length !== KEYS_BYTE_LENGTH) {
|
|
60
61
|
throw new TypeError('"seed" must be 32 bytes in length.')
|
|
61
62
|
} else if (!(seed instanceof Uint8Array)) {
|
|
@@ -66,12 +67,12 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
|
|
|
66
67
|
const publicKeyRaw = derivePublicKey(seed)
|
|
67
68
|
|
|
68
69
|
return {
|
|
69
|
-
privateKey:
|
|
70
|
+
privateKey: uint8arrayConcat([seed, publicKeyRaw], seed.byteLength + publicKeyRaw.byteLength),
|
|
70
71
|
publicKey: publicKeyRaw
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
export
|
|
75
|
+
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Buffer {
|
|
75
76
|
if (!(key instanceof Uint8Array)) {
|
|
76
77
|
throw new TypeError('"key" must be a node.js Buffer, or Uint8Array.')
|
|
77
78
|
}
|
|
@@ -99,10 +100,10 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Bu
|
|
|
99
100
|
}
|
|
100
101
|
})
|
|
101
102
|
|
|
102
|
-
return crypto.sign(null, msg, obj)
|
|
103
|
+
return crypto.sign(null, msg instanceof Uint8Array ? msg : msg.subarray(), obj)
|
|
103
104
|
}
|
|
104
105
|
|
|
105
|
-
export
|
|
106
|
+
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
|
|
106
107
|
if (key.byteLength !== PUBLIC_KEY_BYTE_LENGTH) {
|
|
107
108
|
throw new TypeError('"key" must be 32 bytes in length.')
|
|
108
109
|
} else if (!(key instanceof Uint8Array)) {
|
|
@@ -124,14 +125,5 @@ export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint
|
|
|
124
125
|
}
|
|
125
126
|
})
|
|
126
127
|
|
|
127
|
-
return crypto.verify(null, msg, obj, sig)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function concatKeys (privateKeyRaw: Uint8Array, publicKey: Uint8Array): Uint8Array {
|
|
131
|
-
const privateKey = new Uint8Array(PRIVATE_KEY_BYTE_LENGTH)
|
|
132
|
-
for (let i = 0; i < KEYS_BYTE_LENGTH; i++) {
|
|
133
|
-
privateKey[i] = privateKeyRaw[i]
|
|
134
|
-
privateKey[KEYS_BYTE_LENGTH + i] = publicKey[i]
|
|
135
|
-
}
|
|
136
|
-
return privateKey
|
|
128
|
+
return crypto.verify(null, msg instanceof Uint8Array ? msg : msg.subarray(), obj, sig)
|
|
137
129
|
}
|
package/src/keys/rsa-browser.ts
CHANGED
|
@@ -6,6 +6,7 @@ import webcrypto from '../webcrypto.js'
|
|
|
6
6
|
import { jwk2pub, jwk2priv } from './jwk2pem.js'
|
|
7
7
|
import * as utils from './rsa-utils.js'
|
|
8
8
|
import type { JWKKeyPair } from './interface.js'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export { utils }
|
|
11
12
|
|
|
@@ -60,7 +61,7 @@ export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair>
|
|
|
60
61
|
|
|
61
62
|
export { randomBytes as getRandomValues }
|
|
62
63
|
|
|
63
|
-
export async function hashAndSign (key: JsonWebKey, msg: Uint8Array): Promise<Uint8Array> {
|
|
64
|
+
export async function hashAndSign (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
64
65
|
const privateKey = await webcrypto.get().subtle.importKey(
|
|
65
66
|
'jwk',
|
|
66
67
|
key,
|
|
@@ -75,13 +76,13 @@ export async function hashAndSign (key: JsonWebKey, msg: Uint8Array): Promise<Ui
|
|
|
75
76
|
const sig = await webcrypto.get().subtle.sign(
|
|
76
77
|
{ name: 'RSASSA-PKCS1-v1_5' },
|
|
77
78
|
privateKey,
|
|
78
|
-
Uint8Array.
|
|
79
|
+
msg instanceof Uint8Array ? msg : msg.subarray()
|
|
79
80
|
)
|
|
80
81
|
|
|
81
82
|
return new Uint8Array(sig, 0, sig.byteLength)
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
|
|
85
|
+
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
85
86
|
const publicKey = await webcrypto.get().subtle.importKey(
|
|
86
87
|
'jwk',
|
|
87
88
|
key,
|
|
@@ -97,7 +98,7 @@ export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint
|
|
|
97
98
|
{ name: 'RSASSA-PKCS1-v1_5' },
|
|
98
99
|
publicKey,
|
|
99
100
|
sig,
|
|
100
|
-
msg
|
|
101
|
+
msg instanceof Uint8Array ? msg : msg.subarray()
|
|
101
102
|
)
|
|
102
103
|
}
|
|
103
104
|
|
|
@@ -141,18 +142,18 @@ Explanation:
|
|
|
141
142
|
|
|
142
143
|
*/
|
|
143
144
|
|
|
144
|
-
function convertKey (key: JsonWebKey, pub: boolean, msg: Uint8Array, handle: (msg: string, key: { encrypt(msg: string): string, decrypt(msg: string): string }) => string): Uint8Array {
|
|
145
|
+
function convertKey (key: JsonWebKey, pub: boolean, msg: Uint8Array | Uint8ArrayList, handle: (msg: string, key: { encrypt(msg: string): string, decrypt(msg: string): string }) => string): Uint8Array {
|
|
145
146
|
const fkey = pub ? jwk2pub(key) : jwk2priv(key)
|
|
146
|
-
const fmsg = uint8ArrayToString(Uint8Array.
|
|
147
|
+
const fmsg = uint8ArrayToString(msg instanceof Uint8Array ? msg : msg.subarray(), 'ascii')
|
|
147
148
|
const fomsg = handle(fmsg, fkey)
|
|
148
149
|
return uint8ArrayFromString(fomsg, 'ascii')
|
|
149
150
|
}
|
|
150
151
|
|
|
151
|
-
export function encrypt (key: JsonWebKey, msg: Uint8Array): Uint8Array {
|
|
152
|
+
export function encrypt (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
152
153
|
return convertKey(key, true, msg, (msg, key) => key.encrypt(msg))
|
|
153
154
|
}
|
|
154
155
|
|
|
155
|
-
export function decrypt (key: JsonWebKey, msg: Uint8Array): Uint8Array {
|
|
156
|
+
export function decrypt (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
156
157
|
return convertKey(key, false, msg, (msg, key) => key.decrypt(msg))
|
|
157
158
|
}
|
|
158
159
|
|
package/src/keys/rsa-class.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { exporter } from './exporter.js'
|
|
|
9
9
|
import * as pbm from './keys.js'
|
|
10
10
|
import * as crypto from './rsa.js'
|
|
11
11
|
import type { Multibase } from 'multiformats'
|
|
12
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
12
13
|
|
|
13
14
|
export const MAX_KEY_SIZE = 8192
|
|
14
15
|
|
|
@@ -19,7 +20,7 @@ export class RsaPublicKey {
|
|
|
19
20
|
this._key = key
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> {
|
|
23
|
+
async verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): Promise<boolean> {
|
|
23
24
|
return crypto.hashAndVerify(this._key, sig, data)
|
|
24
25
|
}
|
|
25
26
|
|
|
@@ -34,7 +35,7 @@ export class RsaPublicKey {
|
|
|
34
35
|
}).subarray()
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
encrypt (bytes: Uint8Array): Uint8Array {
|
|
38
|
+
encrypt (bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
38
39
|
return crypto.encrypt(this._key, bytes)
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -62,7 +63,7 @@ export class RsaPrivateKey {
|
|
|
62
63
|
return crypto.getRandomValues(16)
|
|
63
64
|
}
|
|
64
65
|
|
|
65
|
-
async sign (message: Uint8Array): Promise<Uint8Array> {
|
|
66
|
+
async sign (message: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
66
67
|
return crypto.hashAndSign(this._key, message)
|
|
67
68
|
}
|
|
68
69
|
|
|
@@ -74,7 +75,7 @@ export class RsaPrivateKey {
|
|
|
74
75
|
return new RsaPublicKey(this._publicKey)
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
decrypt (bytes: Uint8Array): Uint8Array {
|
|
78
|
+
decrypt (bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
78
79
|
return crypto.decrypt(this._key, bytes)
|
|
79
80
|
}
|
|
80
81
|
|
package/src/keys/rsa.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { CodeError } from '@libp2p/interface/errors'
|
|
|
4
4
|
import randomBytes from '../random-bytes.js'
|
|
5
5
|
import * as utils from './rsa-utils.js'
|
|
6
6
|
import type { JWKKeyPair } from './interface.js'
|
|
7
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
7
8
|
|
|
8
9
|
const keypair = promisify(crypto.generateKeyPair)
|
|
9
10
|
|
|
@@ -42,30 +43,56 @@ export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair>
|
|
|
42
43
|
|
|
43
44
|
export { randomBytes as getRandomValues }
|
|
44
45
|
|
|
45
|
-
export async function hashAndSign (key: JsonWebKey, msg: Uint8Array): Promise<Uint8Array> {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
.
|
|
46
|
+
export async function hashAndSign (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
47
|
+
const hash = crypto.createSign('RSA-SHA256')
|
|
48
|
+
|
|
49
|
+
if (msg instanceof Uint8Array) {
|
|
50
|
+
hash.update(msg)
|
|
51
|
+
} else {
|
|
52
|
+
for (const buf of msg) {
|
|
53
|
+
hash.update(buf)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
58
|
+
return hash.sign({ format: 'jwk', key })
|
|
50
59
|
}
|
|
51
60
|
|
|
52
|
-
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.
|
|
61
|
+
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
62
|
+
const hash = crypto.createVerify('RSA-SHA256')
|
|
63
|
+
|
|
64
|
+
if (msg instanceof Uint8Array) {
|
|
65
|
+
hash.update(msg)
|
|
66
|
+
} else {
|
|
67
|
+
for (const buf of msg) {
|
|
68
|
+
hash.update(buf)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
73
|
+
return hash.verify({ format: 'jwk', key }, sig)
|
|
57
74
|
}
|
|
58
75
|
|
|
59
76
|
const padding = crypto.constants.RSA_PKCS1_PADDING
|
|
60
77
|
|
|
61
|
-
export function encrypt (key: JsonWebKey, bytes: Uint8Array): Uint8Array {
|
|
62
|
-
|
|
63
|
-
|
|
78
|
+
export function encrypt (key: JsonWebKey, bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
79
|
+
if (bytes instanceof Uint8Array) {
|
|
80
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
81
|
+
return crypto.publicEncrypt({ format: 'jwk', key, padding }, bytes)
|
|
82
|
+
} else {
|
|
83
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
84
|
+
return crypto.publicEncrypt({ format: 'jwk', key, padding }, bytes.subarray())
|
|
85
|
+
}
|
|
64
86
|
}
|
|
65
87
|
|
|
66
|
-
export function decrypt (key: JsonWebKey, bytes: Uint8Array): Uint8Array {
|
|
67
|
-
|
|
68
|
-
|
|
88
|
+
export function decrypt (key: JsonWebKey, bytes: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
89
|
+
if (bytes instanceof Uint8Array) {
|
|
90
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
91
|
+
return crypto.privateDecrypt({ format: 'jwk', key, padding }, bytes)
|
|
92
|
+
} else {
|
|
93
|
+
// @ts-expect-error node types are missing jwk as a format
|
|
94
|
+
return crypto.privateDecrypt({ format: 'jwk', key, padding }, bytes.subarray())
|
|
95
|
+
}
|
|
69
96
|
}
|
|
70
97
|
|
|
71
98
|
export function keySize (jwk: JsonWebKey): number {
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
3
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
|
4
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
5
|
+
|
|
6
|
+
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
7
|
+
|
|
8
|
+
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
9
|
+
|
|
10
|
+
export function generateKey (): Uint8Array {
|
|
11
|
+
return secp.utils.randomPrivateKey()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hash and sign message with private key
|
|
16
|
+
*/
|
|
17
|
+
export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
18
|
+
const { digest } = await sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
19
|
+
try {
|
|
20
|
+
const signature = secp.sign(digest, key)
|
|
21
|
+
return signature.toDERRawBytes()
|
|
22
|
+
} catch (err) {
|
|
23
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Hash message and verify signature with public key
|
|
29
|
+
*/
|
|
30
|
+
export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
31
|
+
try {
|
|
32
|
+
const { digest } = await sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
|
|
33
|
+
return secp.verify(sig, digest, key)
|
|
34
|
+
} catch (err) {
|
|
35
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function compressPublicKey (key: Uint8Array): Uint8Array {
|
|
40
|
+
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(true)
|
|
41
|
+
return point
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function decompressPublicKey (key: Uint8Array): Uint8Array {
|
|
45
|
+
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(false)
|
|
46
|
+
return point
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function validatePrivateKey (key: Uint8Array): void {
|
|
50
|
+
try {
|
|
51
|
+
secp.getPublicKey(key, true)
|
|
52
|
+
} catch (err) {
|
|
53
|
+
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function validatePublicKey (key: Uint8Array): void {
|
|
58
|
+
try {
|
|
59
|
+
secp.ProjectivePoint.fromHex(key)
|
|
60
|
+
} catch (err) {
|
|
61
|
+
throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY')
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function computePublicKey (privateKey: Uint8Array): Uint8Array {
|
|
66
|
+
try {
|
|
67
|
+
return secp.getPublicKey(privateKey, true)
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -6,6 +6,7 @@ import { exporter } from './exporter.js'
|
|
|
6
6
|
import * as keysProtobuf from './keys.js'
|
|
7
7
|
import * as crypto from './secp256k1.js'
|
|
8
8
|
import type { Multibase } from 'multiformats'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export class Secp256k1PublicKey {
|
|
11
12
|
private readonly _key: Uint8Array
|
|
@@ -15,7 +16,7 @@ export class Secp256k1PublicKey {
|
|
|
15
16
|
this._key = key
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> {
|
|
19
|
+
async verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): Promise<boolean> {
|
|
19
20
|
return crypto.hashAndVerify(this._key, sig, data)
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -52,7 +53,7 @@ export class Secp256k1PrivateKey {
|
|
|
52
53
|
crypto.validatePublicKey(this._publicKey)
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
async sign (message: Uint8Array): Promise<Uint8Array> {
|
|
56
|
+
async sign (message: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
56
57
|
return crypto.hashAndSign(this._key, message)
|
|
57
58
|
}
|
|
58
59
|
|
package/src/keys/secp256k1.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import crypto from 'node:crypto'
|
|
1
2
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
3
|
import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
3
|
-
import {
|
|
4
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
4
5
|
|
|
5
6
|
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
6
7
|
|
|
@@ -13,8 +14,19 @@ export function generateKey (): Uint8Array {
|
|
|
13
14
|
/**
|
|
14
15
|
* Hash and sign message with private key
|
|
15
16
|
*/
|
|
16
|
-
export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> {
|
|
17
|
-
const
|
|
17
|
+
export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
18
|
+
const hash = crypto.createHash('sha256')
|
|
19
|
+
|
|
20
|
+
if (msg instanceof Uint8Array) {
|
|
21
|
+
hash.update(msg)
|
|
22
|
+
} else {
|
|
23
|
+
for (const buf of msg) {
|
|
24
|
+
hash.update(buf)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const digest = hash.digest()
|
|
29
|
+
|
|
18
30
|
try {
|
|
19
31
|
const signature = secp.sign(digest, key)
|
|
20
32
|
return signature.toDERRawBytes()
|
|
@@ -26,9 +38,20 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Ui
|
|
|
26
38
|
/**
|
|
27
39
|
* Hash message and verify signature with public key
|
|
28
40
|
*/
|
|
29
|
-
export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
|
|
41
|
+
export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
42
|
+
const hash = crypto.createHash('sha256')
|
|
43
|
+
|
|
44
|
+
if (msg instanceof Uint8Array) {
|
|
45
|
+
hash.update(msg)
|
|
46
|
+
} else {
|
|
47
|
+
for (const buf of msg) {
|
|
48
|
+
hash.update(buf)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const digest = hash.digest()
|
|
53
|
+
|
|
30
54
|
try {
|
|
31
|
-
const { digest } = await sha256.digest(msg)
|
|
32
55
|
return secp.verify(sig, digest, key)
|
|
33
56
|
} catch (err) {
|
|
34
57
|
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|