@waku/enr 0.0.1
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/bundle/crypto-5b69130e.js +2914 -0
- package/bundle/crypto.js +1 -0
- package/bundle/index.js +30072 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +8 -0
- package/dist/constants.js.map +1 -0
- package/dist/crypto.d.ts +22 -0
- package/dist/crypto.js +47 -0
- package/dist/crypto.js.map +1 -0
- package/dist/enr.d.ts +90 -0
- package/dist/enr.js +432 -0
- package/dist/enr.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/keypair/index.d.ts +8 -0
- package/dist/keypair/index.js +53 -0
- package/dist/keypair/index.js.map +1 -0
- package/dist/keypair/secp256k1.d.ts +13 -0
- package/dist/keypair/secp256k1.js +57 -0
- package/dist/keypair/secp256k1.js.map +1 -0
- package/dist/keypair/types.d.ts +13 -0
- package/dist/keypair/types.js +7 -0
- package/dist/keypair/types.js.map +1 -0
- package/dist/multiaddr_from_fields.d.ts +2 -0
- package/dist/multiaddr_from_fields.js +8 -0
- package/dist/multiaddr_from_fields.js.map +1 -0
- package/dist/multiaddrs_codec.d.ts +3 -0
- package/dist/multiaddrs_codec.js +32 -0
- package/dist/multiaddrs_codec.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/v4.d.ts +3 -0
- package/dist/v4.js +14 -0
- package/dist/v4.js.map +1 -0
- package/dist/waku2_codec.d.ts +8 -0
- package/dist/waku2_codec.js +36 -0
- package/dist/waku2_codec.js.map +1 -0
- package/package.json +191 -0
- package/src/constants.ts +10 -0
- package/src/crypto.ts +61 -0
- package/src/enr.ts +524 -0
- package/src/index.ts +6 -0
- package/src/keypair/index.ts +76 -0
- package/src/keypair/secp256k1.ts +69 -0
- package/src/keypair/types.ts +14 -0
- package/src/multiaddr_from_fields.ts +18 -0
- package/src/multiaddrs_codec.ts +50 -0
- package/src/types.ts +11 -0
- package/src/v4.ts +21 -0
- package/src/waku2_codec.ts +39 -0
@@ -0,0 +1,53 @@
|
|
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
|
@@ -0,0 +1 @@
|
|
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,gBAAgB,CAAC;AAClD,OAAO,EAAY,WAAW,EAAE,MAAM,YAAY,CAAC;AAEnD,MAAM,CAAC,MAAM,wBAAwB,GAAG,8BAA8B,CAAC;AACvE,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAE/B,MAAM,UAAU,aAAa,CAC3B,IAAiB,EACjB,UAAuB,EACvB,SAAsB;IAEtB,QAAQ,IAAI,EAAE;QACZ,KAAK,WAAW,CAAC,SAAS;YACxB,OAAO,IAAI,gBAAgB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QACrD;YACE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;KAC7C;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAAiB;IAEjB,QAAQ,OAAO,CAAC,IAAI,EAAE;QACpB,KAAK,WAAW,CAAC,SAAS,CAAC,CAAC;YAC1B,MAAM,SAAS,GAAG,IAAI,aAAa,CAAC,SAAS,CAAC,kBAAkB,CAC9D,OAAO,CAAC,SAAS,CAClB,CAAC;YAEF,MAAM,UAAU,GAAG,OAAO,CAAC,aAAa,EAAE;gBACxC,CAAC,CAAC,IAAI,aAAa,CAAC,SAAS,CAAC,mBAAmB,CAAC,OAAO,CAAC,UAAU,CAAC;gBACrE,CAAC,CAAC,SAAS,CAAC;YAEd,OAAO,cAAc,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;SAC3D;QACD;YACE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;KAC7C;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,MAAc;IAEd,IAAI,WAAW,CAAC;IAChB,QAAQ,MAAM,CAAC,IAAI,EAAE;QACnB,KAAK,KAAK;YACR,WAAW,GAAG,WAAW,CAAC,GAAG,CAAC;YAC9B,MAAM;QACR,KAAK,SAAS;YACZ,WAAW,GAAG,WAAW,CAAC,OAAO,CAAC;YAClC,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"}
|
@@ -0,0 +1,13 @@
|
|
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
|
+
}
|
@@ -0,0 +1,57 @@
|
|
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
|
@@ -0,0 +1 @@
|
|
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;AAEnD,MAAM,OAAO,gBAAgB;IAK3B,YAAY,UAAuB,EAAE,SAAsB;QACzD,IAAI,GAAG,GAAG,SAAS,CAAC;QACpB,IAAI,GAAG,EAAE;YACP,GAAG,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;SAC9B;QACD,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE;YAC/D,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;SACxC;QACD,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE;YACtD,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACvC;QAED,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC,SAAS,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,QAAQ;QACnB,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;QACnC,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;QAChD,OAAO,IAAI,gBAAgB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;IACrD,CAAC;IAED,gBAAgB,CAAC,GAAG,GAAG,IAAI,CAAC,WAAW;QACrC,IAAI,GAAG,EAAE;YACP,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC;SAC1C;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,eAAe,CAAC,GAAG,GAAG,IAAI,CAAC,UAAU;QACnC,IAAI,GAAG,EAAE;YACP,IAAI;gBACF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBACxB,OAAO,IAAI,CAAC;aACb;YAAC,MAAM;gBACN,OAAO,KAAK,CAAC;aACd;SACF;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,UAAU;QACZ,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"}
|
@@ -0,0 +1,13 @@
|
|
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
|
+
}
|
@@ -0,0 +1,7 @@
|
|
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
|
@@ -0,0 +1 @@
|
|
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"}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { multiaddr } from "@multiformats/multiaddr";
|
2
|
+
import { convertToString } from "@multiformats/multiaddr/convert";
|
3
|
+
export function multiaddrFromFields(ipFamily, protocol, ipBytes, protocolBytes) {
|
4
|
+
let ma = multiaddr("/" + ipFamily + "/" + convertToString(ipFamily, ipBytes));
|
5
|
+
ma = ma.encapsulate(multiaddr("/" + protocol + "/" + convertToString(protocol, protocolBytes)));
|
6
|
+
return ma;
|
7
|
+
}
|
8
|
+
//# sourceMappingURL=multiaddr_from_fields.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"multiaddr_from_fields.js","sourceRoot":"","sources":["../src/multiaddr_from_fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAElE,MAAM,UAAU,mBAAmB,CACjC,QAAgB,EAChB,QAAgB,EAChB,OAAmB,EACnB,aAAyB;IAEzB,IAAI,EAAE,GAAG,SAAS,CAAC,GAAG,GAAG,QAAQ,GAAG,GAAG,GAAG,eAAe,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;IAE9E,EAAE,GAAG,EAAE,CAAC,WAAW,CACjB,SAAS,CAAC,GAAG,GAAG,QAAQ,GAAG,GAAG,GAAG,eAAe,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAC3E,CAAC;IAEF,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { multiaddr } from "@multiformats/multiaddr";
|
2
|
+
import { MULTIADDR_LENGTH_SIZE } from "./constants.js";
|
3
|
+
export function decodeMultiaddrs(bytes) {
|
4
|
+
const multiaddrs = [];
|
5
|
+
let index = 0;
|
6
|
+
while (index < bytes.length) {
|
7
|
+
const sizeDataView = new DataView(bytes.buffer, index, MULTIADDR_LENGTH_SIZE);
|
8
|
+
const size = sizeDataView.getUint16(0);
|
9
|
+
index += MULTIADDR_LENGTH_SIZE;
|
10
|
+
const multiaddrBytes = bytes.slice(index, index + size);
|
11
|
+
index += size;
|
12
|
+
multiaddrs.push(multiaddr(multiaddrBytes));
|
13
|
+
}
|
14
|
+
return multiaddrs;
|
15
|
+
}
|
16
|
+
export function encodeMultiaddrs(multiaddrs) {
|
17
|
+
const totalLength = multiaddrs.reduce((acc, ma) => acc + MULTIADDR_LENGTH_SIZE + ma.bytes.length, 0);
|
18
|
+
const bytes = new Uint8Array(totalLength);
|
19
|
+
const dataView = new DataView(bytes.buffer);
|
20
|
+
let index = 0;
|
21
|
+
multiaddrs.forEach((multiaddr) => {
|
22
|
+
if (multiaddr.getPeerId())
|
23
|
+
throw new Error("`multiaddr` field MUST not contain peer id");
|
24
|
+
// Prepend the size of the next entry
|
25
|
+
dataView.setUint16(index, multiaddr.bytes.length);
|
26
|
+
index += MULTIADDR_LENGTH_SIZE;
|
27
|
+
bytes.set(multiaddr.bytes, index);
|
28
|
+
index += multiaddr.bytes.length;
|
29
|
+
});
|
30
|
+
return bytes;
|
31
|
+
}
|
32
|
+
//# sourceMappingURL=multiaddrs_codec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"multiaddrs_codec.js","sourceRoot":"","sources":["../src/multiaddrs_codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,MAAM,UAAU,gBAAgB,CAAC,KAAiB;IAChD,MAAM,UAAU,GAAG,EAAE,CAAC;IAEtB,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,OAAO,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE;QAC3B,MAAM,YAAY,GAAG,IAAI,QAAQ,CAC/B,KAAK,CAAC,MAAM,EACZ,KAAK,EACL,qBAAqB,CACtB,CAAC;QACF,MAAM,IAAI,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACvC,KAAK,IAAI,qBAAqB,CAAC;QAE/B,MAAM,cAAc,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC,CAAC;QACxD,KAAK,IAAI,IAAI,CAAC;QAEd,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC;KAC5C;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,UAAuB;IACtD,MAAM,WAAW,GAAG,UAAU,CAAC,MAAM,CACnC,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,qBAAqB,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAC1D,CAAC,CACF,CAAC;IACF,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;IAC1C,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAE5C,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,UAAU,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;QAC/B,IAAI,SAAS,CAAC,SAAS,EAAE;YACvB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAEhE,qCAAqC;QACrC,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAClD,KAAK,IAAI,qBAAqB,CAAC;QAE/B,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAClC,KAAK,IAAI,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/types.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* We represent NodeId as a hex string, since node equality is used very heavily
|
3
|
+
* and it is convenient to index data by NodeId
|
4
|
+
*/
|
5
|
+
export declare type NodeId = string;
|
6
|
+
export declare type SequenceNumber = bigint;
|
7
|
+
export declare type ENRKey = string;
|
8
|
+
export declare type ENRValue = Uint8Array;
|
package/dist/types.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,oCAAoC"}
|
package/dist/v4.d.ts
ADDED
package/dist/v4.js
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
import * as secp from "@noble/secp256k1";
|
2
|
+
import { bytesToHex } from "@waku/byte-utils";
|
3
|
+
import { keccak256 } from "./crypto.js";
|
4
|
+
export async function sign(privKey, msg) {
|
5
|
+
return secp.sign(keccak256(msg), privKey, {
|
6
|
+
der: false,
|
7
|
+
});
|
8
|
+
}
|
9
|
+
export function nodeId(pubKey) {
|
10
|
+
const publicKey = secp.Point.fromHex(pubKey);
|
11
|
+
const uncompressedPubkey = publicKey.toRawBytes(false);
|
12
|
+
return bytesToHex(keccak256(uncompressedPubkey.slice(1)));
|
13
|
+
}
|
14
|
+
//# sourceMappingURL=v4.js.map
|
package/dist/v4.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"v4.js","sourceRoot":"","sources":["../src/v4.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,OAAmB,EACnB,GAAe;IAEf,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE;QACxC,GAAG,EAAE,KAAK;KACX,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,MAAkB;IACvC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7C,MAAM,kBAAkB,GAAG,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAEvD,OAAO,UAAU,CAAC,SAAS,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5D,CAAC"}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
export function encodeWaku2(protocols) {
|
2
|
+
let byte = 0;
|
3
|
+
if (protocols.lightPush)
|
4
|
+
byte += 1;
|
5
|
+
byte = byte << 1;
|
6
|
+
if (protocols.filter)
|
7
|
+
byte += 1;
|
8
|
+
byte = byte << 1;
|
9
|
+
if (protocols.store)
|
10
|
+
byte += 1;
|
11
|
+
byte = byte << 1;
|
12
|
+
if (protocols.relay)
|
13
|
+
byte += 1;
|
14
|
+
return byte;
|
15
|
+
}
|
16
|
+
export function decodeWaku2(byte) {
|
17
|
+
const waku2 = {
|
18
|
+
relay: false,
|
19
|
+
store: false,
|
20
|
+
filter: false,
|
21
|
+
lightPush: false,
|
22
|
+
};
|
23
|
+
if (byte % 2)
|
24
|
+
waku2.relay = true;
|
25
|
+
byte = byte >> 1;
|
26
|
+
if (byte % 2)
|
27
|
+
waku2.store = true;
|
28
|
+
byte = byte >> 1;
|
29
|
+
if (byte % 2)
|
30
|
+
waku2.filter = true;
|
31
|
+
byte = byte >> 1;
|
32
|
+
if (byte % 2)
|
33
|
+
waku2.lightPush = true;
|
34
|
+
return waku2;
|
35
|
+
}
|
36
|
+
//# sourceMappingURL=waku2_codec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"waku2_codec.js","sourceRoot":"","sources":["../src/waku2_codec.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,WAAW,CAAC,SAAgB;IAC1C,IAAI,IAAI,GAAG,CAAC,CAAC;IAEb,IAAI,SAAS,CAAC,SAAS;QAAE,IAAI,IAAI,CAAC,CAAC;IACnC,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IACjB,IAAI,SAAS,CAAC,MAAM;QAAE,IAAI,IAAI,CAAC,CAAC;IAChC,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IACjB,IAAI,SAAS,CAAC,KAAK;QAAE,IAAI,IAAI,CAAC,CAAC;IAC/B,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IACjB,IAAI,SAAS,CAAC,KAAK;QAAE,IAAI,IAAI,CAAC,CAAC;IAE/B,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,MAAM,KAAK,GAAG;QACZ,KAAK,EAAE,KAAK;QACZ,KAAK,EAAE,KAAK;QACZ,MAAM,EAAE,KAAK;QACb,SAAS,EAAE,KAAK;KACjB,CAAC;IAEF,IAAI,IAAI,GAAG,CAAC;QAAE,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IACjC,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IACjB,IAAI,IAAI,GAAG,CAAC;QAAE,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IACjC,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IACjB,IAAI,IAAI,GAAG,CAAC;QAAE,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC;IAClC,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IACjB,IAAI,IAAI,GAAG,CAAC;QAAE,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC;IAErC,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
{
|
2
|
+
"name": "@waku/enr",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"description": "ENR (EIP-778) for Waku",
|
5
|
+
"types": "./dist/index.d.ts",
|
6
|
+
"module": "./dist/index.js",
|
7
|
+
"exports": {
|
8
|
+
".": {
|
9
|
+
"types": "./dist/index.d.ts",
|
10
|
+
"import": "./dist/index.js"
|
11
|
+
}
|
12
|
+
},
|
13
|
+
"type": "module",
|
14
|
+
"author": "Waku Team",
|
15
|
+
"homepage": "https://github.com/waku-org/js-waku/tree/master/packages/enr#readme",
|
16
|
+
"repository": {
|
17
|
+
"type": "git",
|
18
|
+
"url": "https://github.com/waku-org/js-waku.git"
|
19
|
+
},
|
20
|
+
"bugs": {
|
21
|
+
"url": "https://github.com/waku-org/js-waku/issues"
|
22
|
+
},
|
23
|
+
"license": "MIT OR Apache-2.0",
|
24
|
+
"keywords": [
|
25
|
+
"waku",
|
26
|
+
"decentralized",
|
27
|
+
"secure",
|
28
|
+
"communication",
|
29
|
+
"web3",
|
30
|
+
"ethereum",
|
31
|
+
"dapps",
|
32
|
+
"privacy"
|
33
|
+
],
|
34
|
+
"scripts": {
|
35
|
+
"build": "run-s build:**",
|
36
|
+
"build:esm": "tsc",
|
37
|
+
"build:bundle": "rollup --config rollup.config.js",
|
38
|
+
"fix": "run-s fix:*",
|
39
|
+
"fix:prettier": "prettier . --write",
|
40
|
+
"fix:lint": "eslint src --ext .ts --ext .cjs --fix",
|
41
|
+
"check": "run-s check:*",
|
42
|
+
"check:lint": "eslint src --ext .ts",
|
43
|
+
"check:prettier": "prettier . --list-different",
|
44
|
+
"check:spelling": "cspell \"{README.md,src/**/*.ts}\"",
|
45
|
+
"check:tsc": "tsc -p tsconfig.dev.json",
|
46
|
+
"test": "run-s test:*",
|
47
|
+
"test:node": "TS_NODE_PROJECT=./tsconfig.dev.json mocha",
|
48
|
+
"test:browser": "karma start karma.conf.cjs",
|
49
|
+
"prepublish": "npm run build",
|
50
|
+
"reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build",
|
51
|
+
"release": "semantic-release"
|
52
|
+
},
|
53
|
+
"engines": {
|
54
|
+
"node": ">=16"
|
55
|
+
},
|
56
|
+
"dependencies": {
|
57
|
+
"@ethersproject/rlp": "^5.5.0",
|
58
|
+
"@libp2p/crypto": "^1.0.4",
|
59
|
+
"@libp2p/interface-peer-id": "^1.0.2",
|
60
|
+
"@libp2p/peer-id": "^1.1.10",
|
61
|
+
"@multiformats/multiaddr": "^11.0.6",
|
62
|
+
"@noble/secp256k1": "^1.3.4",
|
63
|
+
"@waku/byte-utils": "*",
|
64
|
+
"js-sha3": "^0.8.0"
|
65
|
+
},
|
66
|
+
"devDependencies": {
|
67
|
+
"uint8arrays": "^4.0.2",
|
68
|
+
"@libp2p/peer-id-factory": "^1.0.15",
|
69
|
+
"@rollup/plugin-commonjs": "^22.0.0",
|
70
|
+
"@rollup/plugin-json": "^4.1.0",
|
71
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
72
|
+
"@semantic-release/changelog": "^6.0.1",
|
73
|
+
"@semantic-release/commit-analyzer": "^9.0.2",
|
74
|
+
"@semantic-release/git": "^10.0.1",
|
75
|
+
"@semantic-release/github": "^8.0.6",
|
76
|
+
"@semantic-release/npm": "^9.0.1",
|
77
|
+
"@semantic-release/release-notes-generator": "^10.0.3",
|
78
|
+
"@typescript-eslint/eslint-plugin": "^5.8.1",
|
79
|
+
"@typescript-eslint/parser": "^5.8.1",
|
80
|
+
"chai": "^4.3.6",
|
81
|
+
"cspell": "^5.14.0",
|
82
|
+
"eslint": "^8.6.0",
|
83
|
+
"eslint-config-prettier": "^8.3.0",
|
84
|
+
"eslint-plugin-eslint-comments": "^3.2.0",
|
85
|
+
"eslint-plugin-functional": "^4.0.2",
|
86
|
+
"eslint-plugin-import": "^2.25.3",
|
87
|
+
"eslint-plugin-prettier": "^4.0.0",
|
88
|
+
"npm-run-all": "^4.1.5",
|
89
|
+
"prettier": "^2.1.1",
|
90
|
+
"rollup": "^2.75.0",
|
91
|
+
"semantic-release": "^19.0.5",
|
92
|
+
"semantic-release-monorepo": "^7.0.5",
|
93
|
+
"ts-loader": "^9.4.1",
|
94
|
+
"typescript": "^4.6.3"
|
95
|
+
},
|
96
|
+
"release": {
|
97
|
+
"branches": [
|
98
|
+
"master"
|
99
|
+
],
|
100
|
+
"extends": "semantic-release-monorepo",
|
101
|
+
"plugins": [
|
102
|
+
[
|
103
|
+
"@semantic-release/commit-analyzer",
|
104
|
+
{
|
105
|
+
"preset": "conventionalcommits",
|
106
|
+
"releaseRules": [
|
107
|
+
{
|
108
|
+
"breaking": true,
|
109
|
+
"release": "patch"
|
110
|
+
},
|
111
|
+
{
|
112
|
+
"revert": true,
|
113
|
+
"release": "patch"
|
114
|
+
},
|
115
|
+
{
|
116
|
+
"type": "feat",
|
117
|
+
"release": "patch"
|
118
|
+
},
|
119
|
+
{
|
120
|
+
"type": "fix",
|
121
|
+
"release": "patch"
|
122
|
+
},
|
123
|
+
{
|
124
|
+
"type": "doc",
|
125
|
+
"release": "patch"
|
126
|
+
},
|
127
|
+
{
|
128
|
+
"type": "test",
|
129
|
+
"release": "patch"
|
130
|
+
},
|
131
|
+
{
|
132
|
+
"scope": "deps",
|
133
|
+
"release": "patch"
|
134
|
+
},
|
135
|
+
{
|
136
|
+
"scope": "no-release",
|
137
|
+
"release": false
|
138
|
+
}
|
139
|
+
]
|
140
|
+
}
|
141
|
+
],
|
142
|
+
[
|
143
|
+
"@semantic-release/release-notes-generator",
|
144
|
+
{
|
145
|
+
"preset": "conventionalcommits",
|
146
|
+
"presetConfig": {
|
147
|
+
"types": [
|
148
|
+
{
|
149
|
+
"type": "feat",
|
150
|
+
"section": "Features"
|
151
|
+
},
|
152
|
+
{
|
153
|
+
"type": "fix",
|
154
|
+
"section": "Bug Fixes"
|
155
|
+
},
|
156
|
+
{
|
157
|
+
"type": "chore",
|
158
|
+
"section": "Trivial Changes"
|
159
|
+
},
|
160
|
+
{
|
161
|
+
"type": "doc",
|
162
|
+
"section": "Documentation"
|
163
|
+
},
|
164
|
+
{
|
165
|
+
"type": "test",
|
166
|
+
"section": "Tests"
|
167
|
+
}
|
168
|
+
]
|
169
|
+
}
|
170
|
+
}
|
171
|
+
],
|
172
|
+
"@semantic-release/changelog",
|
173
|
+
"@semantic-release/npm",
|
174
|
+
"@semantic-release/github",
|
175
|
+
"@semantic-release/git"
|
176
|
+
]
|
177
|
+
},
|
178
|
+
"typedoc": {
|
179
|
+
"entryPoint": "./src/index.ts"
|
180
|
+
},
|
181
|
+
"files": [
|
182
|
+
"dist",
|
183
|
+
"bundle",
|
184
|
+
"src/**/*.ts",
|
185
|
+
"!**/*.spec.*",
|
186
|
+
"!**/*.json",
|
187
|
+
"CHANGELOG.md",
|
188
|
+
"LICENSE",
|
189
|
+
"README.md"
|
190
|
+
]
|
191
|
+
}
|
package/src/constants.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
// Maximum encoded size of an ENR
|
2
|
+
export const MAX_RECORD_SIZE = 300;
|
3
|
+
|
4
|
+
export const ERR_INVALID_ID = "Invalid record id";
|
5
|
+
|
6
|
+
export const ERR_NO_SIGNATURE = "No valid signature found";
|
7
|
+
|
8
|
+
// The maximum length of byte size of a multiaddr to encode in the `multiaddr` field
|
9
|
+
// The size is a big endian 16-bit unsigned integer
|
10
|
+
export const MULTIADDR_LENGTH_SIZE = 2;
|
package/src/crypto.ts
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
import * as secp from "@noble/secp256k1";
|
2
|
+
import { concat } from "@waku/byte-utils";
|
3
|
+
import sha3 from "js-sha3";
|
4
|
+
|
5
|
+
export const randomBytes = secp.utils.randomBytes;
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Return the public key for the given private key, to be used for asymmetric
|
9
|
+
* encryption.
|
10
|
+
*/
|
11
|
+
export const getPublicKey = secp.getPublicKey;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* ECDSA Sign a message with the given private key.
|
15
|
+
*
|
16
|
+
* @param message The message to sign, usually a hash.
|
17
|
+
* @param privateKey The ECDSA private key to use to sign the message.
|
18
|
+
*
|
19
|
+
* @returns The signature and the recovery id concatenated.
|
20
|
+
*/
|
21
|
+
export async function sign(
|
22
|
+
message: Uint8Array,
|
23
|
+
privateKey: Uint8Array
|
24
|
+
): Promise<Uint8Array> {
|
25
|
+
const [signature, recoveryId] = await secp.sign(message, privateKey, {
|
26
|
+
recovered: true,
|
27
|
+
der: false,
|
28
|
+
});
|
29
|
+
return concat(
|
30
|
+
[signature, new Uint8Array([recoveryId])],
|
31
|
+
signature.length + 1
|
32
|
+
);
|
33
|
+
}
|
34
|
+
|
35
|
+
export function keccak256(input: Uint8Array): Uint8Array {
|
36
|
+
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
37
|
+
}
|
38
|
+
|
39
|
+
export function compressPublicKey(publicKey: Uint8Array): Uint8Array {
|
40
|
+
if (publicKey.length === 64) {
|
41
|
+
publicKey = concat([new Uint8Array([4]), publicKey], 65);
|
42
|
+
}
|
43
|
+
const point = secp.Point.fromHex(publicKey);
|
44
|
+
return point.toRawBytes(true);
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Verify an ECDSA signature.
|
49
|
+
*/
|
50
|
+
export function verifySignature(
|
51
|
+
signature: Uint8Array,
|
52
|
+
message: Uint8Array | string,
|
53
|
+
publicKey: Uint8Array
|
54
|
+
): boolean {
|
55
|
+
try {
|
56
|
+
const _signature = secp.Signature.fromCompact(signature.slice(0, 64));
|
57
|
+
return secp.verify(_signature, message, publicKey);
|
58
|
+
} catch {
|
59
|
+
return false;
|
60
|
+
}
|
61
|
+
}
|