Skip to content

Commit 93dda74

Browse files
committed
fix: peer record interop with go (#739)
* test: add go peer record interop test * fix: correct the payload type of peer records * chore: fix linting * test: fix envelope test
1 parent cfbd52d commit 93dda74

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"merge-options": "^2.0.0",
6666
"moving-average": "^1.0.0",
6767
"multiaddr": "^8.0.0",
68-
"multicodec": "^1.0.2",
68+
"multicodec": "^2.0.0",
6969
"multistream-select": "^1.0.0",
7070
"mutable-proxy": "^1.0.0",
7171
"node-forge": "^0.9.1",

src/record/envelope/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Envelope.createFromProtobuf = async (data) => {
140140
*/
141141
Envelope.seal = async (record, peerId) => {
142142
const domain = record.domain
143-
const payloadType = uint8arraysFromString(record.codec)
143+
const payloadType = record.codec
144144
const payload = record.marshal()
145145

146146
const signData = formatSignaturePayload(domain, payloadType, payload)

src/record/peer-record/consts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const multicodec = require('multicodec')
44

55
// The domain string used for peer records contained in a Envelope.
6-
module.exports.ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
6+
module.exports.ENVELOPE_DOMAIN_PEER_RECORD = multicodec.getName(multicodec.LIBP2P_PEER_RECORD)
77

88
// The type hint used to identify peer records in a Envelope.
99
// Defined in https://github.com/multiformats/multicodec/blob/master/table.csv
1010
// with name "libp2p-peer-record"
11-
module.exports.ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = multicodec.print[multicodec.LIBP2P_PEER_RECORD]
11+
module.exports.ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = Uint8Array.from([3, 1])

test/record/envelope.spec.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ chai.use(require('chai-bytes'))
77
chai.use(require('chai-as-promised'))
88
const { expect } = chai
99

10+
const uint8arrayFromString = require('uint8arrays/from-string')
11+
const uint8arrayEquals = require('uint8arrays/equals')
1012
const Envelope = require('../../src/record/envelope')
1113
const Record = require('libp2p-interfaces/src/record')
1214
const { codes: ErrorCodes } = require('../../src/errors')
1315

1416
const peerUtils = require('../utils/creators/peer')
1517

1618
const domain = 'libp2p-testing'
17-
const codec = '/libp2p/testdata'
19+
const codec = uint8arrayFromString('/libp2p/testdata')
1820

1921
class TestRecord extends Record {
2022
constructor (data) {
@@ -23,16 +25,16 @@ class TestRecord extends Record {
2325
}
2426

2527
marshal () {
26-
return Buffer.from(this.data)
28+
return uint8arrayFromString(this.data)
2729
}
2830

2931
equals (other) {
30-
return Buffer.compare(this.data, other.data)
32+
return uint8arrayEquals(this.data, other.data)
3133
}
3234
}
3335

3436
describe('Envelope', () => {
35-
const payloadType = Buffer.from(codec)
37+
const payloadType = codec
3638
let peerId
3739
let testRecord
3840

@@ -43,7 +45,7 @@ describe('Envelope', () => {
4345

4446
it('creates an envelope with a random key', () => {
4547
const payload = testRecord.marshal()
46-
const signature = Buffer.from(Math.random().toString(36).substring(7))
48+
const signature = uint8arrayFromString(Math.random().toString(36).substring(7))
4749

4850
const envelope = new Envelope({
4951
peerId,
@@ -63,7 +65,7 @@ describe('Envelope', () => {
6365
const envelope = await Envelope.seal(testRecord, peerId)
6466
expect(envelope).to.exist()
6567
expect(envelope.peerId.equals(peerId)).to.eql(true)
66-
expect(envelope.payloadType).to.equalBytes(payloadType)
68+
expect(envelope.payloadType).to.eql(payloadType)
6769
expect(envelope.payload).to.exist()
6870
expect(envelope.signature).to.exist()
6971
})

test/record/peer-record.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { expect } = chai
77

88
const tests = require('libp2p-interfaces/src/record/tests')
99
const multiaddr = require('multiaddr')
10+
const PeerId = require('peer-id')
1011

1112
const Envelope = require('../../src/record/envelope')
1213
const PeerRecord = require('../../src/record/peer-record')
@@ -32,6 +33,23 @@ describe('PeerRecord', () => {
3233
[peerId] = await peerUtils.createPeerId()
3334
})
3435

36+
it('de/serializes the same as a go record', async () => {
37+
const privKey = Uint8Array.from([8, 1, 18, 64, 133, 251, 231, 43, 96, 100, 40, 144, 4, 165, 49, 249, 103, 137, 141, 245, 49, 158, 224, 41, 146, 253, 216, 64, 33, 250, 80, 82, 67, 75, 246, 238, 17, 187, 163, 237, 23, 33, 148, 140, 239, 180, 229, 11, 10, 11, 181, 202, 216, 166, 181, 45, 199, 177, 164, 15, 79, 102, 82, 16, 92, 145, 226, 196])
38+
const rawEnvelope = Uint8Array.from([10, 36, 8, 1, 18, 32, 17, 187, 163, 237, 23, 33, 148, 140, 239, 180, 229, 11, 10, 11, 181, 202, 216, 166, 181, 45, 199, 177, 164, 15, 79, 102, 82, 16, 92, 145, 226, 196, 18, 2, 3, 1, 26, 170, 1, 10, 38, 0, 36, 8, 1, 18, 32, 17, 187, 163, 237, 23, 33, 148, 140, 239, 180, 229, 11, 10, 11, 181, 202, 216, 166, 181, 45, 199, 177, 164, 15, 79, 102, 82, 16, 92, 145, 226, 196, 16, 216, 184, 224, 191, 147, 145, 182, 151, 22, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 0, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 1, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 2, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 3, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 4, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 5, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 6, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 7, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 8, 26, 10, 10, 8, 4, 1, 2, 3, 4, 6, 0, 9, 42, 64, 177, 151, 247, 107, 159, 40, 138, 242, 180, 103, 254, 102, 111, 119, 68, 118, 40, 112, 73, 180, 36, 183, 57, 117, 200, 134, 14, 251, 2, 55, 45, 2, 106, 121, 149, 132, 84, 26, 215, 47, 38, 84, 52, 100, 133, 188, 163, 236, 227, 100, 98, 183, 209, 177, 57, 28, 141, 39, 109, 196, 171, 139, 202, 11])
39+
const peerId = await PeerId.createFromPrivKey(privKey)
40+
41+
const env = await Envelope.openAndCertify(rawEnvelope, PeerRecord.DOMAIN)
42+
expect(peerId.equals(env.peerId))
43+
44+
const record = PeerRecord.createFromProtobuf(env.payload)
45+
46+
// The payload isn't going to match because of how the protobuf encodes uint64 values
47+
// They are marshalled correctly on both sides, but will be off by 1 value
48+
// Signatures will still be validated
49+
const jsEnv = await Envelope.seal(record, peerId)
50+
expect(env.payloadType).to.eql(jsEnv.payloadType)
51+
})
52+
3553
it('creates a peer record with peerId', () => {
3654
const peerRecord = new PeerRecord({ peerId })
3755

0 commit comments

Comments
 (0)