Skip to content

Commit 430cd08

Browse files
committed
vuuuu
1 parent 15c245e commit 430cd08

File tree

8 files changed

+173
-184
lines changed

8 files changed

+173
-184
lines changed

README.md

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![codecov](https://img.shields.io/codecov/c/github/ipfs/js-ipns.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-ipns)
66
[![CI](https://img.shields.io/github/actions/workflow/status/ipfs/js-ipns/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/ipfs/js-ipns/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
77

8-
> ipns record definitions
8+
> IPNS Record definitions.
99
1010
## Table of contents <!-- omit in toc -->
1111

@@ -54,15 +54,15 @@ This module contains all the necessary code for creating, understanding and vali
5454
```js
5555
import * as ipns from 'ipns'
5656

57-
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
57+
const ipnsRecord = await ipns.create(privateKey, value, sequenceNumber, lifetime)
5858
```
5959

6060
### Validate record
6161

6262
```js
6363
import * as ipns from 'ipns'
6464

65-
await ipns.validate(publicKey, ipnsEntry)
65+
await ipns.validate(publicKey, ipnsRecord)
6666
// if no error thrown, the record is valid
6767
```
6868

@@ -71,15 +71,15 @@ await ipns.validate(publicKey, ipnsEntry)
7171
```js
7272
import * as ipns from 'ipns'
7373

74-
const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
74+
const ipnsRecordWithEmbeddedPublicKey = await ipns.embedPublicKey(publicKey, ipnsRecord)
7575
```
7676

7777
### Extract public key from record
7878

7979
```js
8080
import * as ipns from 'ipns'
8181

82-
const publicKey = await ipns.extractPublicKey(peerId, ipnsEntry)
82+
const publicKey = await ipns.extractPublicKey(peerId, ipnsRecord)
8383
```
8484

8585
### Datastore key
@@ -90,7 +90,7 @@ import * as ipns from 'ipns'
9090
ipns.getLocalKey(peerId)
9191
```
9292

93-
Returns a key to be used for storing the ipns entry locally, that is:
93+
Returns a key to be used for storing the IPNS record locally, that is:
9494

9595
/ipns/${base32(<HASH>)}
9696

@@ -99,23 +99,23 @@ Returns a key to be used for storing the ipns entry locally, that is:
9999
```js
100100
import * as ipns from 'ipns'
101101

102-
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
102+
const ipnsRecord = await ipns.create(privateKey, value, sequenceNumber, lifetime)
103103
// ...
104-
const marshalledData = ipns.marshal(entryData)
104+
const marshalledData = ipns.marshal(ipnsRecord)
105105
// ...
106106
```
107107

108-
Returns the entry data serialized.
108+
Returns the record data serialized.
109109

110110
### Unmarshal data from proto buffer
111111

112112
```js
113113
import * as ipns from 'ipns'
114114

115-
const data = ipns.unmarshal(storedData)
115+
const ipnsRecord = ipns.unmarshal(storedData)
116116
```
117117

118-
Returns the entry data structure after being serialized.
118+
Returns the `IPNSRecord` after being serialized.
119119

120120
### Validator
121121

@@ -129,15 +129,15 @@ Contains an object with `validate (marshalledData, key)` and `select (dataA, dat
129129

130130
The `validate` async function aims to verify if an IPNS record is valid. First the record is unmarshalled, then the public key is obtained and finally the record is validated (`signatureV2` of CBOR `data` is verified).
131131

132-
The `select` function is responsible for deciding which ipns record is the best (newer) between two records. Both records are unmarshalled and their sequence numbers are compared. If the first record provided is the newer, the operation result will be `0`, otherwise the operation result will be `1`.
132+
The `select` function is responsible for deciding which IPNS record is the best (newer) between two records. Both records are unmarshalled and their sequence numbers are compared. If the first record provided is the newer, the operation result will be `0`, otherwise the operation result will be `1`.
133133

134134
## API
135135

136136
### Create record
137137

138138
```js
139139

140-
ipns.create(privateKey, value, sequenceNumber, lifetime)
140+
ipns.create(privateKey, value, sequenceNumber, lifetime, options)
141141
```
142142

143143
Create an IPNS record for being stored in a protocol buffer.
@@ -146,64 +146,53 @@ Create an IPNS record for being stored in a protocol buffer.
146146
- `value` (Uint8Array): 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).
149+
- `options` (CreateOptions): additional creation options.
149150

150-
Returns a `Promise` that resolves to an object with the entry's properties eg:
151-
152-
```js
153-
{
154-
value: Uint8Array,
155-
signature: Uint8Array, // V1 (legacy, ignored)
156-
validityType: 0,
157-
validity: Uint8Array,
158-
sequence: 2,
159-
signatureV2: Uint8Array, // V2 signature of data field
160-
data: Uint8Array // DAG-CBOR that was signed
161-
}
162-
```
151+
Returns a `Promise` that resolves to an object with a `IPNSRecord`.
163152

164153
### Validate record
165154

166155
```js
167-
ipns.validate(publicKey, ipnsEntry)
156+
ipns.validate(publicKey, ipnsRecord)
168157
```
169158

170159
Validate an IPNS record previously stored in a protocol buffer.
171160

172161
- `publicKey` (`PubKey` [RSA Instance](https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/rsa-class.js)): key to be used for cryptographic operations.
173-
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
162+
- `ipnsRecord` (`IPNSRecord`): ipns record record (obtained using the create function).
174163

175164
Returns a `Promise`, which may be rejected if the validation was not successful.
176165

177166
### Marshal data with proto buffer
178167

179168
```js
180-
const marshalledData = ipns.marshal(entryData)
169+
const marshalledData = ipns.marshal(ipnsRecord)
181170
```
182171

183-
Returns the entry data serialized.
172+
Returns the serialized IPNS record.
184173

185-
- `entryData` (Object): ipns entry record (obtained using the create function).
174+
- `ipnsRecord` (`IPNSRecord`): ipns record (obtained using the create function).
186175

187176
### Unmarshal data from proto buffer
188177

189178
```js
190179
const data = ipns.unmarshal(storedData)
191180
```
192181

193-
Returns the entry data structure after being serialized.
182+
Returns a `IPNSRecord` after being serialized.
194183

195-
- `storedData` (Uint8Array): ipns entry record serialized.
184+
- `storedData` (Uint8Array): ipns record serialized.
196185

197186
### Extract public key from record
198187

199188
```js
200-
const publicKey = await ipns.extractPublicKey(peerId, ipnsEntry)
189+
const publicKey = await ipns.extractPublicKey(peerId, ipnsRecord)
201190
```
202191

203-
Extract a public key from an IPNS entry.
192+
Extract a public key from an IPNS record.
204193

205194
- `peerId` (`PeerId` [Instance](https://github.com/libp2p/js-libp2p-peer-id/tree/master/packages/libp2p-peer-id)): peer identifier object.
206-
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
195+
- `ipnsRecord` (`IPNSRecord`): ipns record (obtained using the create function).
207196

208197
Returns a `Promise` which resolves to public key ([`PublicKey`](https://github.com/libp2p/js-libp2p-interfaces/blob/master/packages/interface-keys/src/index.ts) ): may be used for cryptographic operations.
209198

src/index.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1212
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1313
import * as ERRORS from './errors.js'
1414
import { IpnsEntry } from './pb/ipns.js'
15-
import { createCborData, ipnsEntryDataForV1Sig, ipnsEntryDataForV2Sig, parseRFC3339 } from './utils.js'
15+
import { createCborData, ipnsRecordDataForV1Sig, ipnsRecordDataForV2Sig, parseRFC3339 } from './utils.js'
1616
import type { PrivateKey } from '@libp2p/interface-keys'
1717
import type { PeerId } from '@libp2p/interface-peer-id'
1818

@@ -83,8 +83,8 @@ const defaultCreateOptions: CreateOptions = {
8383
}
8484

8585
/**
86-
* Creates a new ipns entry and signs it with the given private key.
87-
* The ipns entry validity should follow the [RFC3339]{@link https://www.ietf.org/rfc/rfc3339.txt} with nanoseconds precision.
86+
* Creates a new IPNS record and signs it with the given private key.
87+
* The IPNS Record validity should follow the [RFC3339]{@link https://www.ietf.org/rfc/rfc3339.txt} with nanoseconds precision.
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.
@@ -111,7 +111,7 @@ export const create = async (peerId: PeerId, value: Uint8Array, seq: number | bi
111111
* @param {Uint8Array} 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.
114-
* @param {CreateOptions} options - additional create options.
114+
* @param {CreateOptions} options - additional creation options.
115115
*/
116116
export const createWithExpiration = async (peerId: PeerId, value: Uint8Array, seq: number | bigint, expiration: string, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
117117
const expirationDate = NanoDate.fromString(expiration)
@@ -133,22 +133,22 @@ const _create = async (peerId: PeerId, value: Uint8Array, seq: number | bigint,
133133

134134
const privateKey = await unmarshalPrivateKey(peerId.privateKey)
135135
const data = createCborData(value, isoValidity, validityType, seq, ttl)
136-
const sigData = ipnsEntryDataForV2Sig(data)
136+
const sigData = ipnsRecordDataForV2Sig(data)
137137
const signatureV2 = await privateKey.sign(sigData)
138138

139-
const entry: IpnsEntry = {
139+
const pb: IpnsEntry = {
140140
signatureV2,
141141
data
142142
}
143143

144144
if (options.v1Compatible === true) {
145145
const signatureV1 = await signLegacyV1(privateKey, value, validityType, isoValidity)
146-
entry.value = value
147-
entry.validity = isoValidity
148-
entry.validityType = validityType
149-
entry.signature = signatureV1
150-
entry.sequence = seq
151-
entry.ttl = ttl
146+
pb.value = value
147+
pb.validity = isoValidity
148+
pb.validityType = validityType
149+
pb.signature = signatureV1
150+
pb.sequence = seq
151+
pb.ttl = ttl
152152
}
153153

154154
// if we cannot derive the public key from the PeerId (e.g. RSA PeerIDs),
@@ -157,12 +157,12 @@ const _create = async (peerId: PeerId, value: Uint8Array, seq: number | bigint,
157157
const digest = Digest.decode(peerId.toBytes())
158158

159159
if (digest.code !== ID_MULTIHASH_CODE || !uint8ArrayEquals(peerId.publicKey, digest.digest)) {
160-
entry.pubKey = peerId.publicKey
160+
pb.pubKey = peerId.publicKey
161161
}
162162
}
163163

164-
log('ipns entry for %b created', value)
165-
return new IPNSRecord(entry)
164+
log('ipns record for %b created', value)
165+
return new IPNSRecord(pb)
166166
}
167167

168168
/**
@@ -189,7 +189,7 @@ export { extractPublicKey } from './utils.js'
189189
*/
190190
const signLegacyV1 = async (privateKey: PrivateKey, value: Uint8Array, validityType: IpnsEntry.ValidityType, validity: Uint8Array): Promise<Uint8Array> => {
191191
try {
192-
const dataForSignature = ipnsEntryDataForV1Sig(value, validityType, validity)
192+
const dataForSignature = ipnsRecordDataForV1Sig(value, validityType, validity)
193193

194194
return await privateKey.sign(dataForSignature)
195195
} catch (error: any) {

src/selector.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import type { SelectFn } from '@libp2p/interface-dht'
33

44
export const ipnsSelector: SelectFn = (key, data) => {
55
const entries = data.map((buf, index) => ({
6-
entry: unmarshal(buf),
6+
record: unmarshal(buf),
77
index
88
}))
99

1010
entries.sort((a, b) => {
1111
// having a newer signature version is better than an older signature version
12-
if (a.entry.pb.signatureV2 != null && b.entry.pb.signatureV2 == null) {
12+
if (a.record.pb.signatureV2 != null && b.record.pb.signatureV2 == null) {
1313
return -1
14-
} else if (a.entry.pb.signatureV2 == null && b.entry.pb.signatureV2 != null) {
14+
} else if (a.record.pb.signatureV2 == null && b.record.pb.signatureV2 != null) {
1515
return 1
1616
}
1717

18-
const aSeq = a.entry.sequence()
19-
const bSeq = b.entry.sequence()
18+
const aSeq = a.record.sequence()
19+
const bSeq = b.record.sequence()
2020

2121
// choose later sequence number
2222
if (aSeq > bSeq) {
@@ -26,14 +26,14 @@ export const ipnsSelector: SelectFn = (key, data) => {
2626
}
2727

2828
// choose longer lived record if sequence numbers the same
29-
const entryAValidityDate = a.entry.validity()
30-
const entryBValidityDate = b.entry.validity()
29+
const recordAValidityDate = a.record.validity()
30+
const recordBValidityDate = b.record.validity()
3131

32-
if (entryAValidityDate.getTime() > entryBValidityDate.getTime()) {
32+
if (recordAValidityDate.getTime() > recordBValidityDate.getTime()) {
3333
return -1
3434
}
3535

36-
if (entryAValidityDate.getTime() < entryBValidityDate.getTime()) {
36+
if (recordAValidityDate.getTime() < recordBValidityDate.getTime()) {
3737
return 1
3838
}
3939

src/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export function parseRFC3339 (time: string): Date {
6565
* Extracts a public key from the passed PeerId, falling
6666
* back to the pubKey embedded in the ipns record
6767
*/
68-
export const extractPublicKey = async (peerId: PeerId, entry: IPNSRecord): Promise<PublicKey> => {
69-
if (entry == null || peerId == null) {
68+
export const extractPublicKey = async (peerId: PeerId, record: IPNSRecord): Promise<PublicKey> => {
69+
if (record == null || peerId == null) {
7070
const error = new Error('one or more of the provided parameters are not defined')
7171

7272
log.error(error)
@@ -75,15 +75,15 @@ export const extractPublicKey = async (peerId: PeerId, entry: IPNSRecord): Promi
7575

7676
let pubKey: PublicKey | undefined
7777

78-
if (entry.pb.pubKey != null) {
78+
if (record.pb.pubKey != null) {
7979
try {
80-
pubKey = unmarshalPublicKey(entry.pb.pubKey)
80+
pubKey = unmarshalPublicKey(record.pb.pubKey)
8181
} catch (err) {
8282
log.error(err)
8383
throw err
8484
}
8585

86-
const otherId = await peerIdFromKeys(entry.pb.pubKey)
86+
const otherId = await peerIdFromKeys(record.pb.pubKey)
8787

8888
if (!otherId.equals(peerId)) {
8989
throw errCode(new Error('Embedded public key did not match PeerID'), ERRORS.ERR_INVALID_EMBEDDED_KEY)
@@ -102,7 +102,7 @@ export const extractPublicKey = async (peerId: PeerId, entry: IPNSRecord): Promi
102102
/**
103103
* Utility for creating the record data for being signed
104104
*/
105-
export const ipnsEntryDataForV1Sig = (value: Uint8Array, validityType: IpnsEntry.ValidityType, validity: Uint8Array): Uint8Array => {
105+
export const ipnsRecordDataForV1Sig = (value: Uint8Array, validityType: IpnsEntry.ValidityType, validity: Uint8Array): Uint8Array => {
106106
const validityTypeBuffer = uint8ArrayFromString(validityType)
107107

108108
return uint8ArrayConcat([value, validity, validityTypeBuffer])
@@ -111,7 +111,7 @@ export const ipnsEntryDataForV1Sig = (value: Uint8Array, validityType: IpnsEntry
111111
/**
112112
* Utility for creating the record data for being signed
113113
*/
114-
export const ipnsEntryDataForV2Sig = (data: Uint8Array): Uint8Array => {
114+
export const ipnsRecordDataForV2Sig = (data: Uint8Array): Uint8Array => {
115115
const entryData = uint8ArrayFromString('ipns-signature:')
116116

117117
return uint8ArrayConcat([entryData, data])

0 commit comments

Comments
 (0)