Skip to content

Instantly share code, notes, and snippets.

@GrandSchtroumpf
Created October 15, 2018 09:29
Show Gist options
  • Save GrandSchtroumpf/8d653970f19332d54938f3435ac7c81e to your computer and use it in GitHub Desktop.
Save GrandSchtroumpf/8d653970f19332d54938f3435ac7c81e to your computer and use it in GitHub Desktop.
A simple component using the ipfs injection token
import { Component, Inject, OnInit } from '@angular/core';
import { IPFS } from './ipfs';
import {Buffer} from 'buffer';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public hash: string;
// Inject the ipfs injection token
constructor(@Inject(IPFS) private ipfs) {}
ngOnInit() {
// Wait for the ipfs node to be ready
this.ipfs.on('ready', async () => {
// Get the version
const version = await this.ipfs.version();
console.log({version});
// Add a file in IPFS
const filesAdded = await this.ipfs.files.add({
path: 'hello.txt',
content: Buffer.from('Hello World')
});
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash);
// Read the file from IPFS
const fileBuffer = await this.ipfs.files.cat(filesAdded[0].hash);
console.log('Added file contents:', fileBuffer.toString());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment