Skip to content

Commit afc160b

Browse files
committed
refactor: change value from bytes array to string
1 parent 2a62e1f commit afc160b

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ ipns.create(privateKey, value, sequenceNumber, lifetime, options)
143143
Create an IPNS record for being stored in a protocol buffer.
144144

145145
- `privateKey` (`PrivKey` [RSA Instance](https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/rsa-class.js)): key to be used for cryptographic operations.
146-
- `value` (Uint8Array): ipfs path of the object to be published.
146+
- `value` (string): IPFS path of the object to be published.
147147
- `sequenceNumber` (Number): number representing the current version of the record.
148148
- `lifetime` (Number): lifetime of the record (in milliseconds).
149149
- `options` (CreateOptions): additional creation options.

src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ const defaultCreateOptions: CreateOptions = {
8888
* Note: This function does not embed the public key. If you want to do that, use `EmbedPublicKey`.
8989
*
9090
* @param {PeerId} peerId - peer id containing private key for signing the record.
91-
* @param {Uint8Array} value - value to be stored in the record.
91+
* @param {string} value - value to be stored in the record.
9292
* @param {number | bigint} seq - number representing the current version of the record.
9393
* @param {number} lifetime - lifetime of the record (in milliseconds).
9494
* @param {CreateOptions} options - additional create options.
9595
*/
96-
export const create = async (peerId: PeerId, value: Uint8Array, seq: number | bigint, lifetime: number, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
96+
export const create = async (peerId: PeerId, value: string, seq: number | bigint, lifetime: number, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
9797
// Validity in ISOString with nanoseconds precision and validity type EOL
9898
const expirationDate = new NanoDate(Date.now() + Number(lifetime))
9999
const validityType = IpnsEntry.ValidityType.EOL
@@ -108,12 +108,12 @@ export const create = async (peerId: PeerId, value: Uint8Array, seq: number | bi
108108
* WARNING: nano precision is not standard, make sure the value in seconds is 9 orders of magnitude lesser than the one provided.
109109
*
110110
* @param {PeerId} peerId - PeerId containing private key for signing the record.
111-
* @param {Uint8Array} value - value to be stored in the record.
111+
* @param {string} value - value to be stored in the record.
112112
* @param {number | bigint} seq - number representing the current version of the record.
113113
* @param {string} expiration - expiration datetime for record in the [RFC3339]{@link https://www.ietf.org/rfc/rfc3339.txt} with nanoseconds precision.
114114
* @param {CreateOptions} options - additional creation options.
115115
*/
116-
export const createWithExpiration = async (peerId: PeerId, value: Uint8Array, seq: number | bigint, expiration: string, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
116+
export const createWithExpiration = async (peerId: PeerId, value: string, seq: number | bigint, expiration: string, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
117117
const expirationDate = NanoDate.fromString(expiration)
118118
const validityType = IpnsEntry.ValidityType.EOL
119119

@@ -123,16 +123,17 @@ export const createWithExpiration = async (peerId: PeerId, value: Uint8Array, se
123123
return _create(peerId, value, seq, validityType, expirationDate, ttlNs, options)
124124
}
125125

126-
const _create = async (peerId: PeerId, value: Uint8Array, seq: number | bigint, validityType: IpnsEntry.ValidityType, expirationDate: NanoDate, ttl: bigint, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
126+
const _create = async (peerId: PeerId, value: string, seq: number | bigint, validityType: IpnsEntry.ValidityType, expirationDate: NanoDate, ttl: bigint, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
127127
seq = BigInt(seq)
128128
const isoValidity = uint8ArrayFromString(expirationDate.toString())
129+
const encodedValue = uint8ArrayFromString(value)
129130

130131
if (peerId.privateKey == null) {
131132
throw errCode(new Error('Missing private key'), ERRORS.ERR_MISSING_PRIVATE_KEY)
132133
}
133134

134135
const privateKey = await unmarshalPrivateKey(peerId.privateKey)
135-
const data = createCborData(value, isoValidity, validityType, seq, ttl)
136+
const data = createCborData(encodedValue, isoValidity, validityType, seq, ttl)
136137
const sigData = ipnsRecordDataForV2Sig(data)
137138
const signatureV2 = await privateKey.sign(sigData)
138139

@@ -142,8 +143,8 @@ const _create = async (peerId: PeerId, value: Uint8Array, seq: number | bigint,
142143
}
143144

144145
if (options.v1Compatible === true) {
145-
const signatureV1 = await signLegacyV1(privateKey, value, validityType, isoValidity)
146-
pb.value = value
146+
const signatureV1 = await signLegacyV1(privateKey, encodedValue, validityType, isoValidity)
147+
pb.value = encodedValue
147148
pb.validity = isoValidity
148149
pb.validityType = validityType
149150
pb.signatureV1 = signatureV1

test/index.spec.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
77
import { expect } from 'aegir/chai'
88
import { base58btc } from 'multiformats/bases/base58'
99
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1110
import * as ERRORS from '../src/errors.js'
1211
import * as ipns from '../src/index.js'
1312
import { IpnsEntry } from '../src/pb/ipns.js'
@@ -18,7 +17,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
1817
describe('ipns', function () {
1918
this.timeout(20 * 1000)
2019

21-
const cid = uint8ArrayFromString('QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq')
20+
const contentPath = '/ipfs/bafkqae3imvwgy3zamzzg63janjzs22lqnzzqu'
2221
let peerId: PeerId
2322

2423
before(async () => {
@@ -30,16 +29,16 @@ describe('ipns', function () {
3029
const sequence = 0
3130
const validity = 1000000
3231

33-
const record = await ipns.create(peerId, cid, sequence, validity)
32+
const record = await ipns.create(peerId, contentPath, sequence, validity)
3433

35-
expect(record.value()).to.equal(uint8ArrayToString(cid))
34+
expect(record.value()).to.equal(contentPath)
3635
expect(record.sequence()).to.equal(BigInt(0))
3736
expect(record.validityType()).to.equal(IpnsEntry.ValidityType.EOL)
3837
expect(record.validity()).to.exist()
3938
expect(record.ttl()).to.equal(BigInt(validity * 100000))
4039

4140
expect(record.pb).to.deep.include({
42-
value: cid,
41+
value: uint8ArrayFromString(contentPath),
4342
sequence: BigInt(sequence)
4443
})
4544
expect(record.pb).to.have.property('validity')
@@ -53,9 +52,9 @@ describe('ipns', function () {
5352
const sequence = 0
5453
const validity = 1000000
5554

56-
const record = await ipns.create(peerId, cid, sequence, validity, { v1Compatible: false })
55+
const record = await ipns.create(peerId, contentPath, sequence, validity, { v1Compatible: false })
5756

58-
expect(record.value()).to.equal(uint8ArrayToString(cid))
57+
expect(record.value()).to.equal(contentPath)
5958
expect(record.sequence()).to.equal(BigInt(0))
6059
expect(record.validityType()).to.equal(IpnsEntry.ValidityType.EOL)
6160
expect(record.validity()).to.exist()
@@ -75,7 +74,7 @@ describe('ipns', function () {
7574
// 2033-05-18T03:33:20.000000000Z
7675
const expiration = '2033-05-18T03:33:20.000000000Z'
7776

78-
const record = await ipns.createWithExpiration(peerId, cid, sequence, expiration)
77+
const record = await ipns.createWithExpiration(peerId, contentPath, sequence, expiration)
7978

8079
await ipnsValidator(peerIdToRoutingKey(peerId), marshal(record))
8180
expect(record.pb).to.have.property('validity')
@@ -87,7 +86,7 @@ describe('ipns', function () {
8786
// 2033-05-18T03:33:20.000000000Z
8887
const expiration = '2033-05-18T03:33:20.000000000Z'
8988

90-
const record = await ipns.createWithExpiration(peerId, cid, sequence, expiration, { v1Compatible: false })
89+
const record = await ipns.createWithExpiration(peerId, contentPath, sequence, expiration, { v1Compatible: false })
9190

9291
await ipnsValidator(peerIdToRoutingKey(peerId), marshal(record))
9392
expect(record.pb).to.not.have.property('validity')
@@ -98,23 +97,23 @@ describe('ipns', function () {
9897
const sequence = 0
9998
const validity = 1000000
10099

101-
const record = await ipns.create(peerId, cid, sequence, validity)
100+
const record = await ipns.create(peerId, contentPath, sequence, validity)
102101
await ipnsValidator(peerIdToRoutingKey(peerId), marshal(record))
103102
})
104103

105104
it('should create an ipns record (V2) and validate it correctly', async () => {
106105
const sequence = 0
107106
const validity = 1000000
108107

109-
const record = await ipns.create(peerId, cid, sequence, validity, { v1Compatible: false })
108+
const record = await ipns.create(peerId, contentPath, sequence, validity, { v1Compatible: false })
110109
await ipnsValidator(peerIdToRoutingKey(peerId), marshal(record))
111110
})
112111

113112
it('should fail to validate a v1 (deprecated legacy) message', async () => {
114113
const sequence = 0
115114
const validity = 1000000
116115

117-
const record = await ipns.create(peerId, cid, sequence, validity)
116+
const record = await ipns.create(peerId, contentPath, sequence, validity)
118117

119118
// remove the extra fields added for v2 sigs
120119
delete record.pb.data
@@ -130,7 +129,7 @@ describe('ipns', function () {
130129
const sequence = 0
131130
const validity = 1000000
132131

133-
const record = await ipns.create(peerId, cid, sequence, validity)
132+
const record = await ipns.create(peerId, contentPath, sequence, validity)
134133

135134
// remove v2 sig
136135
delete record.pb.signatureV2
@@ -145,7 +144,7 @@ describe('ipns', function () {
145144
const sequence = 0
146145
const validity = 1000000
147146

148-
const record = await ipns.create(peerId, cid, sequence, validity)
147+
const record = await ipns.create(peerId, contentPath, sequence, validity)
149148

150149
// corrupt the record by changing the value to random bytes
151150
record.pb.value = randomBytes(46)
@@ -157,7 +156,7 @@ describe('ipns', function () {
157156
const sequence = 0
158157
const validity = 0.00001
159158

160-
const record = await ipns.create(peerId, cid, sequence, validity)
159+
const record = await ipns.create(peerId, contentPath, sequence, validity)
161160

162161
await new Promise(resolve => setTimeout(resolve, 1))
163162

@@ -168,7 +167,7 @@ describe('ipns', function () {
168167
const sequence = 0
169168
const validity = 1000000
170169

171-
const createdRecord = await ipns.create(peerId, cid, sequence, validity)
170+
const createdRecord = await ipns.create(peerId, contentPath, sequence, validity)
172171

173172
const marshalledData = marshal(createdRecord)
174173
const unmarshalledData = unmarshal(marshalledData)
@@ -220,7 +219,7 @@ describe('ipns', function () {
220219
const sequence = 0
221220
const validity = 1000000
222221

223-
const record = await ipns.create(peerId, cid, sequence, validity)
222+
const record = await ipns.create(peerId, contentPath, sequence, validity)
224223

225224
expect(record.pb).to.deep.include({
226225
pubKey: peerId.publicKey
@@ -238,7 +237,7 @@ describe('ipns', function () {
238237
const validity = 1000000
239238

240239
const ed25519 = await createEd25519PeerId()
241-
const record = await ipns.create(ed25519, cid, sequence, validity)
240+
const record = await ipns.create(ed25519, contentPath, sequence, validity)
242241

243242
expect(record.pb).to.not.have.property('pubKey') // ed25519 keys should not be embedded
244243
})
@@ -247,7 +246,7 @@ describe('ipns', function () {
247246
const sequence = 0
248247
const validity = 1000000
249248

250-
const record = await ipns.create(peerId, cid, sequence, validity)
249+
const record = await ipns.create(peerId, contentPath, sequence, validity)
251250
delete record.pb.pubKey
252251

253252
const marshalledData = marshal(record)
@@ -260,7 +259,7 @@ describe('ipns', function () {
260259
const sequence = 0
261260
const validity = 1000000
262261

263-
const record = await ipns.create(peerId, cid, sequence, validity)
262+
const record = await ipns.create(peerId, contentPath, sequence, validity)
264263

265264
const publicKey = await extractPublicKey(peerId, record)
266265
expect(publicKey).to.deep.include({

test/selector.spec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { generateKeyPair } from '@libp2p/crypto/keys'
44
import { peerIdFromKeys } from '@libp2p/peer-id'
55
import { expect } from 'aegir/chai'
6-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
76
import * as ipns from '../src/index.js'
87
import { ipnsSelector } from '../src/selector.js'
98
import { marshal, peerIdToRoutingKey } from '../src/utils.js'
@@ -12,7 +11,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
1211
describe('selector', function () {
1312
this.timeout(20 * 1000)
1413

15-
const cid = uint8ArrayFromString('QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq')
14+
const contentPath = '/ipfs/bafkqae3imvwgy3zamzzg63janjzs22lqnzzqu'
1615
let peerId: PeerId
1716

1817
before(async () => {
@@ -24,8 +23,8 @@ describe('selector', function () {
2423
const sequence = 0
2524
const lifetime = 1000000
2625

27-
const record = await ipns.create(peerId, cid, sequence, lifetime)
28-
const newRecord = await ipns.create(peerId, cid, (sequence + 1), lifetime)
26+
const record = await ipns.create(peerId, contentPath, sequence, lifetime)
27+
const newRecord = await ipns.create(peerId, contentPath, (sequence + 1), lifetime)
2928

3029
const marshalledData = marshal(record)
3130
const marshalledNewData = marshal(newRecord)
@@ -43,8 +42,8 @@ describe('selector', function () {
4342
const sequence = 0
4443
const lifetime = 1000000
4544

46-
const record = await ipns.create(peerId, cid, sequence, lifetime)
47-
const newRecord = await ipns.create(peerId, cid, sequence, (lifetime + 1))
45+
const record = await ipns.create(peerId, contentPath, sequence, lifetime)
46+
const newRecord = await ipns.create(peerId, contentPath, sequence, (lifetime + 1))
4847

4948
const marshalledData = marshal(record)
5049
const marshalledNewData = marshal(newRecord)
@@ -62,9 +61,9 @@ describe('selector', function () {
6261
const sequence = 0
6362
const lifetime = 1000000
6463

65-
const record = await ipns.create(peerId, cid, sequence, lifetime)
64+
const record = await ipns.create(peerId, contentPath, sequence, lifetime)
6665

67-
const newRecord = await ipns.create(peerId, cid, sequence + 1, lifetime)
66+
const newRecord = await ipns.create(peerId, contentPath, sequence + 1, lifetime)
6867
delete newRecord.pb.signatureV2
6968

7069
const marshalledData = marshal(record)

test/validator.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
1616
describe('validator', function () {
1717
this.timeout(20 * 1000)
1818

19-
const cid = uint8ArrayFromString('QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq')
19+
const contentPath = '/ipfs/bafkqae3imvwgy3zamzzg63janjzs22lqnzzqu'
2020
let peerId1: PeerId
2121
let peerId2: PeerId
2222

@@ -32,7 +32,7 @@ describe('validator', function () {
3232
const sequence = 0
3333
const validity = 1000000
3434

35-
const record = await ipns.create(peerId1, cid, sequence, validity, { v1Compatible: false })
35+
const record = await ipns.create(peerId1, contentPath, sequence, validity, { v1Compatible: false })
3636
const marshalledData = marshal(record)
3737

3838
const keyBytes = base58btc.decode(`z${peerId1.toString()}`)
@@ -45,7 +45,7 @@ describe('validator', function () {
4545
const sequence = 0
4646
const validity = 1000000
4747

48-
const record = await ipns.create(peerId1, cid, sequence, validity, { v1Compatible: true })
48+
const record = await ipns.create(peerId1, contentPath, sequence, validity, { v1Compatible: true })
4949
const marshalledData = marshal(record)
5050

5151
const keyBytes = base58btc.decode(`z${peerId1.toString()}`)
@@ -58,7 +58,7 @@ describe('validator', function () {
5858
const sequence = 0
5959
const validity = 1000000
6060

61-
const record = await ipns.create(peerId1, cid, sequence, validity)
61+
const record = await ipns.create(peerId1, contentPath, sequence, validity)
6262

6363
// corrupt the record by changing the value to random bytes
6464
record.pb.value = randomBytes(record.pb.value?.length ?? 0)
@@ -73,7 +73,7 @@ describe('validator', function () {
7373
const sequence = 0
7474
const validity = 1000000
7575

76-
const record = await ipns.create(peerId1, cid, sequence, validity)
76+
const record = await ipns.create(peerId1, contentPath, sequence, validity)
7777
const marshalledData = marshal(record)
7878

7979
const key = peerIdToRoutingKey(peerId2)
@@ -85,7 +85,7 @@ describe('validator', function () {
8585
const sequence = 0
8686
const validity = 1000000
8787

88-
const record = await ipns.create(peerId1, cid, sequence, validity)
88+
const record = await ipns.create(peerId1, contentPath, sequence, validity)
8989
record.pb.pubKey = peerId2.publicKey
9090
const marshalledData = marshal(record)
9191

0 commit comments

Comments
 (0)