Giter Site home page Giter Site logo

js-ipns's Introduction

ipns

ipfs.io IRC Discord codecov CI

ipns record definitions

Table of contents

Install

$ npm i ipns

This module contains all the necessary code for creating, understanding and validating IPNS records.

Lead Maintainer

Vasco Santos.

Usage

Create record

import * as ipns from 'ipns'

const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)

Validate record

import * as ipns from 'ipns'

await ipns.validate(publicKey, ipnsEntry)
// if no error thrown, the record is valid

Embed public key to record

import * as ipns from 'ipns'

const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)

Extract public key from record

import * as ipns from 'ipns'

const publicKey = ipns.extractPublicKey(peerId, ipnsEntry)

Datastore key

import * as ipns from 'ipns'

ipns.getLocalKey(peerId)

Returns a key to be used for storing the ipns entry locally, that is:

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

Marshal data with proto buffer

import * as ipns from 'ipns'

const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
// ...
const marshalledData = ipns.marshal(entryData)
// ...

Returns the entry data serialized.

Unmarshal data from proto buffer

import * as ipns from 'ipns'

const data = ipns.unmarshal(storedData)

Returns the entry data structure after being serialized.

Validator

import * as ipns from 'ipns'

const validator = ipns.validator

Contains an object with validate (marshalledData, key) and select (dataA, dataB) functions.

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 (signature and validity are verified).

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.

API

Create record

ipns.create(privateKey, value, sequenceNumber, lifetime)

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

  • privateKey (PrivKey RSA Instance): key to be used for cryptographic operations.
  • value (Uint8Array): ipfs path of the object to be published.
  • sequenceNumber (Number): number representing the current version of the record.
  • lifetime (Number): lifetime of the record (in milliseconds).

Returns a Promise that resolves to an object with the entry's properties eg:

{
  value: Uint8Array,
  signature: Uint8Array,
  validityType: 0,
  validity: Uint8Array,
  sequence: 2
}

Validate record

ipns.validate(publicKey, ipnsEntry)

Validate an IPNS record previously stored in a protocol buffer.

  • publicKey (PubKey RSA Instance): key to be used for cryptographic operations.
  • ipnsEntry (Object): ipns entry record (obtained using the create function).

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

Datastore key

ipns.getDatastoreKey(peerId)

Get a key for storing the ipns entry in the datastore.

  • peerId (Uint8Array): peer identifier.

Marshal data with proto buffer

const marshalledData = ipns.marshal(entryData)

Returns the entry data serialized.

  • entryData (Object): ipns entry record (obtained using the create function).

Unmarshal data from proto buffer

const data = ipns.unmarshal(storedData)

Returns the entry data structure after being serialized.

  • storedData (Uint8Array): ipns entry record serialized.

Embed public key to record

const recordWithPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)

Embed a public key in an IPNS entry. If it is possible to extract the public key from the peer-id, there is no need to embed.

  • publicKey (PubKey RSA Instance): key to be used for cryptographic operations.
  • ipnsEntry (Object): ipns entry record (obtained using the create function).

Returns a Promise. If the promise resolves to null it means the public key can be extracted directly from the peer-id.

Extract public key from record

ipns.extractPublicKey(peerId, ipnsEntry, [callback])

Extract a public key from an IPNS entry.

  • peerId (PeerId Instance): peer identifier object.
  • ipnsEntry (Object): ipns entry record (obtained using the create function).

The returned public key (PubKey RSA Instance): may be used for cryptographic operations.

Namespace

Namespace constants for records.

ipns.namespace

// '/ipns/'
ipns.namespaceLength

// 6

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the IPFS Code of Conduct.

License

Licensed under either of

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the IPFS Code of Conduct.

js-ipns's People

Contributors

vasco-santos avatar achingbrain avatar dependabot-preview[bot] avatar semantic-release-bot avatar jacobheun avatar hugomrdias avatar hsanjuan avatar web3-bot avatar web-flow avatar dependabot[bot] avatar swedneck avatar ipfs-mgmt-read-write[bot] avatar dirkmc avatar stebalien avatar mr0grog avatar iiska avatar eordano avatar fsdiogo avatar stensonb avatar alanshaw avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.