mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-14 01:31:22 +00:00
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
This commit is contained in:
@ -65,7 +65,7 @@
|
||||
"merge-options": "^2.0.0",
|
||||
"moving-average": "^1.0.0",
|
||||
"multiaddr": "^8.0.0",
|
||||
"multicodec": "^1.0.2",
|
||||
"multicodec": "^2.0.0",
|
||||
"multistream-select": "^1.0.0",
|
||||
"mutable-proxy": "^1.0.0",
|
||||
"node-forge": "^0.9.1",
|
||||
|
@ -140,7 +140,7 @@ Envelope.createFromProtobuf = async (data) => {
|
||||
*/
|
||||
Envelope.seal = async (record, peerId) => {
|
||||
const domain = record.domain
|
||||
const payloadType = uint8arraysFromString(record.codec)
|
||||
const payloadType = record.codec
|
||||
const payload = record.marshal()
|
||||
|
||||
const signData = formatSignaturePayload(domain, payloadType, payload)
|
||||
|
@ -3,9 +3,9 @@
|
||||
const multicodec = require('multicodec')
|
||||
|
||||
// The domain string used for peer records contained in a Envelope.
|
||||
module.exports.ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
|
||||
module.exports.ENVELOPE_DOMAIN_PEER_RECORD = multicodec.getName(multicodec.LIBP2P_PEER_RECORD)
|
||||
|
||||
// The type hint used to identify peer records in a Envelope.
|
||||
// Defined in https://github.com/multiformats/multicodec/blob/master/table.csv
|
||||
// with name "libp2p-peer-record"
|
||||
module.exports.ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = multicodec.print[multicodec.LIBP2P_PEER_RECORD]
|
||||
module.exports.ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = Uint8Array.from([3, 1])
|
||||
|
@ -7,6 +7,8 @@ chai.use(require('chai-bytes'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
|
||||
const uint8arrayFromString = require('uint8arrays/from-string')
|
||||
const uint8arrayEquals = require('uint8arrays/equals')
|
||||
const Envelope = require('../../src/record/envelope')
|
||||
const Record = require('libp2p-interfaces/src/record')
|
||||
const { codes: ErrorCodes } = require('../../src/errors')
|
||||
@ -14,7 +16,7 @@ const { codes: ErrorCodes } = require('../../src/errors')
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
|
||||
const domain = 'libp2p-testing'
|
||||
const codec = '/libp2p/testdata'
|
||||
const codec = uint8arrayFromString('/libp2p/testdata')
|
||||
|
||||
class TestRecord extends Record {
|
||||
constructor (data) {
|
||||
@ -23,16 +25,16 @@ class TestRecord extends Record {
|
||||
}
|
||||
|
||||
marshal () {
|
||||
return Buffer.from(this.data)
|
||||
return uint8arrayFromString(this.data)
|
||||
}
|
||||
|
||||
equals (other) {
|
||||
return Buffer.compare(this.data, other.data)
|
||||
return uint8arrayEquals(this.data, other.data)
|
||||
}
|
||||
}
|
||||
|
||||
describe('Envelope', () => {
|
||||
const payloadType = Buffer.from(codec)
|
||||
const payloadType = codec
|
||||
let peerId
|
||||
let testRecord
|
||||
|
||||
@ -43,7 +45,7 @@ describe('Envelope', () => {
|
||||
|
||||
it('creates an envelope with a random key', () => {
|
||||
const payload = testRecord.marshal()
|
||||
const signature = Buffer.from(Math.random().toString(36).substring(7))
|
||||
const signature = uint8arrayFromString(Math.random().toString(36).substring(7))
|
||||
|
||||
const envelope = new Envelope({
|
||||
peerId,
|
||||
@ -63,7 +65,7 @@ describe('Envelope', () => {
|
||||
const envelope = await Envelope.seal(testRecord, peerId)
|
||||
expect(envelope).to.exist()
|
||||
expect(envelope.peerId.equals(peerId)).to.eql(true)
|
||||
expect(envelope.payloadType).to.equalBytes(payloadType)
|
||||
expect(envelope.payloadType).to.eql(payloadType)
|
||||
expect(envelope.payload).to.exist()
|
||||
expect(envelope.signature).to.exist()
|
||||
})
|
||||
|
@ -7,6 +7,7 @@ const { expect } = chai
|
||||
|
||||
const tests = require('libp2p-interfaces/src/record/tests')
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const Envelope = require('../../src/record/envelope')
|
||||
const PeerRecord = require('../../src/record/peer-record')
|
||||
@ -32,6 +33,23 @@ describe('PeerRecord', () => {
|
||||
[peerId] = await peerUtils.createPeerId()
|
||||
})
|
||||
|
||||
it('de/serializes the same as a go record', async () => {
|
||||
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])
|
||||
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])
|
||||
const peerId = await PeerId.createFromPrivKey(privKey)
|
||||
|
||||
const env = await Envelope.openAndCertify(rawEnvelope, PeerRecord.DOMAIN)
|
||||
expect(peerId.equals(env.peerId))
|
||||
|
||||
const record = PeerRecord.createFromProtobuf(env.payload)
|
||||
|
||||
// The payload isn't going to match because of how the protobuf encodes uint64 values
|
||||
// They are marshalled correctly on both sides, but will be off by 1 value
|
||||
// Signatures will still be validated
|
||||
const jsEnv = await Envelope.seal(record, peerId)
|
||||
expect(env.payloadType).to.eql(jsEnv.payloadType)
|
||||
})
|
||||
|
||||
it('creates a peer record with peerId', () => {
|
||||
const peerRecord = new PeerRecord({ peerId })
|
||||
|
||||
|
Reference in New Issue
Block a user