Skip to content

Commit

Permalink
chore: update deps (#126)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses new peer-id class and supporting ecosystem modules
  • Loading branch information
achingbrain authored Jul 9, 2021
1 parent d567530 commit 063fc85
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 121 deletions.
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prepare:proto": "pbjs -t static-module -w commonjs -r ipfs-ipns --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/pb/ipns.js src/pb/ipns.proto",
"prepare:proto-types": "pbts -o src/pb/ipns.d.ts src/pb/ipns.js",
"prepare:types": "aegir build --no-bundle",
"lint": "aegir lint",
"lint": "aegir ts -p check &&aegir lint",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
Expand Down Expand Up @@ -44,22 +44,27 @@
"cborg": "^1.3.3",
"debug": "^4.2.0",
"err-code": "^3.0.1",
"interface-datastore": "^4.0.2",
"libp2p-crypto": "^0.19.0",
"interface-datastore": "^5.1.1",
"libp2p-crypto": "^0.19.5",
"long": "^4.0.0",
"multibase": "^4.0.2",
"multihashes": "^4.0.2",
"peer-id": "^0.14.2",
"multiformats": "^9.2.0",
"peer-id": "^0.15.0",
"protobufjs": "^6.10.2",
"timestamp-nano": "^1.0.0",
"uint8arrays": "^2.0.5"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"aegir": "^33.1.0",
"aegir": "^34.0.1",
"npm-run-all": "^4.1.5",
"util": "^0.12.3"
},
"eslintConfig": {
"extends": "ipfs",
"ignorePatterns": [
"src/pb/ipns.d.ts"
]
},
"contributors": [
"Vasco Santos <vasco.santos@moxy.studio>",
"Alex Potsides <alex@achingbrain.net>",
Expand Down
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const NanoDate = require('timestamp-nano')
const { Key } = require('interface-datastore')
const crypto = require('libp2p-crypto')
const PeerId = require('peer-id')
const multihash = require('multihashes')
const Digest = require('multiformats/hashes/digest')
const { identity } = require('multiformats/hashes/identity')
const errCode = require('err-code')
const multibase = require('multibase')
const { base32upper } = require('multiformats/bases/base32')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayConcat = require('uint8arrays/concat')
Expand All @@ -25,7 +26,7 @@ const {
const { parseRFC3339 } = require('./utils')
const ERRORS = require('./errors')

const ID_MULTIHASH_CODE = multihash.names.identity
const ID_MULTIHASH_CODE = identity.code

const namespace = '/ipns/'

Expand Down Expand Up @@ -310,7 +311,7 @@ const extractPublicKey = (peerId, entry) => {
*
* @param {Uint8Array} key
*/
const rawStdEncoding = (key) => uint8ArrayToString(multibase.encode('base32', key)).slice(1).toUpperCase()
const rawStdEncoding = (key) => base32upper.encode(key).slice(1)

/**
* Get key for storing the record locally.
Expand Down Expand Up @@ -402,13 +403,13 @@ const ipnsEntryDataForV2Sig = (data) => {
* @param {PeerId} peerId
*/
const extractPublicKeyFromId = (peerId) => {
const decodedId = multihash.decode(peerId.id)
const digest = Digest.decode(peerId.id)

if (decodedId.code !== ID_MULTIHASH_CODE) {
if (digest.code !== ID_MULTIHASH_CODE) {
return null
}

return crypto.keys.unmarshalPublicKey(decodedId.digest)
return crypto.keys.unmarshalPublicKey(digest.digest)
}

/**
Expand Down Expand Up @@ -452,8 +453,8 @@ const validator = {
*/
validate: async (marshalledData, key) => {
const receivedEntry = unmarshal(marshalledData)
const bufferId = key.slice('/ipns/'.length)
const peerId = PeerId.createFromBytes(bufferId)
const bufferId = uint8ArrayToString(key).substring('/ipns/'.length)
const peerId = PeerId.parse(bufferId)

// extract public key
const pubKey = extractPublicKey(peerId, receivedEntry)
Expand Down
195 changes: 100 additions & 95 deletions src/pb/ipns.d.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,120 @@
import * as $protobuf from "protobufjs";
import * as $protobuf from 'protobufjs'
/** Properties of an IpnsEntry. */
export interface IIpnsEntry {

/** IpnsEntry value */
value?: (Uint8Array|null);
/** IpnsEntry value */
value?: (Uint8Array|null)

/** IpnsEntry signature */
signature?: (Uint8Array|null);
/** IpnsEntry signature */
signature?: (Uint8Array|null)

/** IpnsEntry validityType */
validityType?: (IpnsEntry.ValidityType|null);
/** IpnsEntry validityType */
validityType?: (IpnsEntry.ValidityType|null)

/** IpnsEntry validity */
validity?: (Uint8Array|null);
/** IpnsEntry validity */
validity?: (Uint8Array|null)

/** IpnsEntry sequence */
sequence?: (number|Long|null);
/** IpnsEntry sequence */
sequence?: (number|Long|null)

/** IpnsEntry ttl */
ttl?: (number|Long|null);
/** IpnsEntry ttl */
ttl?: (number|Long|null)

/** IpnsEntry pubKey */
pubKey?: (Uint8Array|null);
/** IpnsEntry pubKey */
pubKey?: (Uint8Array|null)

/** IpnsEntry signatureV2 */
signatureV2?: (Uint8Array|null);
/** IpnsEntry signatureV2 */
signatureV2?: (Uint8Array|null)

/** IpnsEntry data */
data?: (Uint8Array|null);
/** IpnsEntry data */
data?: (Uint8Array|null)
}

/** Represents an IpnsEntry. */
export class IpnsEntry implements IIpnsEntry {

/**
* Constructs a new IpnsEntry.
* @param [p] Properties to set
*/
constructor(p?: IIpnsEntry);

/** IpnsEntry value. */
public value: Uint8Array;

/** IpnsEntry signature. */
public signature: Uint8Array;

/** IpnsEntry validityType. */
public validityType: IpnsEntry.ValidityType;

/** IpnsEntry validity. */
public validity: Uint8Array;

/** IpnsEntry sequence. */
public sequence: (number|Long);

/** IpnsEntry ttl. */
public ttl: (number|Long);

/** IpnsEntry pubKey. */
public pubKey: Uint8Array;

/** IpnsEntry signatureV2. */
public signatureV2: Uint8Array;

/** IpnsEntry data. */
public data: Uint8Array;

/**
* Encodes the specified IpnsEntry message. Does not implicitly {@link IpnsEntry.verify|verify} messages.
* @param m IpnsEntry message or plain object to encode
* @param [w] Writer to encode to
* @returns Writer
*/
public static encode(m: IIpnsEntry, w?: $protobuf.Writer): $protobuf.Writer;

/**
* Decodes an IpnsEntry message from the specified reader or buffer.
* @param r Reader or buffer to decode from
* @param [l] Message length if known beforehand
* @returns IpnsEntry
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): IpnsEntry;

/**
* Creates an IpnsEntry message from a plain object. Also converts values to their respective internal types.
* @param d Plain object
* @returns IpnsEntry
*/
public static fromObject(d: { [k: string]: any }): IpnsEntry;

/**
* Creates a plain object from an IpnsEntry message. Also converts values to other types if specified.
* @param m IpnsEntry
* @param [o] Conversion options
* @returns Plain object
*/
public static toObject(m: IpnsEntry, o?: $protobuf.IConversionOptions): { [k: string]: any };

/**
* Converts this IpnsEntry to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
/**
* Constructs a new IpnsEntry.
*
* @param [p] - Properties to set
*/
constructor (p?: IIpnsEntry);

/** IpnsEntry value. */
public value: Uint8Array

/** IpnsEntry signature. */
public signature: Uint8Array

/** IpnsEntry validityType. */
public validityType: IpnsEntry.ValidityType

/** IpnsEntry validity. */
public validity: Uint8Array

/** IpnsEntry sequence. */
public sequence: (number|Long)

/** IpnsEntry ttl. */
public ttl: (number|Long)

/** IpnsEntry pubKey. */
public pubKey: Uint8Array

/** IpnsEntry signatureV2. */
public signatureV2: Uint8Array

/** IpnsEntry data. */
public data: Uint8Array

/**
* Encodes the specified IpnsEntry message. Does not implicitly {@link IpnsEntry.verify|verify} messages.
*
* @param m - IpnsEntry message or plain object to encode
* @param [w] - Writer to encode to
* @returns Writer
*/
public static encode (m: IIpnsEntry, w?: $protobuf.Writer): $protobuf.Writer;

/**
* Decodes an IpnsEntry message from the specified reader or buffer.
*
* @param r - Reader or buffer to decode from
* @param [l] - Message length if known beforehand
* @returns IpnsEntry
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode (r: ($protobuf.Reader|Uint8Array), l?: number): IpnsEntry;

/**
* Creates an IpnsEntry message from a plain object. Also converts values to their respective internal types.
*
* @param d - Plain object
* @returns IpnsEntry
*/
public static fromObject (d: { [k: string]: any }): IpnsEntry;

/**
* Creates a plain object from an IpnsEntry message. Also converts values to other types if specified.
*
* @param m - IpnsEntry
* @param [o] - Conversion options
* @returns Plain object
*/
public static toObject (m: IpnsEntry, o?: $protobuf.IConversionOptions): { [k: string]: any };

/**
* Converts this IpnsEntry to JSON.
*
* @returns JSON object
*/
public toJSON (): { [k: string]: any };
}

export namespace IpnsEntry {

/** ValidityType enum. */
enum ValidityType {
EOL = 0
}
/** ValidityType enum. */
enum ValidityType {
EOL = 0
}
}
8 changes: 4 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import ValidityType from './pb/ipns'

export interface IPNSEntry {
value: Uint8Array, // value to be stored in the record
signature: Uint8Array, // signature of the record
validityType: ValidityType, // Type of validation being used
validity: Uint8Array, // expiration datetime for the record in RFC3339 format
value: Uint8Array // value to be stored in the record
signature: Uint8Array // signature of the record
validityType: ValidityType // Type of validation being used
validity: Uint8Array // expiration datetime for the record in RFC3339 format
sequence: BigInt // number representing the version of the record
ttl?: BigInt // ttl in nanoseconds
pubKey?: Uint8Array
Expand Down
11 changes: 5 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
'use strict'

const { expect } = require('aegir/utils/chai')
const { toB58String } = require('multihashes')
const { base58btc } = require('multiformats/bases/base58')
const uint8ArrayFromString = require('uint8arrays/from-string')
const PeerId = require('peer-id')

const crypto = require('libp2p-crypto')
const { fromB58String } = require('multihashes')

const ipns = require('../src')
const ERRORS = require('../src/errors')
Expand Down Expand Up @@ -126,14 +125,14 @@ describe('ipns', function () {
})

it('should get datastore key correctly', () => {
const datastoreKey = ipns.getLocalKey(fromB58String(ipfsId.id))
const datastoreKey = ipns.getLocalKey(base58btc.decode(`z${ipfsId.id}`))

expect(datastoreKey).to.exist()
expect(datastoreKey.toString()).to.startWith('/ipns/CIQ')
})

it('should get id keys correctly', () => {
const idKeys = ipns.getIdKeys(fromB58String(ipfsId.id))
const idKeys = ipns.getIdKeys(base58btc.decode(`z${ipfsId.id}`))

expect(idKeys).to.exist()
expect(idKeys).to.have.property('routingPubKey')
Expand All @@ -153,8 +152,8 @@ describe('ipns', function () {
]

keys.forEach(key => {
const { routingKey } = ipns.getIdKeys(fromB58String(key))
const id = toB58String(routingKey.uint8Array().subarray(ipns.namespaceLength))
const { routingKey } = ipns.getIdKeys(base58btc.decode(`z${key}`))
const id = base58btc.encode(routingKey.uint8Array().subarray(ipns.namespaceLength)).substring(1)

expect(id).to.equal(key)
})
Expand Down

0 comments on commit 063fc85

Please sign in to comment.