@libp2p/peer-record
Advanced tools
Comparing version 5.0.3 to 5.0.4
@@ -0,4 +1,4 @@ | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import type { PeerId } from '@libp2p/interface-peer-id'; | ||
import type { Record, Envelope } from '@libp2p/interface-record'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
export interface RecordEnvelopeInit { | ||
@@ -5,0 +5,0 @@ peerId: PeerId; |
@@ -1,13 +0,63 @@ | ||
var _a; | ||
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys'; | ||
import { CodeError } from '@libp2p/interfaces/errors'; | ||
import { peerIdFromKeys } from '@libp2p/peer-id'; | ||
import { unsigned } from 'uint8-varint'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'; | ||
import { fromString as uint8arraysFromString } from 'uint8arrays/from-string'; | ||
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'; | ||
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys'; | ||
import { codes } from '../errors.js'; | ||
import { Envelope as Protobuf } from './envelope.js'; | ||
import { peerIdFromKeys } from '@libp2p/peer-id'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { unsigned } from 'uint8-varint'; | ||
export class RecordEnvelope { | ||
/** | ||
* Unmarshal a serialized Envelope protobuf message | ||
*/ | ||
static createFromProtobuf = async (data) => { | ||
const envelopeData = Protobuf.decode(data); | ||
const peerId = await peerIdFromKeys(envelopeData.publicKey); | ||
return new RecordEnvelope({ | ||
peerId, | ||
payloadType: envelopeData.payloadType, | ||
payload: envelopeData.payload, | ||
signature: envelopeData.signature | ||
}); | ||
}; | ||
/** | ||
* Seal marshals the given Record, places the marshaled bytes inside an Envelope | ||
* and signs it with the given peerId's private key | ||
*/ | ||
static seal = async (record, peerId) => { | ||
if (peerId.privateKey == null) { | ||
throw new Error('Missing private key'); | ||
} | ||
const domain = record.domain; | ||
const payloadType = record.codec; | ||
const payload = record.marshal(); | ||
const signData = formatSignaturePayload(domain, payloadType, payload); | ||
const key = await unmarshalPrivateKey(peerId.privateKey); | ||
const signature = await key.sign(signData.subarray()); | ||
return new RecordEnvelope({ | ||
peerId, | ||
payloadType, | ||
payload, | ||
signature | ||
}); | ||
}; | ||
/** | ||
* Open and certify a given marshalled envelope. | ||
* Data is unmarshalled and the signature validated for the given domain. | ||
*/ | ||
static openAndCertify = async (data, domain) => { | ||
const envelope = await RecordEnvelope.createFromProtobuf(data); | ||
const valid = await envelope.validate(domain); | ||
if (!valid) { | ||
throw new CodeError('envelope signature is not valid for the given domain', codes.ERR_SIGNATURE_NOT_VALID); | ||
} | ||
return envelope; | ||
}; | ||
peerId; | ||
payloadType; | ||
payload; | ||
signature; | ||
marshaled; | ||
/** | ||
* The Envelope is responsible for keeping an arbitrary signed record | ||
@@ -55,53 +105,6 @@ * by a libp2p peer. | ||
const key = unmarshalPublicKey(this.peerId.publicKey); | ||
return await key.verify(signData.subarray(), this.signature); | ||
return key.verify(signData.subarray(), this.signature); | ||
} | ||
} | ||
_a = RecordEnvelope; | ||
/** | ||
* Unmarshal a serialized Envelope protobuf message | ||
*/ | ||
RecordEnvelope.createFromProtobuf = async (data) => { | ||
const envelopeData = Protobuf.decode(data); | ||
const peerId = await peerIdFromKeys(envelopeData.publicKey); | ||
return new RecordEnvelope({ | ||
peerId, | ||
payloadType: envelopeData.payloadType, | ||
payload: envelopeData.payload, | ||
signature: envelopeData.signature | ||
}); | ||
}; | ||
/** | ||
* Seal marshals the given Record, places the marshaled bytes inside an Envelope | ||
* and signs it with the given peerId's private key | ||
*/ | ||
RecordEnvelope.seal = async (record, peerId) => { | ||
if (peerId.privateKey == null) { | ||
throw new Error('Missing private key'); | ||
} | ||
const domain = record.domain; | ||
const payloadType = record.codec; | ||
const payload = record.marshal(); | ||
const signData = formatSignaturePayload(domain, payloadType, payload); | ||
const key = await unmarshalPrivateKey(peerId.privateKey); | ||
const signature = await key.sign(signData.subarray()); | ||
return new RecordEnvelope({ | ||
peerId, | ||
payloadType, | ||
payload, | ||
signature | ||
}); | ||
}; | ||
/** | ||
* Open and certify a given marshalled envelope. | ||
* Data is unmarshalled and the signature validated for the given domain. | ||
*/ | ||
RecordEnvelope.openAndCertify = async (data, domain) => { | ||
const envelope = await RecordEnvelope.createFromProtobuf(data); | ||
const valid = await envelope.validate(domain); | ||
if (!valid) { | ||
throw new CodeError('envelope signature is not valid for the given domain', codes.ERR_SIGNATURE_NOT_VALID); | ||
} | ||
return envelope; | ||
}; | ||
/** | ||
* Helper function that prepares a Uint8Array to sign or verify a signature | ||
@@ -108,0 +111,0 @@ */ |
@@ -0,3 +1,3 @@ | ||
import type { PeerId } from '@libp2p/interface-peer-id'; | ||
import type { Multiaddr } from '@multiformats/multiaddr'; | ||
import type { PeerId } from '@libp2p/interface-peer-id'; | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
@@ -4,0 +4,0 @@ export interface PeerRecordInit { |
@@ -0,6 +1,6 @@ | ||
import { peerIdFromBytes } from '@libp2p/peer-id'; | ||
import { arrayEquals } from '@libp2p/utils/array-equals'; | ||
import { multiaddr } from '@multiformats/multiaddr'; | ||
import { arrayEquals } from '@libp2p/utils/array-equals'; | ||
import { peerIdFromBytes } from '@libp2p/peer-id'; | ||
import { ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD } from './consts.js'; | ||
import { PeerRecord as Protobuf } from './peer-record.js'; | ||
import { ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD } from './consts.js'; | ||
/** | ||
@@ -11,5 +11,21 @@ * The PeerRecord is used for distributing peer routing records across the network. | ||
export class PeerRecord { | ||
/** | ||
* Unmarshal Peer Record Protobuf | ||
*/ | ||
static createFromProtobuf = (buf) => { | ||
const peerRecord = Protobuf.decode(buf); | ||
const peerId = peerIdFromBytes(peerRecord.peerId); | ||
const multiaddrs = (peerRecord.addresses ?? []).map((a) => multiaddr(a.multiaddr)); | ||
const seqNumber = peerRecord.seq; | ||
return new PeerRecord({ peerId, multiaddrs, seqNumber }); | ||
}; | ||
static DOMAIN = ENVELOPE_DOMAIN_PEER_RECORD; | ||
static CODEC = ENVELOPE_PAYLOAD_TYPE_PEER_RECORD; | ||
peerId; | ||
multiaddrs; | ||
seqNumber; | ||
domain = PeerRecord.DOMAIN; | ||
codec = PeerRecord.CODEC; | ||
marshaled; | ||
constructor(init) { | ||
this.domain = PeerRecord.DOMAIN; | ||
this.codec = PeerRecord.CODEC; | ||
const { peerId, multiaddrs, seqNumber } = init; | ||
@@ -57,14 +73,2 @@ this.peerId = peerId; | ||
} | ||
/** | ||
* Unmarshal Peer Record Protobuf | ||
*/ | ||
PeerRecord.createFromProtobuf = (buf) => { | ||
const peerRecord = Protobuf.decode(buf); | ||
const peerId = peerIdFromBytes(peerRecord.peerId); | ||
const multiaddrs = (peerRecord.addresses ?? []).map((a) => multiaddr(a.multiaddr)); | ||
const seqNumber = peerRecord.seq; | ||
return new PeerRecord({ peerId, multiaddrs, seqNumber }); | ||
}; | ||
PeerRecord.DOMAIN = ENVELOPE_DOMAIN_PEER_RECORD; | ||
PeerRecord.CODEC = ENVELOPE_PAYLOAD_TYPE_PEER_RECORD; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@libp2p/peer-record", | ||
"version": "5.0.3", | ||
"version": "5.0.4", | ||
"description": "Used to transfer signed peer data across the network", | ||
@@ -165,5 +165,5 @@ "license": "Apache-2.0 OR MIT", | ||
"@types/varint": "^6.0.0", | ||
"aegir": "^38.1.2", | ||
"aegir": "^39.0.10", | ||
"protons": "^7.0.2" | ||
} | ||
} |
@@ -0,12 +1,12 @@ | ||
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys' | ||
import { CodeError } from '@libp2p/interfaces/errors' | ||
import { peerIdFromKeys } from '@libp2p/peer-id' | ||
import { unsigned } from 'uint8-varint' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { equals as uint8ArrayEquals } from 'uint8arrays/equals' | ||
import { fromString as uint8arraysFromString } from 'uint8arrays/from-string' | ||
import { equals as uint8ArrayEquals } from 'uint8arrays/equals' | ||
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys' | ||
import { codes } from '../errors.js' | ||
import { Envelope as Protobuf } from './envelope.js' | ||
import { peerIdFromKeys } from '@libp2p/peer-id' | ||
import type { PeerId } from '@libp2p/interface-peer-id' | ||
import type { Record, Envelope } from '@libp2p/interface-record' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { unsigned } from 'uint8-varint' | ||
@@ -133,3 +133,3 @@ export interface RecordEnvelopeInit { | ||
return await key.verify(signData.subarray(), this.signature) | ||
return key.verify(signData.subarray(), this.signature) | ||
} | ||
@@ -136,0 +136,0 @@ } |
@@ -1,7 +0,4 @@ | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
import type { PeerId } from '@libp2p/interface-peer-id' | ||
import { peerIdFromBytes } from '@libp2p/peer-id' | ||
import { arrayEquals } from '@libp2p/utils/array-equals' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { arrayEquals } from '@libp2p/utils/array-equals' | ||
import { peerIdFromBytes } from '@libp2p/peer-id' | ||
import { PeerRecord as Protobuf } from './peer-record.js' | ||
import { | ||
@@ -11,2 +8,5 @@ ENVELOPE_DOMAIN_PEER_RECORD, | ||
} from './consts.js' | ||
import { PeerRecord as Protobuf } from './peer-record.js' | ||
import type { PeerId } from '@libp2p/interface-peer-id' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
@@ -13,0 +13,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
328238
1984