@libp2p/peer-record 7.0.25-dd7b329c4 → 7.0.25-e1ca9cced
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/README.md +9 -6
- package/dist/index.min.js +2 -3
- package/dist/src/envelope/envelope.d.ts +2 -2
- package/dist/src/envelope/envelope.d.ts.map +1 -1
- package/dist/src/envelope/envelope.js +19 -13
- package/dist/src/envelope/envelope.js.map +1 -1
- package/dist/src/envelope/errors.d.ts +7 -0
- package/dist/src/envelope/errors.d.ts.map +1 -0
- package/dist/src/envelope/errors.js +10 -0
- package/dist/src/envelope/errors.js.map +1 -0
- package/dist/src/envelope/index.d.ts +4 -4
- package/dist/src/envelope/index.d.ts.map +1 -1
- package/dist/src/envelope/index.js +14 -24
- package/dist/src/envelope/index.js.map +1 -1
- package/dist/src/index.d.ts +9 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/peer-record/index.d.ts.map +1 -1
- package/dist/src/peer-record/index.js +4 -3
- package/dist/src/peer-record/index.js.map +1 -1
- package/dist/src/peer-record/peer-record.d.ts +3 -3
- package/dist/src/peer-record/peer-record.d.ts.map +1 -1
- package/dist/src/peer-record/peer-record.js +28 -16
- package/dist/src/peer-record/peer-record.js.map +1 -1
- package/package.json +6 -6
- package/src/envelope/envelope.ts +19 -14
- package/src/envelope/errors.ts +9 -0
- package/src/envelope/index.ts +16 -29
- package/src/index.ts +9 -6
- package/src/peer-record/index.ts +4 -3
- package/src/peer-record/peer-record.ts +29 -17
- package/dist/src/errors.d.ts +0 -4
- package/dist/src/errors.d.ts.map +0 -1
- package/dist/src/errors.js +0 -4
- package/dist/src/errors.js.map +0 -1
- package/src/errors.ts +0 -3
package/src/envelope/envelope.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
5
|
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
7
|
+
import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, message } from 'protons-runtime'
|
|
8
|
+
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
9
9
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
10
10
|
|
|
11
11
|
export interface Envelope {
|
|
@@ -48,12 +48,12 @@ export namespace Envelope {
|
|
|
48
48
|
if (opts.lengthDelimited !== false) {
|
|
49
49
|
w.ldelim()
|
|
50
50
|
}
|
|
51
|
-
}, (reader, length) => {
|
|
51
|
+
}, (reader, length, opts = {}) => {
|
|
52
52
|
const obj: any = {
|
|
53
|
-
publicKey:
|
|
54
|
-
payloadType:
|
|
55
|
-
payload:
|
|
56
|
-
signature:
|
|
53
|
+
publicKey: uint8ArrayAlloc(0),
|
|
54
|
+
payloadType: uint8ArrayAlloc(0),
|
|
55
|
+
payload: uint8ArrayAlloc(0),
|
|
56
|
+
signature: uint8ArrayAlloc(0)
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
const end = length == null ? reader.len : reader.pos + length
|
|
@@ -62,21 +62,26 @@ export namespace Envelope {
|
|
|
62
62
|
const tag = reader.uint32()
|
|
63
63
|
|
|
64
64
|
switch (tag >>> 3) {
|
|
65
|
-
case 1:
|
|
65
|
+
case 1: {
|
|
66
66
|
obj.publicKey = reader.bytes()
|
|
67
67
|
break
|
|
68
|
-
|
|
68
|
+
}
|
|
69
|
+
case 2: {
|
|
69
70
|
obj.payloadType = reader.bytes()
|
|
70
71
|
break
|
|
71
|
-
|
|
72
|
+
}
|
|
73
|
+
case 3: {
|
|
72
74
|
obj.payload = reader.bytes()
|
|
73
75
|
break
|
|
74
|
-
|
|
76
|
+
}
|
|
77
|
+
case 5: {
|
|
75
78
|
obj.signature = reader.bytes()
|
|
76
79
|
break
|
|
77
|
-
|
|
80
|
+
}
|
|
81
|
+
default: {
|
|
78
82
|
reader.skipType(tag & 7)
|
|
79
83
|
break
|
|
84
|
+
}
|
|
80
85
|
}
|
|
81
86
|
}
|
|
82
87
|
|
|
@@ -91,7 +96,7 @@ export namespace Envelope {
|
|
|
91
96
|
return encodeMessage(obj, Envelope.codec())
|
|
92
97
|
}
|
|
93
98
|
|
|
94
|
-
export const decode = (buf: Uint8Array | Uint8ArrayList): Envelope => {
|
|
95
|
-
return decodeMessage(buf, Envelope.codec())
|
|
99
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<Envelope>): Envelope => {
|
|
100
|
+
return decodeMessage(buf, Envelope.codec(), opts)
|
|
96
101
|
}
|
|
97
102
|
}
|
package/src/envelope/index.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CodeError } from '@libp2p/interface'
|
|
3
|
-
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
1
|
+
import { publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
|
|
4
2
|
import * as varint from 'uint8-varint'
|
|
5
3
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
6
4
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
7
5
|
import { fromString as uint8arraysFromString } from 'uint8arrays/from-string'
|
|
8
|
-
import { codes } from '../errors.js'
|
|
9
6
|
import { Envelope as Protobuf } from './envelope.js'
|
|
10
|
-
import
|
|
7
|
+
import { InvalidSignatureError } from './errors.js'
|
|
8
|
+
import type { Record, Envelope, PrivateKey, PublicKey } from '@libp2p/interface'
|
|
11
9
|
|
|
12
10
|
export interface RecordEnvelopeInit {
|
|
13
|
-
|
|
11
|
+
publicKey: PublicKey
|
|
14
12
|
payloadType: Uint8Array
|
|
15
13
|
payload: Uint8Array
|
|
16
14
|
signature: Uint8Array
|
|
@@ -22,10 +20,10 @@ export class RecordEnvelope implements Envelope {
|
|
|
22
20
|
*/
|
|
23
21
|
static createFromProtobuf = async (data: Uint8Array | Uint8ArrayList): Promise<RecordEnvelope> => {
|
|
24
22
|
const envelopeData = Protobuf.decode(data)
|
|
25
|
-
const
|
|
23
|
+
const publicKey = publicKeyFromProtobuf(envelopeData.publicKey)
|
|
26
24
|
|
|
27
25
|
return new RecordEnvelope({
|
|
28
|
-
|
|
26
|
+
publicKey,
|
|
29
27
|
payloadType: envelopeData.payloadType,
|
|
30
28
|
payload: envelopeData.payload,
|
|
31
29
|
signature: envelopeData.signature
|
|
@@ -36,8 +34,8 @@ export class RecordEnvelope implements Envelope {
|
|
|
36
34
|
* Seal marshals the given Record, places the marshaled bytes inside an Envelope
|
|
37
35
|
* and signs it with the given peerId's private key
|
|
38
36
|
*/
|
|
39
|
-
static seal = async (record: Record,
|
|
40
|
-
if (
|
|
37
|
+
static seal = async (record: Record, privateKey: PrivateKey): Promise<RecordEnvelope> => {
|
|
38
|
+
if (privateKey == null) {
|
|
41
39
|
throw new Error('Missing private key')
|
|
42
40
|
}
|
|
43
41
|
|
|
@@ -45,11 +43,10 @@ export class RecordEnvelope implements Envelope {
|
|
|
45
43
|
const payloadType = record.codec
|
|
46
44
|
const payload = record.marshal()
|
|
47
45
|
const signData = formatSignaturePayload(domain, payloadType, payload)
|
|
48
|
-
const
|
|
49
|
-
const signature = await key.sign(signData.subarray())
|
|
46
|
+
const signature = await privateKey.sign(signData.subarray())
|
|
50
47
|
|
|
51
48
|
return new RecordEnvelope({
|
|
52
|
-
|
|
49
|
+
publicKey: privateKey.publicKey,
|
|
53
50
|
payloadType,
|
|
54
51
|
payload,
|
|
55
52
|
signature
|
|
@@ -65,13 +62,13 @@ export class RecordEnvelope implements Envelope {
|
|
|
65
62
|
const valid = await envelope.validate(domain)
|
|
66
63
|
|
|
67
64
|
if (!valid) {
|
|
68
|
-
throw new
|
|
65
|
+
throw new InvalidSignatureError('Envelope signature is not valid for the given domain')
|
|
69
66
|
}
|
|
70
67
|
|
|
71
68
|
return envelope
|
|
72
69
|
}
|
|
73
70
|
|
|
74
|
-
public
|
|
71
|
+
public publicKey: PublicKey
|
|
75
72
|
public payloadType: Uint8Array
|
|
76
73
|
public payload: Uint8Array
|
|
77
74
|
public signature: Uint8Array
|
|
@@ -82,9 +79,9 @@ export class RecordEnvelope implements Envelope {
|
|
|
82
79
|
* by a libp2p peer.
|
|
83
80
|
*/
|
|
84
81
|
constructor (init: RecordEnvelopeInit) {
|
|
85
|
-
const {
|
|
82
|
+
const { publicKey, payloadType, payload, signature } = init
|
|
86
83
|
|
|
87
|
-
this.
|
|
84
|
+
this.publicKey = publicKey
|
|
88
85
|
this.payloadType = payloadType
|
|
89
86
|
this.payload = payload
|
|
90
87
|
this.signature = signature
|
|
@@ -94,13 +91,9 @@ export class RecordEnvelope implements Envelope {
|
|
|
94
91
|
* Marshal the envelope content
|
|
95
92
|
*/
|
|
96
93
|
marshal (): Uint8Array {
|
|
97
|
-
if (this.peerId.publicKey == null) {
|
|
98
|
-
throw new Error('Missing public key')
|
|
99
|
-
}
|
|
100
|
-
|
|
101
94
|
if (this.marshaled == null) {
|
|
102
95
|
this.marshaled = Protobuf.encode({
|
|
103
|
-
publicKey: this.
|
|
96
|
+
publicKey: publicKeyToProtobuf(this.publicKey),
|
|
104
97
|
payloadType: this.payloadType,
|
|
105
98
|
payload: this.payload.subarray(),
|
|
106
99
|
signature: this.signature
|
|
@@ -123,13 +116,7 @@ export class RecordEnvelope implements Envelope {
|
|
|
123
116
|
async validate (domain: string): Promise<boolean> {
|
|
124
117
|
const signData = formatSignaturePayload(domain, this.payloadType, this.payload)
|
|
125
118
|
|
|
126
|
-
|
|
127
|
-
throw new Error('Missing public key')
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const key = unmarshalPublicKey(this.peerId.publicKey)
|
|
131
|
-
|
|
132
|
-
return key.verify(signData.subarray(), this.signature)
|
|
119
|
+
return this.publicKey.verify(signData.subarray(), this.signature)
|
|
133
120
|
}
|
|
134
121
|
}
|
|
135
122
|
|
package/src/index.ts
CHANGED
|
@@ -17,16 +17,18 @@
|
|
|
17
17
|
*
|
|
18
18
|
* ```TypeScript
|
|
19
19
|
* import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record'
|
|
20
|
-
* import {
|
|
20
|
+
* import { generateKeyPair } from '@libp2p/crypto/keys'
|
|
21
|
+
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
|
|
21
22
|
*
|
|
22
|
-
* const
|
|
23
|
+
* const privateKey = await generateKeyPair('Ed25519')
|
|
24
|
+
* const peerId = peerIdFromPrivateKey(privateKey)
|
|
23
25
|
*
|
|
24
26
|
* const record = new PeerRecord({
|
|
25
|
-
*
|
|
27
|
+
* peerId,
|
|
26
28
|
* // ...other data
|
|
27
29
|
* })
|
|
28
30
|
*
|
|
29
|
-
* const envelope = await RecordEnvelope.seal(record,
|
|
31
|
+
* const envelope = await RecordEnvelope.seal(record, privateKey)
|
|
30
32
|
* const wireData = envelope.marshal()
|
|
31
33
|
* ```
|
|
32
34
|
*
|
|
@@ -59,10 +61,11 @@
|
|
|
59
61
|
*
|
|
60
62
|
* ```TypeScript
|
|
61
63
|
* import { PeerRecord } from '@libp2p/peer-record'
|
|
62
|
-
* import {
|
|
64
|
+
* import { peerIdFromPrivateKey } from '@libp2p/peer-id'
|
|
65
|
+
import { generateKeyPair } from '@libp2p/crypto/keys'
|
|
63
66
|
* import { multiaddr } from '@multiformats/multiaddr'
|
|
64
67
|
*
|
|
65
|
-
* const peerId = await
|
|
68
|
+
* const peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
|
|
66
69
|
*
|
|
67
70
|
* const record = new PeerRecord({
|
|
68
71
|
* peerId: peerId,
|
package/src/peer-record/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { peerIdFromMultihash } from '@libp2p/peer-id'
|
|
2
2
|
import { arrayEquals } from '@libp2p/utils/array-equals'
|
|
3
3
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
4
|
+
import * as Digest from 'multiformats/hashes/digest'
|
|
4
5
|
import {
|
|
5
6
|
ENVELOPE_DOMAIN_PEER_RECORD,
|
|
6
7
|
ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
|
|
@@ -34,7 +35,7 @@ export class PeerRecord {
|
|
|
34
35
|
*/
|
|
35
36
|
static createFromProtobuf = (buf: Uint8Array | Uint8ArrayList): PeerRecord => {
|
|
36
37
|
const peerRecord = Protobuf.decode(buf)
|
|
37
|
-
const peerId =
|
|
38
|
+
const peerId = peerIdFromMultihash(Digest.decode(peerRecord.peerId))
|
|
38
39
|
const multiaddrs = (peerRecord.addresses ?? []).map((a) => multiaddr(a.multiaddr))
|
|
39
40
|
const seqNumber = peerRecord.seq
|
|
40
41
|
|
|
@@ -65,7 +66,7 @@ export class PeerRecord {
|
|
|
65
66
|
marshal (): Uint8Array {
|
|
66
67
|
if (this.marshaled == null) {
|
|
67
68
|
this.marshaled = Protobuf.encode({
|
|
68
|
-
peerId: this.peerId.
|
|
69
|
+
peerId: this.peerId.toMultihash().bytes,
|
|
69
70
|
seq: BigInt(this.seqNumber),
|
|
70
71
|
addresses: this.multiaddrs.map((m) => ({
|
|
71
72
|
multiaddr: m.bytes
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
5
|
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
7
|
+
import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, MaxLengthError, message } from 'protons-runtime'
|
|
8
|
+
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
9
9
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
10
10
|
|
|
11
11
|
export interface PeerRecord {
|
|
@@ -37,9 +37,9 @@ export namespace PeerRecord {
|
|
|
37
37
|
if (opts.lengthDelimited !== false) {
|
|
38
38
|
w.ldelim()
|
|
39
39
|
}
|
|
40
|
-
}, (reader, length) => {
|
|
40
|
+
}, (reader, length, opts = {}) => {
|
|
41
41
|
const obj: any = {
|
|
42
|
-
multiaddr:
|
|
42
|
+
multiaddr: uint8ArrayAlloc(0)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const end = length == null ? reader.len : reader.pos + length
|
|
@@ -48,12 +48,14 @@ export namespace PeerRecord {
|
|
|
48
48
|
const tag = reader.uint32()
|
|
49
49
|
|
|
50
50
|
switch (tag >>> 3) {
|
|
51
|
-
case 1:
|
|
51
|
+
case 1: {
|
|
52
52
|
obj.multiaddr = reader.bytes()
|
|
53
53
|
break
|
|
54
|
-
|
|
54
|
+
}
|
|
55
|
+
default: {
|
|
55
56
|
reader.skipType(tag & 7)
|
|
56
57
|
break
|
|
58
|
+
}
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
@@ -68,8 +70,8 @@ export namespace PeerRecord {
|
|
|
68
70
|
return encodeMessage(obj, AddressInfo.codec())
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
export const decode = (buf: Uint8Array | Uint8ArrayList): AddressInfo => {
|
|
72
|
-
return decodeMessage(buf, AddressInfo.codec())
|
|
73
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<AddressInfo>): AddressInfo => {
|
|
74
|
+
return decodeMessage(buf, AddressInfo.codec(), opts)
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
|
|
@@ -102,9 +104,9 @@ export namespace PeerRecord {
|
|
|
102
104
|
if (opts.lengthDelimited !== false) {
|
|
103
105
|
w.ldelim()
|
|
104
106
|
}
|
|
105
|
-
}, (reader, length) => {
|
|
107
|
+
}, (reader, length, opts = {}) => {
|
|
106
108
|
const obj: any = {
|
|
107
|
-
peerId:
|
|
109
|
+
peerId: uint8ArrayAlloc(0),
|
|
108
110
|
seq: 0n,
|
|
109
111
|
addresses: []
|
|
110
112
|
}
|
|
@@ -115,18 +117,28 @@ export namespace PeerRecord {
|
|
|
115
117
|
const tag = reader.uint32()
|
|
116
118
|
|
|
117
119
|
switch (tag >>> 3) {
|
|
118
|
-
case 1:
|
|
120
|
+
case 1: {
|
|
119
121
|
obj.peerId = reader.bytes()
|
|
120
122
|
break
|
|
121
|
-
|
|
123
|
+
}
|
|
124
|
+
case 2: {
|
|
122
125
|
obj.seq = reader.uint64()
|
|
123
126
|
break
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
}
|
|
128
|
+
case 3: {
|
|
129
|
+
if (opts.limits?.addresses != null && obj.addresses.length === opts.limits.addresses) {
|
|
130
|
+
throw new MaxLengthError('Decode error - map field "addresses" had too many elements')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
obj.addresses.push(PeerRecord.AddressInfo.codec().decode(reader, reader.uint32(), {
|
|
134
|
+
limits: opts.limits?.addresses$
|
|
135
|
+
}))
|
|
126
136
|
break
|
|
127
|
-
|
|
137
|
+
}
|
|
138
|
+
default: {
|
|
128
139
|
reader.skipType(tag & 7)
|
|
129
140
|
break
|
|
141
|
+
}
|
|
130
142
|
}
|
|
131
143
|
}
|
|
132
144
|
|
|
@@ -141,7 +153,7 @@ export namespace PeerRecord {
|
|
|
141
153
|
return encodeMessage(obj, PeerRecord.codec())
|
|
142
154
|
}
|
|
143
155
|
|
|
144
|
-
export const decode = (buf: Uint8Array | Uint8ArrayList): PeerRecord => {
|
|
145
|
-
return decodeMessage(buf, PeerRecord.codec())
|
|
156
|
+
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<PeerRecord>): PeerRecord => {
|
|
157
|
+
return decodeMessage(buf, PeerRecord.codec(), opts)
|
|
146
158
|
}
|
|
147
159
|
}
|
package/dist/src/errors.d.ts
DELETED
package/dist/src/errors.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK;;CAEjB,CAAA"}
|
package/dist/src/errors.js
DELETED
package/dist/src/errors.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,uBAAuB,EAAE,yBAAyB;CACnD,CAAA"}
|
package/src/errors.ts
DELETED