Skip to content

Commit dc59973

Browse files
authored
fix!: add a varint 0 for http to match go-libipni (#15)
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.
1 parent b02ad4a commit dc59973

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

provider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Provider {
6464
* @param {GraphsyncMetadata} [metadata] - required if protocol is `graphsync`
6565
*/
6666
encodeMetadata (protocol = this.protocol, metadata = this.metadata) {
67-
if (protocol === 'http') return HTTP_PREFIX
67+
if (protocol === 'http') return concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))])
6868
if (protocol === 'bitswap') return BITSWAP_PREFIX
6969
if (protocol === 'graphsync') {
7070
if (!metadata) throw new Error('metadata is required')

test/advertisement.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { readFile } from 'fs/promises'
77
import { Provider, HTTP_PREFIX, BITSWAP_PREFIX, GRAPHSYNC_PREFIX } from '../provider.js'
88
import { Advertisement, hashSignableBytes, createExtendedProviderAd } from '../advertisement.js'
99
import { encode, decode } from '@ipld/dag-json'
10+
import varint from 'varint'
11+
import { concat } from 'uint8arrays/concat'
1012

1113
const schema = await readFile('schema.ipldsch', { encoding: 'utf8' })
1214
const adValidator = createValidator(parseSchema(schema), 'Advertisement')
@@ -25,7 +27,7 @@ test('one provider', async t => {
2527
Addresses: addresses,
2628
Entries: entries,
2729
ContextID: context,
28-
Metadata: HTTP_PREFIX,
30+
Metadata: concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))]),
2931
IsRm: false
3032
})
3133
t.falsy(encoded.PreviousID, 'previous is not set')
@@ -103,7 +105,7 @@ test('extended providers', async t => {
103105
t.like(encoded.ExtendedProvider?.Providers[1], {
104106
ID: http.peerId.toString(),
105107
Addresses: [http.addresses[0].toString()],
106-
Metadata: HTTP_PREFIX
108+
Metadata: concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))])
107109
})
108110
t.like(encoded.ExtendedProvider?.Providers[2], {
109111
ID: graph.peerId.toString(),

test/provider.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { CID } from 'multiformats/cid'
33
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
44
import { Provider, HTTP_PREFIX, BITSWAP_PREFIX, GRAPHSYNC_PREFIX } from '../provider.js'
55
import { Advertisement } from '../advertisement.js'
6+
import varint from 'varint'
7+
import { concat } from 'uint8arrays/concat'
68

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

1214
const meta = hp.encodeMetadata()
13-
t.deepEqual(meta, HTTP_PREFIX)
15+
t.deepEqual(meta, concat([HTTP_PREFIX, new Uint8Array(varint.encode(0))]))
1416

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

0 commit comments

Comments
 (0)