Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
deps: update protons to 5.1.0 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Aug 11, 2022
1 parent 99fa33a commit 9d2e881
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 33 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"dependencies": {
"@libp2p/crypto": "^1.0.0",
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/interface-record": "^2.0.0",
"@libp2p/interface-record": "^2.0.1",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.13",
"@libp2p/utils": "^3.0.0",
Expand All @@ -160,7 +160,7 @@
"it-map": "^1.0.6",
"it-pipe": "^2.0.3",
"multiformats": "^9.6.3",
"protons-runtime": "^2.0.2",
"protons-runtime": "^3.1.0",
"uint8-varint": "^1.0.2",
"uint8arraylist": "^2.1.0",
"uint8arrays": "^3.0.0",
Expand All @@ -171,7 +171,7 @@
"@libp2p/peer-id-factory": "^1.0.0",
"@types/varint": "^6.0.0",
"aegir": "^37.3.0",
"protons": "^4.0.1",
"protons": "^5.1.0",
"sinon": "^14.0.0"
}
}
104 changes: 95 additions & 9 deletions src/envelope/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable import/export */
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Codec } from 'protons-runtime'

export interface Envelope {
publicKey: Uint8Array
Expand All @@ -13,16 +13,102 @@ export interface Envelope {
}

export namespace Envelope {
let _codec: Codec<Envelope>

export const codec = (): Codec<Envelope> => {
return message<Envelope>({
1: { name: 'publicKey', codec: bytes },
2: { name: 'payloadType', codec: bytes },
3: { name: 'payload', codec: bytes },
5: { name: 'signature', codec: bytes }
})
if (_codec == null) {
_codec = message<Envelope>((obj, writer, opts = {}) => {
if (opts.lengthDelimited !== false) {
writer.fork()
}

if (obj.publicKey != null) {
writer.uint32(10)
writer.bytes(obj.publicKey)
} else {
throw new Error('Protocol error: required field "publicKey" was not found in object')
}

if (obj.payloadType != null) {
writer.uint32(18)
writer.bytes(obj.payloadType)
} else {
throw new Error('Protocol error: required field "payloadType" was not found in object')
}

if (obj.payload != null) {
writer.uint32(26)
writer.bytes(obj.payload)
} else {
throw new Error('Protocol error: required field "payload" was not found in object')
}

if (obj.signature != null) {
writer.uint32(42)
writer.bytes(obj.signature)
} else {
throw new Error('Protocol error: required field "signature" was not found in object')
}

if (opts.lengthDelimited !== false) {
writer.ldelim()
}
}, (reader, length) => {
const obj: any = {
publicKey: new Uint8Array(0),
payloadType: new Uint8Array(0),
payload: new Uint8Array(0),
signature: new Uint8Array(0)
}

const end = length == null ? reader.len : reader.pos + length

while (reader.pos < end) {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
obj.publicKey = reader.bytes()
break
case 2:
obj.payloadType = reader.bytes()
break
case 3:
obj.payload = reader.bytes()
break
case 5:
obj.signature = reader.bytes()
break
default:
reader.skipType(tag & 7)
break
}
}

if (obj.publicKey == null) {
throw new Error('Protocol error: value for required field "publicKey" was not found in protobuf')
}

if (obj.payloadType == null) {
throw new Error('Protocol error: value for required field "payloadType" was not found in protobuf')
}

if (obj.payload == null) {
throw new Error('Protocol error: value for required field "payload" was not found in protobuf')
}

if (obj.signature == null) {
throw new Error('Protocol error: value for required field "signature" was not found in protobuf')
}

return obj
})
}

return _codec
}

export const encode = (obj: Envelope): Uint8ArrayList => {
export const encode = (obj: Envelope): Uint8Array => {
return encodeMessage(obj, Envelope.codec())
}

Expand Down
11 changes: 6 additions & 5 deletions src/envelope/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import errCode from 'err-code'
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'
Expand All @@ -12,7 +13,7 @@ import { unsigned } from 'uint8-varint'
export interface EnvelopeInit {
peerId: PeerId
payloadType: Uint8Array
payload: Uint8Array | Uint8ArrayList
payload: Uint8Array
signature: Uint8Array
}

Expand Down Expand Up @@ -73,9 +74,9 @@ export class RecordEnvelope implements Envelope {

public peerId: PeerId
public payloadType: Uint8Array
public payload: Uint8Array | Uint8ArrayList
public payload: Uint8Array
public signature: Uint8Array
public marshaled?: Uint8ArrayList
public marshaled?: Uint8Array

/**
* The Envelope is responsible for keeping an arbitrary signed record
Expand All @@ -93,7 +94,7 @@ export class RecordEnvelope implements Envelope {
/**
* Marshal the envelope content
*/
marshal (): Uint8ArrayList {
marshal (): Uint8Array {
if (this.peerId.publicKey == null) {
throw new Error('Missing public key')
}
Expand All @@ -114,7 +115,7 @@ export class RecordEnvelope implements Envelope {
* Verifies if the other Envelope is identical to this one
*/
equals (other: Envelope) {
return this.marshal().equals(other.marshal())
return uint8ArrayEquals(this.marshal(), other.marshal())
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/peer-record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class PeerRecord {
public seqNumber: bigint
public domain = PeerRecord.DOMAIN
public codec = PeerRecord.CODEC
private marshaled?: Uint8ArrayList
private marshaled?: Uint8Array

constructor (init: PeerRecordInit) {
const { peerId, multiaddrs, seqNumber } = init
Expand Down
138 changes: 126 additions & 12 deletions src/peer-record/peer-record.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable import/export */
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bytes, uint64 } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Codec } from 'protons-runtime'

export interface PeerRecord {
peerId: Uint8Array
Expand All @@ -17,13 +17,57 @@ export namespace PeerRecord {
}

export namespace AddressInfo {
let _codec: Codec<AddressInfo>

export const codec = (): Codec<AddressInfo> => {
return message<AddressInfo>({
1: { name: 'multiaddr', codec: bytes }
})
if (_codec == null) {
_codec = message<AddressInfo>((obj, writer, opts = {}) => {
if (opts.lengthDelimited !== false) {
writer.fork()
}

if (obj.multiaddr != null) {
writer.uint32(10)
writer.bytes(obj.multiaddr)
} else {
throw new Error('Protocol error: required field "multiaddr" was not found in object')
}

if (opts.lengthDelimited !== false) {
writer.ldelim()
}
}, (reader, length) => {
const obj: any = {
multiaddr: new Uint8Array(0)
}

const end = length == null ? reader.len : reader.pos + length

while (reader.pos < end) {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
obj.multiaddr = reader.bytes()
break
default:
reader.skipType(tag & 7)
break
}
}

if (obj.multiaddr == null) {
throw new Error('Protocol error: value for required field "multiaddr" was not found in protobuf')
}

return obj
})
}

return _codec
}

export const encode = (obj: AddressInfo): Uint8ArrayList => {
export const encode = (obj: AddressInfo): Uint8Array => {
return encodeMessage(obj, AddressInfo.codec())
}

Expand All @@ -32,15 +76,85 @@ export namespace PeerRecord {
}
}

let _codec: Codec<PeerRecord>

export const codec = (): Codec<PeerRecord> => {
return message<PeerRecord>({
1: { name: 'peerId', codec: bytes },
2: { name: 'seq', codec: uint64 },
3: { name: 'addresses', codec: PeerRecord.AddressInfo.codec(), repeats: true }
})
if (_codec == null) {
_codec = message<PeerRecord>((obj, writer, opts = {}) => {
if (opts.lengthDelimited !== false) {
writer.fork()
}

if (obj.peerId != null) {
writer.uint32(10)
writer.bytes(obj.peerId)
} else {
throw new Error('Protocol error: required field "peerId" was not found in object')
}

if (obj.seq != null) {
writer.uint32(16)
writer.uint64(obj.seq)
} else {
throw new Error('Protocol error: required field "seq" was not found in object')
}

if (obj.addresses != null) {
for (const value of obj.addresses) {
writer.uint32(26)
PeerRecord.AddressInfo.codec().encode(value, writer)
}
} else {
throw new Error('Protocol error: required field "addresses" was not found in object')
}

if (opts.lengthDelimited !== false) {
writer.ldelim()
}
}, (reader, length) => {
const obj: any = {
peerId: new Uint8Array(0),
seq: 0n,
addresses: []
}

const end = length == null ? reader.len : reader.pos + length

while (reader.pos < end) {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
obj.peerId = reader.bytes()
break
case 2:
obj.seq = reader.uint64()
break
case 3:
obj.addresses.push(PeerRecord.AddressInfo.codec().decode(reader, reader.uint32()))
break
default:
reader.skipType(tag & 7)
break
}
}

if (obj.peerId == null) {
throw new Error('Protocol error: value for required field "peerId" was not found in protobuf')
}

if (obj.seq == null) {
throw new Error('Protocol error: value for required field "seq" was not found in protobuf')
}

return obj
})
}

return _codec
}

export const encode = (obj: PeerRecord): Uint8ArrayList => {
export const encode = (obj: PeerRecord): Uint8Array => {
return encodeMessage(obj, PeerRecord.codec())
}

Expand Down
6 changes: 3 additions & 3 deletions test/envelope.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect } from 'aegir/chai'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { RecordEnvelope } from '../src/envelope/index.js'
import { codes as ErrorCodes } from '../src/errors.js'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import type { Record } from '@libp2p/interface-record'
import type { PeerId } from '@libp2p/interface-peer-id'
import { Uint8ArrayList } from 'uint8arraylist'

const domain = 'libp2p-testing'
const codec = uint8arrayFromString('/libp2p/testdata')
Expand All @@ -22,11 +22,11 @@ class TestRecord implements Record {
}

marshal () {
return new Uint8ArrayList(uint8arrayFromString(this.data))
return uint8arrayFromString(this.data)
}

equals (other: Record) {
return this.marshal().equals(other.marshal())
return uint8ArrayEquals(this.marshal(), other.marshal())
}
}

Expand Down

0 comments on commit 9d2e881

Please sign in to comment.