$ npm install blockstack
Blockstack JS is a library for profiles/identity and authentication.
The authentication portion of this library can be used to:
- create an authentication request
- create an authentication response
The profiles/identity portion of this library can be used to:
- transform a JSON profile into cryptographically-signed signed tokens
- recover a JSON profile from signed tokens
- validate signed profile tokens
Note: this document uses ES6 in its examples but it is compiled down to Javascript (ES5) and is perfectly compatible with it. If you're using the latter, just make a few adjustments to the examples below (e.g. use "let" instead of "var").
import{requestSignIn}from'blockstack'varappManifest={name: "Hello, Blockstack",start_url: "https://helloblockstack.com",description: "A simple demo of blockstack auth",icons: [{"src": "https://raw.githubusercontent.com/blockstack/blockstack-portal/master/app/images/app-hello-blockstack.png","sizes": "192x192","type": "image/png"}]}$('#login-button').click(function(){requestSignIn(null,appManifest)// The user will be redirected to their identity provider})import{signUserIn}from'blockstack'signUserIn((session)=>{// Redirect the user to the home page})import{makeAuthRequest,makeECPrivateKey}from'blockstack'constprivateKey=makeECPrivateKey()constappManifest={name: "Hello, Blockstack",start_url: "https://helloblockstack.com"}constauthRequest=makeAuthRequest(privateKey,appManifest)import{verifyAuthRequest}from'blockstack'constverified=verifyAuthRequest(authRequest)import{makeAuthResponse,makeECPrivateKey}from'blockstack'constprivateKey=makeECPrivateKey()constauthData={profile: {name: 'Naval Ravikant'},username: 'naval.id'}constauthResponse=makeAuthResponse(privateKey,authData)import { verifyAuthResponse } from 'blockstack'
const verified = verifyAuthResponse(authResponse)
Follow these steps to create and register a profile for a Blockchain ID:
- Create a JSON profile object
- Split up the profile into tokens, sign the tokens, and put them in a token file
- Create a zone file that points to the web location of the profile token file
constprofileOfNaval={"@context": "http://schema.org/","@type": "Person","name": "Naval Ravikant","description": "Co-founder of AngelList"}import{makeECPrivateKey,wrapProfileToken,Person}from'blockstack'constprivateKey=makeECPrivateKey()constperson=newPerson(profileOfNaval)consttoken=person.toToken(privateKey)consttokenFile=[wrapProfileToken(token)]import{verifyProfileToken}from'blockstack'try{constdecodedToken=verifyProfileToken(tokenFile[0].token,publicKey)}catch(e){console.log(e)}constrecoveredProfile=Person.fromToken(tokenFile,publicKey)constvalidationResults=Person.validateSchema(recoveredProfile)import{validateProofs}from'blockstack'constdomainName="naval.id"validateProofs(profile,domainName).then((proofs)=>{console.log(proofs)})$ npm run test
This test will only work with your browser's Cross-Origin Restrictions disabled.
Run npm run compile; npm run browserify before opening the file test.html
in your browser.