Skip to content

Commit

Permalink
fix!: add a varint 0 for http to match go-libipni (#15)
Browse files Browse the repository at this point in the history
Getting an error still decoding HTTP advertisements in go. Looks like
it's due to the fact that HTTP encodes a varint(0) for the payload size
-- see ipni/go-libipni#22 (comment)

Tagging @willscott and @masih for visibility. I wonder if there is a way
we can generate a valid ad and verify against it.
  • Loading branch information
hannahhoward authored May 22, 2023
1 parent b02ad4a commit dc59973
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Provider {
* @param {GraphsyncMetadata} [metadata] - required if protocol is `graphsync`
*/
encodeMetadata (protocol = this.protocol, metadata = this.metadata) {
if (protocol === 'http') return HTTP_PREFIX
if (protocol === 'http') return concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))])
if (protocol === 'bitswap') return BITSWAP_PREFIX
if (protocol === 'graphsync') {
if (!metadata) throw new Error('metadata is required')
Expand Down
6 changes: 4 additions & 2 deletions test/advertisement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { readFile } from 'fs/promises'
import { Provider, HTTP_PREFIX, BITSWAP_PREFIX, GRAPHSYNC_PREFIX } from '../provider.js'
import { Advertisement, hashSignableBytes, createExtendedProviderAd } from '../advertisement.js'
import { encode, decode } from '@ipld/dag-json'
import varint from 'varint'
import { concat } from 'uint8arrays/concat'

const schema = await readFile('schema.ipldsch', { encoding: 'utf8' })
const adValidator = createValidator(parseSchema(schema), 'Advertisement')
Expand All @@ -25,7 +27,7 @@ test('one provider', async t => {
Addresses: addresses,
Entries: entries,
ContextID: context,
Metadata: HTTP_PREFIX,
Metadata: concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))]),
IsRm: false
})
t.falsy(encoded.PreviousID, 'previous is not set')
Expand Down Expand Up @@ -103,7 +105,7 @@ test('extended providers', async t => {
t.like(encoded.ExtendedProvider?.Providers[1], {
ID: http.peerId.toString(),
Addresses: [http.addresses[0].toString()],
Metadata: HTTP_PREFIX
Metadata: concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))])
})
t.like(encoded.ExtendedProvider?.Providers[2], {
ID: graph.peerId.toString(),
Expand Down
4 changes: 3 additions & 1 deletion test/provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { CID } from 'multiformats/cid'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { Provider, HTTP_PREFIX, BITSWAP_PREFIX, GRAPHSYNC_PREFIX } from '../provider.js'
import { Advertisement } from '../advertisement.js'
import varint from 'varint'
import { concat } from 'uint8arrays/concat'

test('http', async t => {
const peerId = await createEd25519PeerId()
const addresses = '/dns4/example.org/tcp/443/https'
const hp = new Provider({ protocol: 'http', peerId, addresses })

const meta = hp.encodeMetadata()
t.deepEqual(meta, HTTP_PREFIX)
t.deepEqual(meta, concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))]))

const entries = CID.parse('bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354')
const context = new Uint8Array([99])
Expand Down

0 comments on commit dc59973

Please sign in to comment.