@libp2p/peer-record 2.0.1 → 4.0.0
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/src/envelope/envelope.d.ts +3 -2
- package/dist/src/envelope/envelope.d.ts.map +1 -1
- package/dist/src/envelope/envelope.js.map +1 -1
- package/dist/src/envelope/index.d.ts +7 -6
- package/dist/src/envelope/index.d.ts.map +1 -1
- package/dist/src/envelope/index.js +13 -21
- package/dist/src/envelope/index.js.map +1 -1
- package/dist/src/peer-record/index.d.ts +3 -2
- package/dist/src/peer-record/index.d.ts.map +1 -1
- package/dist/src/peer-record/index.js.map +1 -1
- package/dist/src/peer-record/peer-record.d.ts +5 -4
- package/dist/src/peer-record/peer-record.d.ts.map +1 -1
- package/dist/src/peer-record/peer-record.js.map +1 -1
- package/package.json +17 -11
- package/src/envelope/envelope.ts +3 -2
- package/src/envelope/index.ts +25 -28
- package/src/peer-record/index.ts +3 -2
- package/src/peer-record/peer-record.ts +5 -4
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Codec } from 'protons-runtime';
|
|
2
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
2
3
|
export interface Envelope {
|
|
3
4
|
publicKey: Uint8Array;
|
|
4
5
|
payloadType: Uint8Array;
|
|
@@ -7,7 +8,7 @@ export interface Envelope {
|
|
|
7
8
|
}
|
|
8
9
|
export declare namespace Envelope {
|
|
9
10
|
const codec: () => Codec<Envelope>;
|
|
10
|
-
const encode: (obj: Envelope) =>
|
|
11
|
-
const decode: (buf: Uint8Array) => Envelope;
|
|
11
|
+
const encode: (obj: Envelope) => Uint8ArrayList;
|
|
12
|
+
const decode: (buf: Uint8Array | Uint8ArrayList) => Envelope;
|
|
12
13
|
}
|
|
13
14
|
//# sourceMappingURL=envelope.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../../../src/envelope/envelope.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../../../src/envelope/envelope.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,MAAM,WAAW,QAAQ;IACvB,SAAS,EAAE,UAAU,CAAA;IACrB,WAAW,EAAE,UAAU,CAAA;IACvB,OAAO,EAAE,UAAU,CAAA;IACnB,SAAS,EAAE,UAAU,CAAA;CACtB;AAED,yBAAiB,QAAQ,CAAC;IACjB,MAAM,KAAK,QAAO,MAAM,QAAQ,CAOtC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,KAAG,cAEtC,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,KAAG,QAEzD,CAAA;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"envelope.js","sourceRoot":"","sources":["../../../src/envelope/envelope.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,oDAAoD;AAEpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"envelope.js","sourceRoot":"","sources":["../../../src/envelope/envelope.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,oDAAoD;AAEpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAW9E,MAAM,KAAW,QAAQ,CAiBxB;AAjBD,WAAiB,QAAQ;IACV,cAAK,GAAG,GAAoB,EAAE;QACzC,OAAO,OAAO,CAAW;YACvB,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE;YACtC,CAAC,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,KAAK,EAAE;YACxC,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE;YACpC,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE;SACvC,CAAC,CAAA;IACJ,CAAC,CAAA;IAEY,eAAM,GAAG,CAAC,GAAa,EAAkB,EAAE;QACtD,OAAO,aAAa,CAAC,GAAG,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;IAC7C,CAAC,CAAA;IAEY,eAAM,GAAG,CAAC,GAAgC,EAAY,EAAE;QACnE,OAAO,aAAa,CAAC,GAAG,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;IAC7C,CAAC,CAAA;AACH,CAAC,EAjBgB,QAAQ,KAAR,QAAQ,QAiBxB"}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
2
2
|
import type { Record, Envelope } from '@libp2p/interface-record';
|
|
3
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
3
4
|
export interface EnvelopeInit {
|
|
4
5
|
peerId: PeerId;
|
|
5
6
|
payloadType: Uint8Array;
|
|
6
|
-
payload: Uint8Array;
|
|
7
|
+
payload: Uint8Array | Uint8ArrayList;
|
|
7
8
|
signature: Uint8Array;
|
|
8
9
|
}
|
|
9
10
|
export declare class RecordEnvelope implements Envelope {
|
|
10
11
|
/**
|
|
11
12
|
* Unmarshal a serialized Envelope protobuf message
|
|
12
13
|
*/
|
|
13
|
-
static createFromProtobuf: (data: Uint8Array) => Promise<RecordEnvelope>;
|
|
14
|
+
static createFromProtobuf: (data: Uint8Array | Uint8ArrayList) => Promise<RecordEnvelope>;
|
|
14
15
|
/**
|
|
15
16
|
* Seal marshals the given Record, places the marshaled bytes inside an Envelope
|
|
16
17
|
* and signs it with the given peerId's private key
|
|
@@ -20,12 +21,12 @@ export declare class RecordEnvelope implements Envelope {
|
|
|
20
21
|
* Open and certify a given marshalled envelope.
|
|
21
22
|
* Data is unmarshalled and the signature validated for the given domain.
|
|
22
23
|
*/
|
|
23
|
-
static openAndCertify: (data: Uint8Array, domain: string) => Promise<RecordEnvelope>;
|
|
24
|
+
static openAndCertify: (data: Uint8Array | Uint8ArrayList, domain: string) => Promise<RecordEnvelope>;
|
|
24
25
|
peerId: PeerId;
|
|
25
26
|
payloadType: Uint8Array;
|
|
26
|
-
payload: Uint8Array;
|
|
27
|
+
payload: Uint8Array | Uint8ArrayList;
|
|
27
28
|
signature: Uint8Array;
|
|
28
|
-
marshaled?:
|
|
29
|
+
marshaled?: Uint8ArrayList;
|
|
29
30
|
/**
|
|
30
31
|
* The Envelope is responsible for keeping an arbitrary signed record
|
|
31
32
|
* by a libp2p peer.
|
|
@@ -34,7 +35,7 @@ export declare class RecordEnvelope implements Envelope {
|
|
|
34
35
|
/**
|
|
35
36
|
* Marshal the envelope content
|
|
36
37
|
*/
|
|
37
|
-
marshal():
|
|
38
|
+
marshal(): Uint8ArrayList;
|
|
38
39
|
/**
|
|
39
40
|
* Verifies if the other Envelope is identical to this one
|
|
40
41
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAG/C,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,UAAU,CAAA;IACvB,OAAO,EAAE,UAAU,GAAG,cAAc,CAAA;IACpC,SAAS,EAAE,UAAU,CAAA;CACtB;AAED,qBAAa,cAAe,YAAW,QAAQ;IAC7C;;OAEG;IACH,MAAM,CAAC,kBAAkB,SAAgB,UAAU,GAAG,cAAc,6BAUnE;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,WAAkB,MAAM,UAAU,MAAM,6BAkBlD;IAED;;;OAGG;IACH,MAAM,CAAC,cAAc,SAAgB,UAAU,GAAG,cAAc,UAAU,MAAM,6BAS/E;IAEM,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,UAAU,CAAA;IACvB,OAAO,EAAE,UAAU,GAAG,cAAc,CAAA;IACpC,SAAS,EAAE,UAAU,CAAA;IACrB,SAAS,CAAC,EAAE,cAAc,CAAA;IAEjC;;;OAGG;gBACU,IAAI,EAAE,YAAY;IAS/B;;OAEG;IACH,OAAO,IAAK,cAAc;IAiB1B;;OAEG;IACH,MAAM,CAAE,KAAK,EAAE,QAAQ;IAIvB;;OAEG;IACG,QAAQ,CAAE,MAAM,EAAE,MAAM;CAW/B"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
var _a;
|
|
2
2
|
import errCode from 'err-code';
|
|
3
|
-
import { concat as uint8arraysConcat } from 'uint8arrays/concat';
|
|
4
3
|
import { fromString as uint8arraysFromString } from 'uint8arrays/from-string';
|
|
5
4
|
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys';
|
|
6
|
-
import varint from 'varint';
|
|
7
|
-
import { equals as uint8arraysEquals } from 'uint8arrays/equals';
|
|
8
5
|
import { codes } from '../errors.js';
|
|
9
6
|
import { Envelope as Protobuf } from './envelope.js';
|
|
10
7
|
import { peerIdFromKeys } from '@libp2p/peer-id';
|
|
8
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
9
|
+
import { unsigned } from 'uint8-varint';
|
|
11
10
|
export class RecordEnvelope {
|
|
12
11
|
/**
|
|
13
12
|
* The Envelope is responsible for keeping an arbitrary signed record
|
|
@@ -31,7 +30,7 @@ export class RecordEnvelope {
|
|
|
31
30
|
this.marshaled = Protobuf.encode({
|
|
32
31
|
publicKey: this.peerId.publicKey,
|
|
33
32
|
payloadType: this.payloadType,
|
|
34
|
-
payload: this.payload,
|
|
33
|
+
payload: this.payload.subarray(),
|
|
35
34
|
signature: this.signature
|
|
36
35
|
});
|
|
37
36
|
}
|
|
@@ -41,7 +40,7 @@ export class RecordEnvelope {
|
|
|
41
40
|
* Verifies if the other Envelope is identical to this one
|
|
42
41
|
*/
|
|
43
42
|
equals(other) {
|
|
44
|
-
return
|
|
43
|
+
return this.marshal().equals(other.marshal());
|
|
45
44
|
}
|
|
46
45
|
/**
|
|
47
46
|
* Validate envelope data signature for the given domain
|
|
@@ -52,7 +51,7 @@ export class RecordEnvelope {
|
|
|
52
51
|
throw new Error('Missing public key');
|
|
53
52
|
}
|
|
54
53
|
const key = unmarshalPublicKey(this.peerId.publicKey);
|
|
55
|
-
return await key.verify(signData, this.signature);
|
|
54
|
+
return await key.verify(signData.subarray(), this.signature);
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
_a = RecordEnvelope;
|
|
@@ -74,15 +73,15 @@ RecordEnvelope.createFromProtobuf = async (data) => {
|
|
|
74
73
|
* and signs it with the given peerId's private key
|
|
75
74
|
*/
|
|
76
75
|
RecordEnvelope.seal = async (record, peerId) => {
|
|
76
|
+
if (peerId.privateKey == null) {
|
|
77
|
+
throw new Error('Missing private key');
|
|
78
|
+
}
|
|
77
79
|
const domain = record.domain;
|
|
78
80
|
const payloadType = record.codec;
|
|
79
81
|
const payload = record.marshal();
|
|
80
82
|
const signData = formatSignaturePayload(domain, payloadType, payload);
|
|
81
|
-
if (peerId.privateKey == null) {
|
|
82
|
-
throw new Error('Missing private key');
|
|
83
|
-
}
|
|
84
83
|
const key = await unmarshalPrivateKey(peerId.privateKey);
|
|
85
|
-
const signature = await key.sign(signData);
|
|
84
|
+
const signature = await key.sign(signData.subarray());
|
|
86
85
|
return new RecordEnvelope({
|
|
87
86
|
peerId,
|
|
88
87
|
payloadType,
|
|
@@ -114,16 +113,9 @@ const formatSignaturePayload = (domain, payloadType, payload) => {
|
|
|
114
113
|
// - The length of the payload field in bytes
|
|
115
114
|
// - The value of the payload field
|
|
116
115
|
const domainUint8Array = uint8arraysFromString(domain);
|
|
117
|
-
const domainLength =
|
|
118
|
-
const payloadTypeLength =
|
|
119
|
-
const payloadLength =
|
|
120
|
-
return
|
|
121
|
-
new Uint8Array(domainLength),
|
|
122
|
-
domainUint8Array,
|
|
123
|
-
new Uint8Array(payloadTypeLength),
|
|
124
|
-
payloadType,
|
|
125
|
-
new Uint8Array(payloadLength),
|
|
126
|
-
payload
|
|
127
|
-
]);
|
|
116
|
+
const domainLength = unsigned.encode(domainUint8Array.byteLength);
|
|
117
|
+
const payloadTypeLength = unsigned.encode(payloadType.length);
|
|
118
|
+
const payloadLength = unsigned.encode(payload.length);
|
|
119
|
+
return new Uint8ArrayList(domainLength, domainUint8Array, payloadTypeLength, payloadType, payloadLength, payload);
|
|
128
120
|
};
|
|
129
121
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":";AAAA,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":";AAAA,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,UAAU,IAAI,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC7E,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAC7E,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AACpC,OAAO,EAAE,QAAQ,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,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iaddr } from '@multiformats/multiaddr';
|
|
2
2
|
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
3
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
3
4
|
export interface PeerRecordInit {
|
|
4
5
|
peerId: PeerId;
|
|
5
6
|
/**
|
|
@@ -19,7 +20,7 @@ export declare class PeerRecord {
|
|
|
19
20
|
/**
|
|
20
21
|
* Unmarshal Peer Record Protobuf
|
|
21
22
|
*/
|
|
22
|
-
static createFromProtobuf: (buf: Uint8Array) => PeerRecord;
|
|
23
|
+
static createFromProtobuf: (buf: Uint8Array | Uint8ArrayList) => PeerRecord;
|
|
23
24
|
static DOMAIN: string;
|
|
24
25
|
static CODEC: Uint8Array;
|
|
25
26
|
peerId: PeerId;
|
|
@@ -32,7 +33,7 @@ export declare class PeerRecord {
|
|
|
32
33
|
/**
|
|
33
34
|
* Marshal a record to be used in an envelope
|
|
34
35
|
*/
|
|
35
|
-
marshal():
|
|
36
|
+
marshal(): Uint8ArrayList;
|
|
36
37
|
/**
|
|
37
38
|
* Returns true if `this` record equals the `other`
|
|
38
39
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/peer-record/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/peer-record/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAQvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,UAAU,CAAC,EAAE,SAAS,EAAE,CAAA;IAExB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;GAGG;AACH,qBAAa,UAAU;IACrB;;OAEG;IACH,MAAM,CAAC,kBAAkB,QAAS,UAAU,GAAG,cAAc,KAAG,UAAU,CAOzE;IAED,MAAM,CAAC,MAAM,SAA8B;IAC3C,MAAM,CAAC,KAAK,aAAoC;IAEzC,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,SAAS,EAAE,CAAA;IACvB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,SAAoB;IAC1B,KAAK,aAAmB;IAC/B,OAAO,CAAC,SAAS,CAAC,CAAgB;gBAErB,IAAI,EAAE,cAAc;IAQjC;;OAEG;IACH,OAAO;IAcP;;OAEG;IACH,MAAM,CAAE,KAAK,EAAE,OAAO;CAsBvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/peer-record/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,UAAU,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EACL,2BAA2B,EAC3B,iCAAiC,EAClC,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/peer-record/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,UAAU,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EACL,2BAA2B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,EAAE;YACtC,OAAO,KAAK,CAAA;SACb;QAED,sBAAsB;QACtB,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,EAAE;YACnD,OAAO,KAAK,CAAA;SACb;QAED,OAAO,IAAI,CAAA;IACb,CAAC;;AAvED;;GAEG;AACI,6BAAkB,GAAG,CAAC,GAAgC,EAAc,EAAE;IAC3E,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACvC,MAAM,MAAM,GAAG,eAAe,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IACjD,MAAM,UAAU,GAAG,CAAC,UAAU,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAA;IACtF,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,CAAA;IAEhC,OAAO,IAAI,UAAU,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC,CAAA;AAC1D,CAAC,CAAA;AAEM,iBAAM,GAAG,2BAA2B,CAAA;AACpC,gBAAK,GAAG,iCAAiC,CAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Codec } from 'protons-runtime';
|
|
2
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
2
3
|
export interface PeerRecord {
|
|
3
4
|
peerId: Uint8Array;
|
|
4
5
|
seq: bigint;
|
|
@@ -10,11 +11,11 @@ export declare namespace PeerRecord {
|
|
|
10
11
|
}
|
|
11
12
|
namespace AddressInfo {
|
|
12
13
|
const codec: () => Codec<AddressInfo>;
|
|
13
|
-
const encode: (obj: AddressInfo) =>
|
|
14
|
-
const decode: (buf: Uint8Array) => AddressInfo;
|
|
14
|
+
const encode: (obj: AddressInfo) => Uint8ArrayList;
|
|
15
|
+
const decode: (buf: Uint8Array | Uint8ArrayList) => AddressInfo;
|
|
15
16
|
}
|
|
16
17
|
const codec: () => Codec<PeerRecord>;
|
|
17
|
-
const encode: (obj: PeerRecord) =>
|
|
18
|
-
const decode: (buf: Uint8Array) => PeerRecord;
|
|
18
|
+
const encode: (obj: PeerRecord) => Uint8ArrayList;
|
|
19
|
+
const decode: (buf: Uint8Array | Uint8ArrayList) => PeerRecord;
|
|
19
20
|
}
|
|
20
21
|
//# sourceMappingURL=peer-record.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer-record.d.ts","sourceRoot":"","sources":["../../../src/peer-record/peer-record.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"peer-record.d.ts","sourceRoot":"","sources":["../../../src/peer-record/peer-record.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,UAAU,CAAA;IAClB,GAAG,EAAE,MAAM,CAAA;IACX,SAAS,EAAE,UAAU,CAAC,WAAW,EAAE,CAAA;CACpC;AAED,yBAAiB,UAAU,CAAC;IAC1B,UAAiB,WAAW;QAC1B,SAAS,EAAE,UAAU,CAAA;KACtB;IAED,UAAiB,WAAW,CAAC;QACpB,MAAM,KAAK,QAAO,MAAM,WAAW,CAIzC,CAAA;QAEM,MAAM,MAAM,QAAS,WAAW,KAAG,cAEzC,CAAA;QAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,KAAG,WAEzD,CAAA;KACF;IAEM,MAAM,KAAK,QAAO,MAAM,UAAU,CAMxC,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,KAAG,cAExC,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,KAAG,UAEzD,CAAA;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer-record.js","sourceRoot":"","sources":["../../../src/peer-record/peer-record.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,oDAAoD;AAEpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"peer-record.js","sourceRoot":"","sources":["../../../src/peer-record/peer-record.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,oDAAoD;AAEpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAUtF,MAAM,KAAW,UAAU,CAoC1B;AApCD,WAAiB,UAAU;IAKzB,IAAiB,WAAW,CAc3B;IAdD,WAAiB,WAAW;QACb,iBAAK,GAAG,GAAuB,EAAE;YAC5C,OAAO,OAAO,CAAc;gBAC1B,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE;aACvC,CAAC,CAAA;QACJ,CAAC,CAAA;QAEY,kBAAM,GAAG,CAAC,GAAgB,EAAkB,EAAE;YACzD,OAAO,aAAa,CAAC,GAAG,EAAE,WAAW,CAAC,KAAK,EAAE,CAAC,CAAA;QAChD,CAAC,CAAA;QAEY,kBAAM,GAAG,CAAC,GAAgC,EAAe,EAAE;YACtE,OAAO,aAAa,CAAC,GAAG,EAAE,WAAW,CAAC,KAAK,EAAE,CAAC,CAAA;QAChD,CAAC,CAAA;IACH,CAAC,EAdgB,WAAW,GAAX,sBAAW,KAAX,sBAAW,QAc3B;IAEY,gBAAK,GAAG,GAAsB,EAAE;QAC3C,OAAO,OAAO,CAAa;YACzB,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE;YACnC,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE;YACjC,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;SAC/E,CAAC,CAAA;IACJ,CAAC,CAAA;IAEY,iBAAM,GAAG,CAAC,GAAe,EAAkB,EAAE;QACxD,OAAO,aAAa,CAAC,GAAG,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC,CAAA;IAC/C,CAAC,CAAA;IAEY,iBAAM,GAAG,CAAC,GAAgC,EAAc,EAAE;QACrE,OAAO,aAAa,CAAC,GAAG,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC,CAAA;IAC/C,CAAC,CAAA;AACH,CAAC,EApCgB,UAAU,KAAV,UAAU,QAoC1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/peer-record",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Used to transfer signed peer data across the network",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-peer-record#readme",
|
|
@@ -71,15 +71,15 @@
|
|
|
71
71
|
"release": "patch"
|
|
72
72
|
},
|
|
73
73
|
{
|
|
74
|
-
"type": "
|
|
74
|
+
"type": "docs",
|
|
75
75
|
"release": "patch"
|
|
76
76
|
},
|
|
77
77
|
{
|
|
78
|
-
"type": "
|
|
78
|
+
"type": "test",
|
|
79
79
|
"release": "patch"
|
|
80
80
|
},
|
|
81
81
|
{
|
|
82
|
-
"type": "
|
|
82
|
+
"type": "deps",
|
|
83
83
|
"release": "patch"
|
|
84
84
|
},
|
|
85
85
|
{
|
|
@@ -109,7 +109,11 @@
|
|
|
109
109
|
},
|
|
110
110
|
{
|
|
111
111
|
"type": "docs",
|
|
112
|
-
"section": "
|
|
112
|
+
"section": "Documentation"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"type": "deps",
|
|
116
|
+
"section": "Dependencies"
|
|
113
117
|
},
|
|
114
118
|
{
|
|
115
119
|
"type": "test",
|
|
@@ -143,10 +147,10 @@
|
|
|
143
147
|
"dependencies": {
|
|
144
148
|
"@libp2p/crypto": "^1.0.0",
|
|
145
149
|
"@libp2p/interface-peer-id": "^1.0.2",
|
|
146
|
-
"@libp2p/interface-record": "^
|
|
150
|
+
"@libp2p/interface-record": "^2.0.0",
|
|
147
151
|
"@libp2p/logger": "^2.0.0",
|
|
148
152
|
"@libp2p/peer-id": "^1.1.13",
|
|
149
|
-
"@libp2p/utils": "^
|
|
153
|
+
"@libp2p/utils": "^3.0.0",
|
|
150
154
|
"@multiformats/multiaddr": "^10.1.5",
|
|
151
155
|
"err-code": "^3.0.1",
|
|
152
156
|
"interface-datastore": "^6.1.0",
|
|
@@ -156,16 +160,18 @@
|
|
|
156
160
|
"it-map": "^1.0.6",
|
|
157
161
|
"it-pipe": "^2.0.3",
|
|
158
162
|
"multiformats": "^9.6.3",
|
|
159
|
-
"protons-runtime": "^
|
|
163
|
+
"protons-runtime": "^2.0.2",
|
|
164
|
+
"uint8-varint": "^1.0.2",
|
|
165
|
+
"uint8arraylist": "^2.1.0",
|
|
160
166
|
"uint8arrays": "^3.0.0",
|
|
161
167
|
"varint": "^6.0.0"
|
|
162
168
|
},
|
|
163
169
|
"devDependencies": {
|
|
164
|
-
"@libp2p/interface-compliance-tests": "^2.0.0",
|
|
170
|
+
"@libp2p/interface-record-compliance-tests": "^2.0.0",
|
|
165
171
|
"@libp2p/peer-id-factory": "^1.0.0",
|
|
166
172
|
"@types/varint": "^6.0.0",
|
|
167
|
-
"aegir": "^37.0
|
|
168
|
-
"protons": "^
|
|
173
|
+
"aegir": "^37.3.0",
|
|
174
|
+
"protons": "^4.0.1",
|
|
169
175
|
"sinon": "^14.0.0"
|
|
170
176
|
}
|
|
171
177
|
}
|
package/src/envelope/envelope.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
|
|
5
5
|
import type { Codec } from 'protons-runtime'
|
|
6
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
7
|
|
|
7
8
|
export interface Envelope {
|
|
8
9
|
publicKey: Uint8Array
|
|
@@ -21,11 +22,11 @@ export namespace Envelope {
|
|
|
21
22
|
})
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export const encode = (obj: Envelope):
|
|
25
|
+
export const encode = (obj: Envelope): Uint8ArrayList => {
|
|
25
26
|
return encodeMessage(obj, Envelope.codec())
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
export const decode = (buf: Uint8Array): Envelope => {
|
|
29
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): Envelope => {
|
|
29
30
|
return decodeMessage(buf, Envelope.codec())
|
|
30
31
|
}
|
|
31
32
|
}
|
package/src/envelope/index.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import errCode from 'err-code'
|
|
2
|
-
import { concat as uint8arraysConcat } from 'uint8arrays/concat'
|
|
3
2
|
import { fromString as uint8arraysFromString } from 'uint8arrays/from-string'
|
|
4
3
|
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
5
|
-
import varint from 'varint'
|
|
6
|
-
import { equals as uint8arraysEquals } from 'uint8arrays/equals'
|
|
7
4
|
import { codes } from '../errors.js'
|
|
8
5
|
import { Envelope as Protobuf } from './envelope.js'
|
|
9
6
|
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
10
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
11
8
|
import type { Record, Envelope } from '@libp2p/interface-record'
|
|
9
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
10
|
+
import { unsigned } from 'uint8-varint'
|
|
12
11
|
|
|
13
12
|
export interface EnvelopeInit {
|
|
14
13
|
peerId: PeerId
|
|
15
14
|
payloadType: Uint8Array
|
|
16
|
-
payload: Uint8Array
|
|
15
|
+
payload: Uint8Array | Uint8ArrayList
|
|
17
16
|
signature: Uint8Array
|
|
18
17
|
}
|
|
19
18
|
|
|
@@ -21,7 +20,7 @@ export class RecordEnvelope implements Envelope {
|
|
|
21
20
|
/**
|
|
22
21
|
* Unmarshal a serialized Envelope protobuf message
|
|
23
22
|
*/
|
|
24
|
-
static createFromProtobuf = async (data: Uint8Array) => {
|
|
23
|
+
static createFromProtobuf = async (data: Uint8Array | Uint8ArrayList) => {
|
|
25
24
|
const envelopeData = Protobuf.decode(data)
|
|
26
25
|
const peerId = await peerIdFromKeys(envelopeData.publicKey)
|
|
27
26
|
|
|
@@ -38,18 +37,16 @@ export class RecordEnvelope implements Envelope {
|
|
|
38
37
|
* and signs it with the given peerId's private key
|
|
39
38
|
*/
|
|
40
39
|
static seal = async (record: Record, peerId: PeerId) => {
|
|
41
|
-
const domain = record.domain
|
|
42
|
-
const payloadType = record.codec
|
|
43
|
-
const payload = record.marshal()
|
|
44
|
-
|
|
45
|
-
const signData = formatSignaturePayload(domain, payloadType, payload)
|
|
46
|
-
|
|
47
40
|
if (peerId.privateKey == null) {
|
|
48
41
|
throw new Error('Missing private key')
|
|
49
42
|
}
|
|
50
43
|
|
|
44
|
+
const domain = record.domain
|
|
45
|
+
const payloadType = record.codec
|
|
46
|
+
const payload = record.marshal()
|
|
47
|
+
const signData = formatSignaturePayload(domain, payloadType, payload)
|
|
51
48
|
const key = await unmarshalPrivateKey(peerId.privateKey)
|
|
52
|
-
const signature = await key.sign(signData)
|
|
49
|
+
const signature = await key.sign(signData.subarray())
|
|
53
50
|
|
|
54
51
|
return new RecordEnvelope({
|
|
55
52
|
peerId,
|
|
@@ -63,7 +60,7 @@ export class RecordEnvelope implements Envelope {
|
|
|
63
60
|
* Open and certify a given marshalled envelope.
|
|
64
61
|
* Data is unmarshalled and the signature validated for the given domain.
|
|
65
62
|
*/
|
|
66
|
-
static openAndCertify = async (data: Uint8Array, domain: string) => {
|
|
63
|
+
static openAndCertify = async (data: Uint8Array | Uint8ArrayList, domain: string) => {
|
|
67
64
|
const envelope = await RecordEnvelope.createFromProtobuf(data)
|
|
68
65
|
const valid = await envelope.validate(domain)
|
|
69
66
|
|
|
@@ -76,9 +73,9 @@ export class RecordEnvelope implements Envelope {
|
|
|
76
73
|
|
|
77
74
|
public peerId: PeerId
|
|
78
75
|
public payloadType: Uint8Array
|
|
79
|
-
public payload: Uint8Array
|
|
76
|
+
public payload: Uint8Array | Uint8ArrayList
|
|
80
77
|
public signature: Uint8Array
|
|
81
|
-
public marshaled?:
|
|
78
|
+
public marshaled?: Uint8ArrayList
|
|
82
79
|
|
|
83
80
|
/**
|
|
84
81
|
* The Envelope is responsible for keeping an arbitrary signed record
|
|
@@ -96,7 +93,7 @@ export class RecordEnvelope implements Envelope {
|
|
|
96
93
|
/**
|
|
97
94
|
* Marshal the envelope content
|
|
98
95
|
*/
|
|
99
|
-
marshal () {
|
|
96
|
+
marshal (): Uint8ArrayList {
|
|
100
97
|
if (this.peerId.publicKey == null) {
|
|
101
98
|
throw new Error('Missing public key')
|
|
102
99
|
}
|
|
@@ -105,7 +102,7 @@ export class RecordEnvelope implements Envelope {
|
|
|
105
102
|
this.marshaled = Protobuf.encode({
|
|
106
103
|
publicKey: this.peerId.publicKey,
|
|
107
104
|
payloadType: this.payloadType,
|
|
108
|
-
payload: this.payload,
|
|
105
|
+
payload: this.payload.subarray(),
|
|
109
106
|
signature: this.signature
|
|
110
107
|
})
|
|
111
108
|
}
|
|
@@ -117,7 +114,7 @@ export class RecordEnvelope implements Envelope {
|
|
|
117
114
|
* Verifies if the other Envelope is identical to this one
|
|
118
115
|
*/
|
|
119
116
|
equals (other: Envelope) {
|
|
120
|
-
return
|
|
117
|
+
return this.marshal().equals(other.marshal())
|
|
121
118
|
}
|
|
122
119
|
|
|
123
120
|
/**
|
|
@@ -132,14 +129,14 @@ export class RecordEnvelope implements Envelope {
|
|
|
132
129
|
|
|
133
130
|
const key = unmarshalPublicKey(this.peerId.publicKey)
|
|
134
131
|
|
|
135
|
-
return await key.verify(signData, this.signature)
|
|
132
|
+
return await key.verify(signData.subarray(), this.signature)
|
|
136
133
|
}
|
|
137
134
|
}
|
|
138
135
|
|
|
139
136
|
/**
|
|
140
137
|
* Helper function that prepares a Uint8Array to sign or verify a signature
|
|
141
138
|
*/
|
|
142
|
-
const formatSignaturePayload = (domain: string, payloadType: Uint8Array, payload: Uint8Array) => {
|
|
139
|
+
const formatSignaturePayload = (domain: string, payloadType: Uint8Array, payload: Uint8Array | Uint8ArrayList): Uint8ArrayList => {
|
|
143
140
|
// When signing, a peer will prepare a Uint8Array by concatenating the following:
|
|
144
141
|
// - The length of the domain separation string string in bytes
|
|
145
142
|
// - The domain separation string, encoded as UTF-8
|
|
@@ -149,16 +146,16 @@ const formatSignaturePayload = (domain: string, payloadType: Uint8Array, payload
|
|
|
149
146
|
// - The value of the payload field
|
|
150
147
|
|
|
151
148
|
const domainUint8Array = uint8arraysFromString(domain)
|
|
152
|
-
const domainLength =
|
|
153
|
-
const payloadTypeLength =
|
|
154
|
-
const payloadLength =
|
|
149
|
+
const domainLength = unsigned.encode(domainUint8Array.byteLength)
|
|
150
|
+
const payloadTypeLength = unsigned.encode(payloadType.length)
|
|
151
|
+
const payloadLength = unsigned.encode(payload.length)
|
|
155
152
|
|
|
156
|
-
return
|
|
157
|
-
|
|
153
|
+
return new Uint8ArrayList(
|
|
154
|
+
domainLength,
|
|
158
155
|
domainUint8Array,
|
|
159
|
-
|
|
156
|
+
payloadTypeLength,
|
|
160
157
|
payloadType,
|
|
161
|
-
|
|
158
|
+
payloadLength,
|
|
162
159
|
payload
|
|
163
|
-
|
|
160
|
+
)
|
|
164
161
|
}
|
package/src/peer-record/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
ENVELOPE_DOMAIN_PEER_RECORD,
|
|
8
8
|
ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
|
|
9
9
|
} from './consts.js'
|
|
10
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
10
11
|
|
|
11
12
|
export interface PeerRecordInit {
|
|
12
13
|
peerId: PeerId
|
|
@@ -30,7 +31,7 @@ export class PeerRecord {
|
|
|
30
31
|
/**
|
|
31
32
|
* Unmarshal Peer Record Protobuf
|
|
32
33
|
*/
|
|
33
|
-
static createFromProtobuf = (buf: Uint8Array): PeerRecord => {
|
|
34
|
+
static createFromProtobuf = (buf: Uint8Array | Uint8ArrayList): PeerRecord => {
|
|
34
35
|
const peerRecord = Protobuf.decode(buf)
|
|
35
36
|
const peerId = peerIdFromBytes(peerRecord.peerId)
|
|
36
37
|
const multiaddrs = (peerRecord.addresses ?? []).map((a) => new Multiaddr(a.multiaddr))
|
|
@@ -47,7 +48,7 @@ export class PeerRecord {
|
|
|
47
48
|
public seqNumber: bigint
|
|
48
49
|
public domain = PeerRecord.DOMAIN
|
|
49
50
|
public codec = PeerRecord.CODEC
|
|
50
|
-
private marshaled?:
|
|
51
|
+
private marshaled?: Uint8ArrayList
|
|
51
52
|
|
|
52
53
|
constructor (init: PeerRecordInit) {
|
|
53
54
|
const { peerId, multiaddrs, seqNumber } = init
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { encodeMessage, decodeMessage, message, bytes, uint64 } from 'protons-runtime'
|
|
5
5
|
import type { Codec } from 'protons-runtime'
|
|
6
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
7
|
|
|
7
8
|
export interface PeerRecord {
|
|
8
9
|
peerId: Uint8Array
|
|
@@ -22,11 +23,11 @@ export namespace PeerRecord {
|
|
|
22
23
|
})
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
export const encode = (obj: AddressInfo):
|
|
26
|
+
export const encode = (obj: AddressInfo): Uint8ArrayList => {
|
|
26
27
|
return encodeMessage(obj, AddressInfo.codec())
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
export const decode = (buf: Uint8Array): AddressInfo => {
|
|
30
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): AddressInfo => {
|
|
30
31
|
return decodeMessage(buf, AddressInfo.codec())
|
|
31
32
|
}
|
|
32
33
|
}
|
|
@@ -39,11 +40,11 @@ export namespace PeerRecord {
|
|
|
39
40
|
})
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
export const encode = (obj: PeerRecord):
|
|
43
|
+
export const encode = (obj: PeerRecord): Uint8ArrayList => {
|
|
43
44
|
return encodeMessage(obj, PeerRecord.codec())
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
export const decode = (buf: Uint8Array): PeerRecord => {
|
|
47
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList): PeerRecord => {
|
|
47
48
|
return decodeMessage(buf, PeerRecord.codec())
|
|
48
49
|
}
|
|
49
50
|
}
|