Skip to content

Commit df71fed

Browse files
hacdiaslidelachingbrain
authored
feat!: opt-in V2-only records, IPIP-428 verification (#234)
Adds a `v1Compatible` option when creating IPNS records that will cause the V1 signature to be added to the record. This option defaults to true, in the future it will be changed to false. The value types have also been updated to make it harder to create invalid records. It now accepts only CIDs, PeerIds, or arbitrary path strings prefixed with `"/"`. BREAKING CHANGE: all /ipns/* keys are now encoded as base36 encoded CIDv1 libp2p-cid Closes #217 --------- Co-authored-by: Marcin Rataj <lidel@lidel.org> Co-authored-by: Alex Potsides <alex@achingbrain.net>
1 parent b510da8 commit df71fed

19 files changed

+776
-321
lines changed

README.md

Lines changed: 26 additions & 37 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, marshalledData)
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
```
9696
/ipns/${base32(<HASH>)}
@@ -101,23 +101,23 @@ Returns a key to be used for storing the ipns entry locally, that is:
101101
```js
102102
import * as ipns from 'ipns'
103103

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

110-
Returns the entry data serialized.
110+
Returns the record data serialized.
111111

112112
### Unmarshal data from proto buffer
113113

114114
```js
115115
import * as ipns from 'ipns'
116116

117-
const data = ipns.unmarshal(storedData)
117+
const ipnsRecord = ipns.unmarshal(storedData)
118118
```
119119

120-
Returns the entry data structure after being serialized.
120+
Returns the `IPNSRecord` after being deserialized.
121121

122122
### Validator
123123

@@ -131,81 +131,70 @@ Contains an object with `validate (marshalledData, key)` and `select (dataA, dat
131131

132132
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).
133133

134-
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`.
134+
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`.
135135

136136
## API
137137

138138
### Create record
139139

140140
```js
141141

142-
ipns.create(privateKey, value, sequenceNumber, lifetime)
142+
ipns.create(privateKey, value, sequenceNumber, lifetime, options)
143143
```
144144

145145
Create an IPNS record for being stored in a protocol buffer.
146146

147147
- `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.
148-
- `value` (Uint8Array): ipfs path of the object to be published.
148+
- `value` (string): IPFS path of the object to be published.
149149
- `sequenceNumber` (Number): number representing the current version of the record.
150150
- `lifetime` (Number): lifetime of the record (in milliseconds).
151+
- `options` (CreateOptions): additional creation options.
151152

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

166155
### Validate record
167156

168157
```js
169-
ipns.validate(publicKey, ipnsEntry)
158+
ipns.validate(publicKey, ipnsRecord)
170159
```
171160

172161
Validate an IPNS record previously stored in a protocol buffer.
173162

174163
- `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.
175-
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
164+
- `ipnsRecord` (`IPNSRecord`): IPNS record (obtained using the create function).
176165

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

179168
### Marshal data with proto buffer
180169

181170
```js
182-
const marshalledData = ipns.marshal(entryData)
171+
const marshalledData = ipns.marshal(ipnsRecord)
183172
```
184173

185-
Returns the entry data serialized.
174+
Returns the serialized IPNS record.
186175

187-
- `entryData` (Object): ipns entry record (obtained using the create function).
176+
- `ipnsRecord` (`IPNSRecord`): ipns record (obtained using the create function).
188177

189178
### Unmarshal data from proto buffer
190179

191180
```js
192181
const data = ipns.unmarshal(storedData)
193182
```
194183

195-
Returns the entry data structure after being serialized.
184+
Returns a `IPNSRecord` after being serialized.
196185

197-
- `storedData` (Uint8Array): ipns entry record serialized.
186+
- `storedData` (Uint8Array): ipns record serialized.
198187

199188
### Extract public key from record
200189

201190
```js
202-
const publicKey = await ipns.extractPublicKey(peerId, ipnsEntry)
191+
const publicKey = await ipns.extractPublicKey(peerId, ipnsRecord)
203192
```
204193

205-
Extract a public key from an IPNS entry.
194+
Extract a public key from an IPNS record.
206195

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

210199
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.
211200

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ipns",
33
"version": "6.0.7",
4-
"description": "ipns record definitions",
4+
"description": "IPNS record definitions",
55
"author": "Vasco Santos <vasco.santos@moxy.studio>",
66
"license": "Apache-2.0 OR MIT",
77
"homepage": "https://github.com/ipfs/js-ipns#readme",

src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const ERR_PEER_ID_FROM_PUBLIC_KEY = 'ERR_PEER_ID_FROM_PUBLIC_KEY'
77
export const ERR_PUBLIC_KEY_FROM_ID = 'ERR_PUBLIC_KEY_FROM_ID'
88
export const ERR_UNDEFINED_PARAMETER = 'ERR_UNDEFINED_PARAMETER'
99
export const ERR_INVALID_RECORD_DATA = 'ERR_INVALID_RECORD_DATA'
10+
export const ERR_INVALID_VALUE = 'ERR_INVALID_VALUE'
1011
export const ERR_INVALID_EMBEDDED_KEY = 'ERR_INVALID_EMBEDDED_KEY'
1112
export const ERR_MISSING_PRIVATE_KEY = 'ERR_MISSING_PRIVATE_KEY'
1213
export const ERR_RECORD_TOO_LARGE = 'ERR_RECORD_TOO_LARGE'

0 commit comments

Comments
 (0)