-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
61 lines (58 loc) · 1.59 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<template>
<div>
<form @submit.prevent="onSubmit">
<input type="text" v-model="title" />
<input type="file" @change="onUpload" />
<input type="submit" value="Upload" />
</form>
</div>
</template>
<script>
import * as IPFS from 'ipfs-http-client'
import { connectWallet } from './web3'
import {Buffer} from 'buffer'
export default {
async setup(){
const ipfs_client = IPFS.create({url: "http://127.0.0.1:5001/api/v0"})
const web3Wallet = await connectWallet()
return {ipfs_client, web3Wallet}
},
data(){
return {
title: "",
imageBuffer: null
}
},
created(){
},
methods: {
async onSubmit(){
if(this.imageBuffer && this.title){
const fileHash = await this.ipfs_client.add(this.imageBuffer)
const textHash = await this.ipfs_client.add(this.title)
this.web3Wallet.contract.methods.sendHash(fileHash.path, textHash.path)
.send({from: this.web3Wallet.accounts[0]}, (error, trx) => {
console.log("error", error)
if(trx !== undefined){
alert("Storing on Ethereum");
this.web3Wallet.contract.once("NewPost", {from: this.web3Wallet.accounts[0]}, () => {
alert("Stored successfull")
})
}
})
}
},
onUpload(e){
const reader = new FileReader();
if(typeof e !== undefined){
reader.readAsArrayBuffer(e.target.files[0])
reader.onload = () => {
this.imageBuffer = Buffer.from(reader.result)
}
}else{
this.imageBuffer = ''
}
},
}
}
</script>