Skip to content

Instantly share code, notes, and snippets.

@elmariachi111
Last active July 28, 2021 09:48
Show Gist options
  • Save elmariachi111/d957f8b79086859a8c0a292a17a8712c to your computer and use it in GitHub Desktop.
Save elmariachi111/d957f8b79086859a8c0a292a17a8712c to your computer and use it in GitHub Desktop.
IPFS Samples
import CeramicClient from "@ceramicnetwork/http-client";
import { IDX } from "@ceramicstudio/idx";
import { Ed25519Provider } from "key-did-provider-ed25519";
import KeyResolver from "key-did-resolver";
import { DID } from "dids";
const seed = Buffer.from("c7943d32...d6edfcd","hex"); //32 random bytes
const did = new DID({
provider: new Ed25519Provider(seed),
resolver: KeyResolver.getResolver(),
});
(async () => {
console.log(await did.authenticate());
const ceramic = new CeramicClient("https://ceramic-clay.3boxlabs.com");
ceramic.setDID(did);
const idx = new IDX({ ceramic, aliases: {} });
const streamId = await idx.set("basicProfile", {
name: "dotnetpro tester",
description: "demoing IDX for dotnetpro",
emoji: "<3",
});
console.log(streamId);
})();
/*
> did:key:z6MkenQ8XxAT7fRa1fFMkw31M7ydDHLfdyJsqzvmyV4KWZfj
> StreamID(k2t6wyfsu4pg1ai6537smnd8hu66z0xhfqt3buuimdzafig8dgrm3x750m2kpc)
$ npx idx index:get did:key:z6MkenQ8XxAT7fRa1fFMkw31M7ydDHLfdyJsqzvmyV4KWZfj basicProfile
> {
> name: 'dotnetpro tester',
> description: 'demoing IDX for dotnetpro',
> emoji: '<3'
> }
*/
mkdir website
echo "Hello, Web3 World" >> website/index.html
# Ordner rekursiv veröffentlichen
ipfs add -r website
> added QmdTNjHwkc79cxBvcrxxMZuaakxaU56jqvovdbHAMdyxx6 website/index.html
> added QmRj5vMxG9A1iMPiAiMyShJq8hFRZyhgnMxVwCVoP82tju website
# Eine Datei via IPFS beziehen und anzeigen
ipfs cat QmdTNjHwkc79cxBvcrxxMZuaakxaU56jqvovdbHAMdyxx6
> Hello, Web3 World
# Eine benannte Datei aus einem Verzeichnis über ein öffentliches HTTP-Gateway beziehen
curl https://ipfs.io/ipfs/QmRj5vMxG9A1iMPiAiMyShJq8hFRZyhgnMxVwCVoP82tju/index.html
> Hello, Web3 World
# erstellt einen neuen IPFS Schlüssel für IPNS
ipfs key gen web3articlekey
> k51qzi5uqu5dmcpyyldjeeyqhisauj3pdkjkmiiud5z0180thr7kruoqgnz69r
# veröffentlicht die /ipfs/-Adresse unter dem Schlüssel
ipfs name publish --key=web3articlekey /ipfs/QmRj5vMxG9A1iMPiAiMyShJq8hFRZyhgnMxVwCVoP82tju
# löst die im Netzwerk bekannte aktuelle CID hinter dem Schlüssel auf
ipfs name resolve k51qzi5uqu5dmcpyyldjeeyqhisauj3pdkjkmiiud5z0180thr7kruoqgnz69r
> /ipfs/QmRj5vMxG9A1iMPiAiMyShJq8hFRZyhgnMxVwCVoP82tju
//npm install multihashing multibase lodash.isequal
const multihashing = require('multihashing')
const multibase = require('multibase')
const crypto = require('crypto')
const isEqual = require('lodash.isequal')
const decoder = new TextDecoder()
const bytes = Buffer.from("Hello, dotnetpro");
// create an sha1 multihash and encode it as base64
const someMultihash = multihashing(bytes, 'sha1')
const encoded = multibase.encode('base64urlpad', someMultihash)
console.log(decoder.decode(encoded));
// UERR6F_mTb9hB29WhwmIy8igojmQzvQ==
//decode and check the hash **without** knowing its encoding / hash function
const b64Decoded = multibase.decode(encoded);
const decodedHash = multihashing.multihash.decode(someMultihash);
const hashFunction = decodedHash.name
const hashed = crypto.createHash(hashFunction).update(bytes).digest()
console.log(isEqual(b64Decoded.slice(2), hashed));
//true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment