|
5 | 5 | const { expect } = require('aegir/utils/chai')
|
6 | 6 | const crypto = require('libp2p-crypto')
|
7 | 7 | const mh = require('multihashes')
|
8 |
| -const CID = require('cids') |
| 8 | +const { CID } = require('multiformats/cid') |
| 9 | +const Digest = require('multiformats/hashes/digest') |
9 | 10 | const uint8ArrayFromString = require('uint8arrays/from-string')
|
10 | 11 | const uint8ArrayToString = require('uint8arrays/to-string')
|
11 | 12 |
|
12 | 13 | const PeerId = require('../src')
|
13 | 14 |
|
14 | 15 | const util = require('util')
|
15 | 16 |
|
| 17 | +const DAG_PB_CODE = 0x70 |
| 18 | +const LIBP2P_KEY_CODE = 0x72 |
| 19 | +const RAW_CODE = 0x55 |
| 20 | + |
16 | 21 | const testId = require('./fixtures/sample-id')
|
17 | 22 | const testIdHex = testId.id
|
18 | 23 | const testIdBytes = mh.fromHexString(testId.id)
|
| 24 | +const testIdDigest = Digest.decode(testIdBytes) |
19 | 25 | const testIdB58String = mh.toB58String(testIdBytes)
|
20 |
| -const testIdCID = new CID(1, 'libp2p-key', testIdBytes) |
21 |
| -const testIdCIDString = testIdCID.toBaseEncodedString('base32') |
| 26 | +const testIdCID = CID.createV1(LIBP2P_KEY_CODE, testIdDigest) |
| 27 | +const testIdCIDString = testIdCID.toString() |
22 | 28 |
|
23 | 29 | const goId = require('./fixtures/go-private-key')
|
24 | 30 |
|
@@ -90,63 +96,55 @@ describe('PeerId', () => {
|
90 | 96 | })
|
91 | 97 |
|
92 | 98 | it('recreate from Base58 String (CIDv0))', () => {
|
93 |
| - const id = PeerId.createFromCID(testIdB58String) |
| 99 | + const id = PeerId.createFromCID(CID.parse(testIdB58String)) |
94 | 100 | expect(testIdCIDString).to.equal(id.toString())
|
95 | 101 | expect(testIdBytes).to.deep.equal(id.toBytes())
|
96 | 102 | })
|
97 | 103 |
|
98 | 104 | it('recreate from CIDv1 Base32 (libp2p-key multicodec)', () => {
|
99 |
| - const cid = new CID(1, 'libp2p-key', testIdBytes) |
100 |
| - const cidString = cid.toBaseEncodedString('base32') |
101 |
| - const id = PeerId.createFromCID(cidString) |
102 |
| - expect(cidString).to.equal(id.toString()) |
| 105 | + const cid = CID.createV1(LIBP2P_KEY_CODE, testIdDigest) |
| 106 | + const id = PeerId.createFromCID(cid) |
| 107 | + expect(cid.toString()).to.equal(id.toString()) |
103 | 108 | expect(testIdBytes).to.deep.equal(id.toBytes())
|
104 | 109 | })
|
105 | 110 |
|
106 | 111 | it('recreate from CIDv1 Base32 (dag-pb multicodec)', () => {
|
107 |
| - const cid = new CID(1, 'dag-pb', testIdBytes) |
108 |
| - const cidString = cid.toBaseEncodedString('base32') |
109 |
| - const id = PeerId.createFromCID(cidString) |
| 112 | + const cid = CID.createV1(DAG_PB_CODE, testIdDigest) |
| 113 | + const id = PeerId.createFromCID(cid) |
110 | 114 | // toString should return CID with multicodec set to libp2p-key
|
111 |
| - expect(new CID(id.toString()).codec).to.equal('libp2p-key') |
| 115 | + expect(CID.parse(id.toString()).code).to.equal(LIBP2P_KEY_CODE) |
112 | 116 | expect(testIdBytes).to.deep.equal(id.toBytes())
|
113 | 117 | })
|
114 | 118 |
|
115 | 119 | it('recreate from CID Uint8Array', () => {
|
116 |
| - const id = PeerId.createFromCID(testIdCID.bytes) |
| 120 | + const id = PeerId.createFromBytes(testIdCID.bytes) |
117 | 121 | expect(testIdCIDString).to.equal(id.toString())
|
118 | 122 | expect(testIdBytes).to.deep.equal(id.toBytes())
|
119 | 123 | })
|
120 | 124 |
|
121 | 125 | it('throws on invalid CID multicodec', () => {
|
122 | 126 | // only libp2p and dag-pb are supported
|
123 |
| - const invalidCID = new CID(1, 'raw', testIdBytes).toBaseEncodedString('base32') |
| 127 | + const invalidCID = CID.createV1(RAW_CODE, testIdDigest) |
124 | 128 | expect(() => {
|
125 | 129 | PeerId.createFromCID(invalidCID)
|
126 |
| - }).to.throw(/Supplied PeerID CID has invalid multicodec: raw/) |
| 130 | + }).to.throw(/invalid/i) |
127 | 131 | })
|
128 | 132 |
|
129 |
| - it('throws on invalid CID value', () => { |
130 |
| - // using function code that does not represent valid hash function |
| 133 | + it('throws on invalid multihash value', () => { |
| 134 | + // using function code 0x50 that does not represent valid hash function |
131 | 135 | // https://github.com/multiformats/js-multihash/blob/b85999d5768bf06f1b0f16b926ef2cb6d9c14265/src/constants.js#L345
|
132 |
| - const invalidCID = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L' |
| 136 | + const invalidMultihash = uint8ArrayToString(Uint8Array.from([0x50, 0x1, 0x0]), 'base58btc') |
133 | 137 | expect(() => {
|
134 |
| - PeerId.createFromCID(invalidCID) |
135 |
| - }).to.throw(/multihash unknown function code: 0x50/) |
| 138 | + PeerId.createFromB58String(invalidMultihash) |
| 139 | + }).to.throw(/invalid/i) |
136 | 140 | })
|
137 | 141 |
|
138 | 142 | it('throws on invalid CID object', () => {
|
139 | 143 | const invalidCID = {}
|
140 | 144 | expect(() => {
|
| 145 | + // @ts-expect-error invalid cid is invalid type |
141 | 146 | PeerId.createFromCID(invalidCID)
|
142 |
| - }).to.throw(/Invalid version, must be a number equal to 1 or 0/) |
143 |
| - }) |
144 |
| - |
145 |
| - it('throws on invalid CID object', () => { |
146 |
| - const invalidCID = {} |
147 |
| - expect(() => { |
148 |
| - PeerId.createFromCID(invalidCID) |
149 |
| - }).to.throw(/Invalid version, must be a number equal to 1 or 0/) |
| 147 | + }).to.throw(/invalid/i) |
150 | 148 | })
|
151 | 149 |
|
152 | 150 | it('recreate from a Public Key', async () => {
|
@@ -174,6 +172,28 @@ describe('PeerId', () => {
|
174 | 172 | expect(uint8ArrayToString(id.marshal(), 'base16')).to.deep.equal(testId.marshaled)
|
175 | 173 | })
|
176 | 174 |
|
| 175 | + it('recreate from embedded ed25519 key', async () => { |
| 176 | + const key = '12D3KooWRm8J3iL796zPFi2EtGGtUJn58AG67gcqzMFHZnnsTzqD' |
| 177 | + const id = await PeerId.parse(key) |
| 178 | + expect(id.toB58String()).to.equal(key) |
| 179 | + const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity')) |
| 180 | + expect(id.toB58String()).to.equal(expB58) |
| 181 | + }) |
| 182 | + |
| 183 | + it('recreate from embedded secp256k1 key', async () => { |
| 184 | + const key = '16Uiu2HAm5qw8UyXP2RLxQUx5KvtSN8DsTKz8quRGqGNC3SYiaB8E' |
| 185 | + const id = await PeerId.parse(key) |
| 186 | + expect(id.toB58String()).to.equal(key) |
| 187 | + const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity')) |
| 188 | + expect(id.toB58String()).to.equal(expB58) |
| 189 | + }) |
| 190 | + |
| 191 | + it('recreate from string key', async () => { |
| 192 | + const key = 'QmRsooYQasV5f5r834NSpdUtmejdQcpxXkK6qsozZWEihC' |
| 193 | + const id = await PeerId.parse(key) |
| 194 | + expect(id.toB58String()).to.equal(key) |
| 195 | + }) |
| 196 | + |
177 | 197 | it('can be created from a Secp256k1 public key', async () => {
|
178 | 198 | const privKey = await crypto.keys.generateKeyPair('secp256k1', 256)
|
179 | 199 | const id = await PeerId.createFromPubKey(privKey.public.bytes)
|
@@ -209,7 +229,8 @@ describe('PeerId', () => {
|
209 | 229 |
|
210 | 230 | it('Pretty printing', async () => {
|
211 | 231 | const id1 = await PeerId.create(testOpts)
|
212 |
| - const id2 = await PeerId.createFromPrivKey((id1.toJSON()).privKey) |
| 232 | + const json = id1.toJSON() |
| 233 | + const id2 = await PeerId.createFromPrivKey(json.privKey || 'invalid, should not happen') |
213 | 234 | expect(id1.toPrint()).to.be.eql(id2.toPrint())
|
214 | 235 | expect(id1.toPrint()).to.equal('<peer.ID ' + id1.toB58String().substr(2, 6) + '>')
|
215 | 236 | })
|
@@ -375,6 +396,7 @@ describe('PeerId', () => {
|
375 | 396 | })
|
376 | 397 |
|
377 | 398 | it('invalid id', () => {
|
| 399 | + // @ts-expect-error incorrect constructor arg type |
378 | 400 | expect(() => new PeerId('hello world')).to.throw(/invalid id/)
|
379 | 401 | })
|
380 | 402 | })
|
|
0 commit comments