@waku/enr 0.0.5 → 0.0.7
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/CHANGELOG.md +44 -1
- package/bundle/index.js +5367 -5359
- package/dist/.tsbuildinfo +1 -0
- package/dist/creator.d.ts +7 -0
- package/dist/creator.js +26 -0
- package/dist/creator.js.map +1 -0
- package/dist/crypto.d.ts +0 -7
- package/dist/crypto.js +1 -7
- package/dist/crypto.js.map +1 -1
- package/dist/decoder.d.ts +5 -0
- package/dist/decoder.js +62 -0
- package/dist/decoder.js.map +1 -0
- package/dist/encoder.d.ts +7 -0
- package/dist/encoder.js +38 -0
- package/dist/encoder.js.map +1 -0
- package/dist/enr.d.ts +19 -68
- package/dist/enr.js +43 -336
- package/dist/enr.js.map +1 -1
- package/dist/get_multiaddr.d.ts +3 -0
- package/dist/get_multiaddr.js +31 -0
- package/dist/get_multiaddr.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/peer_id.d.ts +4 -0
- package/dist/peer_id.js +25 -0
- package/dist/peer_id.js.map +1 -0
- package/dist/raw_enr.d.ts +56 -0
- package/dist/raw_enr.js +142 -0
- package/dist/raw_enr.js.map +1 -0
- package/dist/v4.js +1 -1
- package/dist/v4.js.map +1 -1
- package/package.json +39 -28
- package/src/creator.ts +36 -0
- package/src/crypto.ts +1 -9
- package/src/decoder.ts +84 -0
- package/src/encoder.ts +50 -0
- package/src/enr.ts +50 -411
- package/src/get_multiaddr.ts +47 -0
- package/src/index.ts +3 -1
- package/src/peer_id.ts +34 -0
- package/src/raw_enr.ts +204 -0
- package/src/v4.ts +1 -2
- package/dist/keypair/index.d.ts +0 -8
- package/dist/keypair/index.js +0 -53
- package/dist/keypair/index.js.map +0 -1
- package/dist/keypair/secp256k1.d.ts +0 -13
- package/dist/keypair/secp256k1.js +0 -57
- package/dist/keypair/secp256k1.js.map +0 -1
- package/dist/keypair/types.d.ts +0 -13
- package/dist/keypair/types.js +0 -7
- package/dist/keypair/types.js.map +0 -1
- package/src/keypair/index.ts +0 -76
- package/src/keypair/secp256k1.ts +0 -69
- package/src/keypair/types.ts +0 -14
package/src/raw_enr.ts
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
import type { Multiaddr } from "@multiformats/multiaddr";
|
2
|
+
import {
|
3
|
+
convertToBytes,
|
4
|
+
convertToString,
|
5
|
+
} from "@multiformats/multiaddr/convert";
|
6
|
+
import type { ENRKey, ENRValue, SequenceNumber, Waku2 } from "@waku/interfaces";
|
7
|
+
import { bytesToUtf8 } from "@waku/utils/bytes";
|
8
|
+
|
9
|
+
import { ERR_INVALID_ID } from "./constants.js";
|
10
|
+
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
|
11
|
+
import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
|
12
|
+
|
13
|
+
export class RawEnr extends Map<ENRKey, ENRValue> {
|
14
|
+
public seq: SequenceNumber;
|
15
|
+
public signature?: Uint8Array;
|
16
|
+
|
17
|
+
protected constructor(
|
18
|
+
kvs: Record<ENRKey, ENRValue> = {},
|
19
|
+
seq: SequenceNumber = BigInt(1),
|
20
|
+
signature?: Uint8Array
|
21
|
+
) {
|
22
|
+
super(Object.entries(kvs));
|
23
|
+
this.seq = seq;
|
24
|
+
this.signature = signature;
|
25
|
+
}
|
26
|
+
|
27
|
+
set(k: ENRKey, v: ENRValue): this {
|
28
|
+
this.signature = undefined;
|
29
|
+
this.seq++;
|
30
|
+
return super.set(k, v);
|
31
|
+
}
|
32
|
+
|
33
|
+
get id(): string {
|
34
|
+
const id = this.get("id");
|
35
|
+
if (!id) throw new Error("id not found.");
|
36
|
+
return bytesToUtf8(id);
|
37
|
+
}
|
38
|
+
|
39
|
+
get publicKey(): Uint8Array | undefined {
|
40
|
+
switch (this.id) {
|
41
|
+
case "v4":
|
42
|
+
return this.get("secp256k1");
|
43
|
+
default:
|
44
|
+
throw new Error(ERR_INVALID_ID);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
get ip(): string | undefined {
|
49
|
+
return getStringValue(this, "ip", "ip4");
|
50
|
+
}
|
51
|
+
|
52
|
+
set ip(ip: string | undefined) {
|
53
|
+
setStringValue(this, "ip", "ip4", ip);
|
54
|
+
}
|
55
|
+
|
56
|
+
get tcp(): number | undefined {
|
57
|
+
return getNumberAsStringValue(this, "tcp", "tcp");
|
58
|
+
}
|
59
|
+
|
60
|
+
set tcp(port: number | undefined) {
|
61
|
+
setNumberAsStringValue(this, "tcp", "tcp", port);
|
62
|
+
}
|
63
|
+
|
64
|
+
get udp(): number | undefined {
|
65
|
+
return getNumberAsStringValue(this, "udp", "udp");
|
66
|
+
}
|
67
|
+
|
68
|
+
set udp(port: number | undefined) {
|
69
|
+
setNumberAsStringValue(this, "udp", "udp", port);
|
70
|
+
}
|
71
|
+
|
72
|
+
get ip6(): string | undefined {
|
73
|
+
return getStringValue(this, "ip6", "ip6");
|
74
|
+
}
|
75
|
+
|
76
|
+
set ip6(ip: string | undefined) {
|
77
|
+
setStringValue(this, "ip6", "ip6", ip);
|
78
|
+
}
|
79
|
+
|
80
|
+
get tcp6(): number | undefined {
|
81
|
+
return getNumberAsStringValue(this, "tcp6", "tcp");
|
82
|
+
}
|
83
|
+
|
84
|
+
set tcp6(port: number | undefined) {
|
85
|
+
setNumberAsStringValue(this, "tcp6", "tcp", port);
|
86
|
+
}
|
87
|
+
|
88
|
+
get udp6(): number | undefined {
|
89
|
+
return getNumberAsStringValue(this, "udp6", "udp");
|
90
|
+
}
|
91
|
+
|
92
|
+
set udp6(port: number | undefined) {
|
93
|
+
setNumberAsStringValue(this, "udp6", "udp", port);
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Get the `multiaddrs` field from ENR.
|
98
|
+
*
|
99
|
+
* This field is used to store multiaddresses that cannot be stored with the current ENR pre-defined keys.
|
100
|
+
* These can be a multiaddresses that include encapsulation (e.g. wss) or do not use `ip4` nor `ip6` for the host
|
101
|
+
* address (e.g. `dns4`, `dnsaddr`, etc)..
|
102
|
+
*
|
103
|
+
* If the peer information only contains information that can be represented with the ENR pre-defined keys
|
104
|
+
* (ip, tcp, etc) then the usage of { @link ENR.getLocationMultiaddr } should be preferred.
|
105
|
+
*
|
106
|
+
* The multiaddresses stored in this field are expected to be location multiaddresses, ie, peer id less.
|
107
|
+
*/
|
108
|
+
get multiaddrs(): Multiaddr[] | undefined {
|
109
|
+
const raw = this.get("multiaddrs");
|
110
|
+
|
111
|
+
if (raw) return decodeMultiaddrs(raw);
|
112
|
+
|
113
|
+
return;
|
114
|
+
}
|
115
|
+
|
116
|
+
/**
|
117
|
+
* Set the `multiaddrs` field on the ENR.
|
118
|
+
*
|
119
|
+
* This field is used to store multiaddresses that cannot be stored with the current ENR pre-defined keys.
|
120
|
+
* These can be a multiaddresses that include encapsulation (e.g. wss) or do not use `ip4` nor `ip6` for the host
|
121
|
+
* address (e.g. `dns4`, `dnsaddr`, etc)..
|
122
|
+
*
|
123
|
+
* If the peer information only contains information that can be represented with the ENR pre-defined keys
|
124
|
+
* (ip, tcp, etc) then the usage of { @link ENR.setLocationMultiaddr } should be preferred.
|
125
|
+
* The multiaddresses stored in this field must be location multiaddresses,
|
126
|
+
* ie, without a peer id.
|
127
|
+
*/
|
128
|
+
set multiaddrs(multiaddrs: Multiaddr[] | undefined) {
|
129
|
+
deleteUndefined(this, "multiaddrs", multiaddrs, encodeMultiaddrs);
|
130
|
+
}
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Get the `waku2` field from ENR.
|
134
|
+
*/
|
135
|
+
get waku2(): Waku2 | undefined {
|
136
|
+
const raw = this.get("waku2");
|
137
|
+
if (raw) return decodeWaku2(raw[0]);
|
138
|
+
|
139
|
+
return;
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* Set the `waku2` field on the ENR.
|
144
|
+
*/
|
145
|
+
set waku2(waku2: Waku2 | undefined) {
|
146
|
+
deleteUndefined(
|
147
|
+
this,
|
148
|
+
"waku2",
|
149
|
+
waku2,
|
150
|
+
(w) => new Uint8Array([encodeWaku2(w)])
|
151
|
+
);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
function getStringValue(
|
156
|
+
map: Map<ENRKey, ENRValue>,
|
157
|
+
key: ENRKey,
|
158
|
+
proto: string
|
159
|
+
): string | undefined {
|
160
|
+
const raw = map.get(key);
|
161
|
+
if (!raw) return;
|
162
|
+
return convertToString(proto, raw);
|
163
|
+
}
|
164
|
+
|
165
|
+
function getNumberAsStringValue(
|
166
|
+
map: Map<ENRKey, ENRValue>,
|
167
|
+
key: ENRKey,
|
168
|
+
proto: string
|
169
|
+
): number | undefined {
|
170
|
+
const raw = map.get(key);
|
171
|
+
if (!raw) return;
|
172
|
+
return Number(convertToString(proto, raw));
|
173
|
+
}
|
174
|
+
|
175
|
+
function setStringValue(
|
176
|
+
map: Map<ENRKey, ENRValue>,
|
177
|
+
key: ENRKey,
|
178
|
+
proto: string,
|
179
|
+
value: string | undefined
|
180
|
+
): void {
|
181
|
+
deleteUndefined(map, key, value, convertToBytes.bind({}, proto));
|
182
|
+
}
|
183
|
+
|
184
|
+
function setNumberAsStringValue(
|
185
|
+
map: Map<ENRKey, ENRValue>,
|
186
|
+
key: ENRKey,
|
187
|
+
proto: string,
|
188
|
+
value: number | undefined
|
189
|
+
): void {
|
190
|
+
setStringValue(map, key, proto, value?.toString(10));
|
191
|
+
}
|
192
|
+
|
193
|
+
function deleteUndefined<K, V, W>(
|
194
|
+
map: Map<K, W>,
|
195
|
+
key: K,
|
196
|
+
value: V | undefined,
|
197
|
+
transform: (v: V) => W
|
198
|
+
): void {
|
199
|
+
if (value !== undefined) {
|
200
|
+
map.set(key, transform(value));
|
201
|
+
} else {
|
202
|
+
map.delete(key);
|
203
|
+
}
|
204
|
+
}
|
package/src/v4.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
import * as secp from "@noble/secp256k1";
|
2
|
-
import { bytesToHex } from "@waku/byte-utils";
|
3
2
|
import type { NodeId } from "@waku/interfaces";
|
3
|
+
import { bytesToHex } from "@waku/utils/bytes";
|
4
4
|
|
5
5
|
import { keccak256 } from "./crypto.js";
|
6
|
-
|
7
6
|
export async function sign(
|
8
7
|
privKey: Uint8Array,
|
9
8
|
msg: Uint8Array
|
package/dist/keypair/index.d.ts
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
import type { PeerId } from "@libp2p/interface-peer-id";
|
2
|
-
import { IKeypair, KeypairType } from "./types.js";
|
3
|
-
export declare const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented";
|
4
|
-
export * from "./types.js";
|
5
|
-
export * from "./secp256k1.js";
|
6
|
-
export declare function createKeypair(type: KeypairType, privateKey?: Uint8Array, publicKey?: Uint8Array): IKeypair;
|
7
|
-
export declare function createPeerIdFromKeypair(keypair: IKeypair): Promise<PeerId>;
|
8
|
-
export declare function createKeypairFromPeerId(peerId: PeerId): Promise<IKeypair>;
|
package/dist/keypair/index.js
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
import { unmarshalPrivateKey, unmarshalPublicKey } from "@libp2p/crypto/keys";
|
2
|
-
import { supportedKeys } from "@libp2p/crypto/keys";
|
3
|
-
import { peerIdFromKeys } from "@libp2p/peer-id";
|
4
|
-
import { Secp256k1Keypair } from "./secp256k1.js";
|
5
|
-
import { KeypairType } from "./types.js";
|
6
|
-
export const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented";
|
7
|
-
export * from "./types.js";
|
8
|
-
export * from "./secp256k1.js";
|
9
|
-
export function createKeypair(type, privateKey, publicKey) {
|
10
|
-
switch (type) {
|
11
|
-
case KeypairType.secp256k1:
|
12
|
-
return new Secp256k1Keypair(privateKey, publicKey);
|
13
|
-
default:
|
14
|
-
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
|
15
|
-
}
|
16
|
-
}
|
17
|
-
export async function createPeerIdFromKeypair(keypair) {
|
18
|
-
switch (keypair.type) {
|
19
|
-
case KeypairType.secp256k1: {
|
20
|
-
const publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey(keypair.publicKey);
|
21
|
-
const privateKey = keypair.hasPrivateKey()
|
22
|
-
? new supportedKeys.secp256k1.Secp256k1PrivateKey(keypair.privateKey)
|
23
|
-
: undefined;
|
24
|
-
return peerIdFromKeys(publicKey.bytes, privateKey?.bytes);
|
25
|
-
}
|
26
|
-
default:
|
27
|
-
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
|
28
|
-
}
|
29
|
-
}
|
30
|
-
export async function createKeypairFromPeerId(peerId) {
|
31
|
-
let keypairType;
|
32
|
-
switch (peerId.type) {
|
33
|
-
case "RSA":
|
34
|
-
keypairType = KeypairType.rsa;
|
35
|
-
break;
|
36
|
-
case "Ed25519":
|
37
|
-
keypairType = KeypairType.ed25519;
|
38
|
-
break;
|
39
|
-
case "secp256k1":
|
40
|
-
keypairType = KeypairType.secp256k1;
|
41
|
-
break;
|
42
|
-
default:
|
43
|
-
throw new Error("Unsupported peer id type");
|
44
|
-
}
|
45
|
-
const publicKey = peerId.publicKey
|
46
|
-
? unmarshalPublicKey(peerId.publicKey)
|
47
|
-
: undefined;
|
48
|
-
const privateKey = peerId.privateKey
|
49
|
-
? await unmarshalPrivateKey(peerId.privateKey)
|
50
|
-
: undefined;
|
51
|
-
return createKeypair(keypairType, privateKey?.marshal(), publicKey?.marshal());
|
52
|
-
}
|
53
|
-
//# sourceMappingURL=index.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/keypair/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,MAAM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ClC,MAAM;QACR,KAAK,WAAW;YACd,WAAW,GAAG,WAAW,CAAC,SAAS,CAAC;YACpC,MAAM;QACR;YACE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;KAC/C;IAED,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS;QAChC,CAAC,CAAC,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC;QACtC,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU;QAClC,CAAC,CAAC,MAAM,mBAAmB,CAAC,MAAM,CAAC,UAAU,CAAC;QAC9C,CAAC,CAAC,SAAS,CAAC;IAEd,OAAO,aAAa,CAClB,WAAW,EACX,UAAU,EAAE,OAAO,EAAE,EACrB,SAAS,EAAE,OAAO,EAAE,CACrB,CAAC;AACJ,CAAC"}
|
@@ -1,13 +0,0 @@
|
|
1
|
-
import { IKeypair, KeypairType } from "./types.js";
|
2
|
-
export declare class Secp256k1Keypair implements IKeypair {
|
3
|
-
readonly type: KeypairType;
|
4
|
-
_privateKey?: Uint8Array;
|
5
|
-
readonly _publicKey?: Uint8Array;
|
6
|
-
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array);
|
7
|
-
static generate(): Promise<Secp256k1Keypair>;
|
8
|
-
privateKeyVerify(key?: Uint8Array | undefined): boolean;
|
9
|
-
publicKeyVerify(key?: Uint8Array | undefined): boolean;
|
10
|
-
get privateKey(): Uint8Array;
|
11
|
-
get publicKey(): Uint8Array;
|
12
|
-
hasPrivateKey(): boolean;
|
13
|
-
}
|
@@ -1,57 +0,0 @@
|
|
1
|
-
import * as secp from "@noble/secp256k1";
|
2
|
-
import { compressPublicKey, randomBytes } from "../crypto.js";
|
3
|
-
import { KeypairType } from "./types.js";
|
4
|
-
export class Secp256k1Keypair {
|
5
|
-
constructor(privateKey, publicKey) {
|
6
|
-
let pub = publicKey;
|
7
|
-
if (pub) {
|
8
|
-
pub = compressPublicKey(pub);
|
9
|
-
}
|
10
|
-
if ((this._privateKey = privateKey) && !this.privateKeyVerify()) {
|
11
|
-
throw new Error("Invalid private key");
|
12
|
-
}
|
13
|
-
if ((this._publicKey = pub) && !this.publicKeyVerify()) {
|
14
|
-
throw new Error("Invalid public key");
|
15
|
-
}
|
16
|
-
this.type = KeypairType.secp256k1;
|
17
|
-
}
|
18
|
-
static async generate() {
|
19
|
-
const privateKey = randomBytes(32);
|
20
|
-
const publicKey = secp.getPublicKey(privateKey);
|
21
|
-
return new Secp256k1Keypair(privateKey, publicKey);
|
22
|
-
}
|
23
|
-
privateKeyVerify(key = this._privateKey) {
|
24
|
-
if (key) {
|
25
|
-
return secp.utils.isValidPrivateKey(key);
|
26
|
-
}
|
27
|
-
return true;
|
28
|
-
}
|
29
|
-
publicKeyVerify(key = this._publicKey) {
|
30
|
-
if (key) {
|
31
|
-
try {
|
32
|
-
secp.Point.fromHex(key);
|
33
|
-
return true;
|
34
|
-
}
|
35
|
-
catch {
|
36
|
-
return false;
|
37
|
-
}
|
38
|
-
}
|
39
|
-
return true;
|
40
|
-
}
|
41
|
-
get privateKey() {
|
42
|
-
if (!this._privateKey) {
|
43
|
-
throw new Error();
|
44
|
-
}
|
45
|
-
return this._privateKey;
|
46
|
-
}
|
47
|
-
get publicKey() {
|
48
|
-
if (!this._publicKey) {
|
49
|
-
throw new Error();
|
50
|
-
}
|
51
|
-
return this._publicKey;
|
52
|
-
}
|
53
|
-
hasPrivateKey() {
|
54
|
-
return !!this._privateKey;
|
55
|
-
}
|
56
|
-
}
|
57
|
-
//# sourceMappingURL=secp256k1.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"secp256k1.js","sourceRoot":"","sources":["../../src/keypair/secp256k1.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE9D,OAAO,EAAY,WAAW,EAAE,MAAM,YAAY,CAAC;A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Z,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACrB,MAAM,IAAI,KAAK,EAAE,CAAC;SACnB;QACD,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED,IAAI,SAAS;QACX,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,IAAI,KAAK,EAAE,CAAC;SACnB;QACD,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,aAAa;QACX,OAAO,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;CACF"}
|
package/dist/keypair/types.d.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
export declare enum KeypairType {
|
2
|
-
rsa = 0,
|
3
|
-
ed25519 = 1,
|
4
|
-
secp256k1 = 2
|
5
|
-
}
|
6
|
-
export interface IKeypair {
|
7
|
-
type: KeypairType;
|
8
|
-
privateKey: Uint8Array;
|
9
|
-
publicKey: Uint8Array;
|
10
|
-
privateKeyVerify(): boolean;
|
11
|
-
publicKeyVerify(): boolean;
|
12
|
-
hasPrivateKey(): boolean;
|
13
|
-
}
|
package/dist/keypair/types.js
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
export var KeypairType;
|
2
|
-
(function (KeypairType) {
|
3
|
-
KeypairType[KeypairType["rsa"] = 0] = "rsa";
|
4
|
-
KeypairType[KeypairType["ed25519"] = 1] = "ed25519";
|
5
|
-
KeypairType[KeypairType["secp256k1"] = 2] = "secp256k1";
|
6
|
-
})(KeypairType || (KeypairType = {}));
|
7
|
-
//# sourceMappingURL=types.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/keypair/types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,WAIX;AAJD,WAAY,WAAW;IACrB,2CAAO,CAAA;IACP,mDAAW,CAAA;IACX,uDAAa,CAAA;AACf,CAAC,EAJW,WAAW,KAAX,WAAW,QAItB"}
|
package/src/keypair/index.ts
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
import { unmarshalPrivateKey, unmarshalPublicKey } from "@libp2p/crypto/keys";
|
2
|
-
import { supportedKeys } from "@libp2p/crypto/keys";
|
3
|
-
import type { PeerId } from "@libp2p/interface-peer-id";
|
4
|
-
import { peerIdFromKeys } from "@libp2p/peer-id";
|
5
|
-
|
6
|
-
import { Secp256k1Keypair } from "./secp256k1.js";
|
7
|
-
import { IKeypair, KeypairType } from "./types.js";
|
8
|
-
|
9
|
-
export const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented";
|
10
|
-
export * from "./types.js";
|
11
|
-
export * from "./secp256k1.js";
|
12
|
-
|
13
|
-
export function createKeypair(
|
14
|
-
type: KeypairType,
|
15
|
-
privateKey?: Uint8Array,
|
16
|
-
publicKey?: Uint8Array
|
17
|
-
): IKeypair {
|
18
|
-
switch (type) {
|
19
|
-
case KeypairType.secp256k1:
|
20
|
-
return new Secp256k1Keypair(privateKey, publicKey);
|
21
|
-
default:
|
22
|
-
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
|
23
|
-
}
|
24
|
-
}
|
25
|
-
|
26
|
-
export async function createPeerIdFromKeypair(
|
27
|
-
keypair: IKeypair
|
28
|
-
): Promise<PeerId> {
|
29
|
-
switch (keypair.type) {
|
30
|
-
case KeypairType.secp256k1: {
|
31
|
-
const publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey(
|
32
|
-
keypair.publicKey
|
33
|
-
);
|
34
|
-
|
35
|
-
const privateKey = keypair.hasPrivateKey()
|
36
|
-
? new supportedKeys.secp256k1.Secp256k1PrivateKey(keypair.privateKey)
|
37
|
-
: undefined;
|
38
|
-
|
39
|
-
return peerIdFromKeys(publicKey.bytes, privateKey?.bytes);
|
40
|
-
}
|
41
|
-
default:
|
42
|
-
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
export async function createKeypairFromPeerId(
|
47
|
-
peerId: PeerId
|
48
|
-
): Promise<IKeypair> {
|
49
|
-
let keypairType;
|
50
|
-
switch (peerId.type) {
|
51
|
-
case "RSA":
|
52
|
-
keypairType = KeypairType.rsa;
|
53
|
-
break;
|
54
|
-
case "Ed25519":
|
55
|
-
keypairType = KeypairType.ed25519;
|
56
|
-
break;
|
57
|
-
case "secp256k1":
|
58
|
-
keypairType = KeypairType.secp256k1;
|
59
|
-
break;
|
60
|
-
default:
|
61
|
-
throw new Error("Unsupported peer id type");
|
62
|
-
}
|
63
|
-
|
64
|
-
const publicKey = peerId.publicKey
|
65
|
-
? unmarshalPublicKey(peerId.publicKey)
|
66
|
-
: undefined;
|
67
|
-
const privateKey = peerId.privateKey
|
68
|
-
? await unmarshalPrivateKey(peerId.privateKey)
|
69
|
-
: undefined;
|
70
|
-
|
71
|
-
return createKeypair(
|
72
|
-
keypairType,
|
73
|
-
privateKey?.marshal(),
|
74
|
-
publicKey?.marshal()
|
75
|
-
);
|
76
|
-
}
|
package/src/keypair/secp256k1.ts
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
import * as secp from "@noble/secp256k1";
|
2
|
-
|
3
|
-
import { compressPublicKey, randomBytes } from "../crypto.js";
|
4
|
-
|
5
|
-
import { IKeypair, KeypairType } from "./types.js";
|
6
|
-
|
7
|
-
export class Secp256k1Keypair implements IKeypair {
|
8
|
-
readonly type: KeypairType;
|
9
|
-
_privateKey?: Uint8Array;
|
10
|
-
readonly _publicKey?: Uint8Array;
|
11
|
-
|
12
|
-
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
|
13
|
-
let pub = publicKey;
|
14
|
-
if (pub) {
|
15
|
-
pub = compressPublicKey(pub);
|
16
|
-
}
|
17
|
-
if ((this._privateKey = privateKey) && !this.privateKeyVerify()) {
|
18
|
-
throw new Error("Invalid private key");
|
19
|
-
}
|
20
|
-
if ((this._publicKey = pub) && !this.publicKeyVerify()) {
|
21
|
-
throw new Error("Invalid public key");
|
22
|
-
}
|
23
|
-
|
24
|
-
this.type = KeypairType.secp256k1;
|
25
|
-
}
|
26
|
-
|
27
|
-
static async generate(): Promise<Secp256k1Keypair> {
|
28
|
-
const privateKey = randomBytes(32);
|
29
|
-
const publicKey = secp.getPublicKey(privateKey);
|
30
|
-
return new Secp256k1Keypair(privateKey, publicKey);
|
31
|
-
}
|
32
|
-
|
33
|
-
privateKeyVerify(key = this._privateKey): boolean {
|
34
|
-
if (key) {
|
35
|
-
return secp.utils.isValidPrivateKey(key);
|
36
|
-
}
|
37
|
-
return true;
|
38
|
-
}
|
39
|
-
|
40
|
-
publicKeyVerify(key = this._publicKey): boolean {
|
41
|
-
if (key) {
|
42
|
-
try {
|
43
|
-
secp.Point.fromHex(key);
|
44
|
-
return true;
|
45
|
-
} catch {
|
46
|
-
return false;
|
47
|
-
}
|
48
|
-
}
|
49
|
-
return true;
|
50
|
-
}
|
51
|
-
|
52
|
-
get privateKey(): Uint8Array {
|
53
|
-
if (!this._privateKey) {
|
54
|
-
throw new Error();
|
55
|
-
}
|
56
|
-
return this._privateKey;
|
57
|
-
}
|
58
|
-
|
59
|
-
get publicKey(): Uint8Array {
|
60
|
-
if (!this._publicKey) {
|
61
|
-
throw new Error();
|
62
|
-
}
|
63
|
-
return this._publicKey;
|
64
|
-
}
|
65
|
-
|
66
|
-
hasPrivateKey(): boolean {
|
67
|
-
return !!this._privateKey;
|
68
|
-
}
|
69
|
-
}
|
package/src/keypair/types.ts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
export enum KeypairType {
|
2
|
-
rsa = 0,
|
3
|
-
ed25519 = 1,
|
4
|
-
secp256k1 = 2,
|
5
|
-
}
|
6
|
-
|
7
|
-
export interface IKeypair {
|
8
|
-
type: KeypairType;
|
9
|
-
privateKey: Uint8Array;
|
10
|
-
publicKey: Uint8Array;
|
11
|
-
privateKeyVerify(): boolean;
|
12
|
-
publicKeyVerify(): boolean;
|
13
|
-
hasPrivateKey(): boolean;
|
14
|
-
}
|