add hash poll content to ipfs

add sign ipfs hash and transmit
This commit is contained in:
Barry Gitarts 2020-07-15 15:55:40 -04:00
parent 1a88c8393c
commit 9d9ad0d16f
6 changed files with 247 additions and 13 deletions

View File

@ -7,6 +7,9 @@ import useStyles from '../styles/createPoll'
import StatusTextField from './base/TextField'
import DatePicker from './base/DatePicker'
import StatusButton from './base/Button'
import { prettySign, verifySignedMessage } from '../utils/signing'
import { uploadFilesToIpfs, uploadToIpfs } from '../utils/ipfs'
import { sendToPublicChat } from '../utils/status'
type FormikValues = {
title: string,
@ -16,7 +19,12 @@ type FormikValues = {
description: string
}
const POLLS = 'polls'
const TEN_DAYS_FUTURE = new Date(new Date().getTime()+(10*24*60*60*1000))
const createJSON = (values: FormikValues): string => {
return JSON.stringify(values, null, 2)
}
function CreatePoll() {
const [showPreview, setPreview] = useState<boolean>(false)
const classes: any = useStyles()
@ -31,7 +39,13 @@ function CreatePoll() {
datePicker: TEN_DAYS_FUTURE,
description: ''
}}
onSubmit={(values) => console.log({values})}
onSubmit={async (values) => {
const message = createJSON(values)
const ipfsHash = await uploadToIpfs(message)
const signedMessage = await prettySign(ipfsHash)
const stringified = JSON.stringify(signedMessage)
await sendToPublicChat(POLLS, stringified)
}}
>
{({
values,

55
dapp/src/utils/ipfs.ts Normal file
View File

@ -0,0 +1,55 @@
// @ts-ignore
import ipfsClient from 'ipfs-http-client'
const ipfsHttpStatus = ipfsClient({ host: 'ipfs.status.im', protocol: 'https', port: '443' })
type file = {
name: string
type: string
}
type ipfsFile = {
path: string
content: any
}
export const formatForIpfsGateway = (file: file): ipfsFile => {
const { name } = file
const content = file
return {
path: `/root/${name}`,
content
}
}
const uploadToIpfsRaw = async (str: string) => {
for await (const result of ipfsHttpStatus.add(str)) {
return result
}
}
export const uploadToIpfs = async (str:string): Promise<string> => {
const res = await uploadToIpfsRaw(str)
return res.cid.string
}
export const uploadToIpfsGateway = async (files: ipfsFile[]): Promise<string> => {
const res = await ipfsHttpStatus.add(files)
return `ipfs/${res.slice(-1)[0].hash}`
}
export const uploadFilesToIpfs = async (
manifest: string, files: any = {}
): Promise<string> => {
let fileLists: ipfsFile[] = []
const formatFn = formatForIpfsGateway
const uploadFn = uploadToIpfsGateway
Object.keys(files).forEach(k => {
fileLists = [...fileLists, formatFn(files[k][0])]
})
fileLists.push({
path: '/root/manifest.json', content: Buffer.from(manifest)
})
const res = await uploadFn(fileLists)
return res
}

View File

@ -4,7 +4,6 @@ type signedMessage = {
address: string,
msg: string,
sig: string,
version: number
}
export function sign(message: string): Promise<string> {
@ -26,7 +25,6 @@ export async function prettySign(message: string): Promise<signedMessage> {
return {
address: web3.eth.defaultAccount,
msg: message,
sig,
version: 2
sig
};
}

25
dapp/src/utils/status.ts Normal file
View File

@ -0,0 +1,25 @@
export {}
declare global {
interface Window {
ethereum: any;
}
}
export async function sendToPublicChat(topic: string, message: string | object) {
try {
const res = await window.ethereum.status.sendToPublicChat(topic, message)
return res
} catch (e) {
console.error('send to public chat', {e})
}
}
export async function gotoPublicChat(topic: string) {
try {
const res = window.ethereum.status.gotoPublicChat(topic)
return res
} catch(e) {
console.error('send to public chat', {e})
}
}

View File

@ -34,6 +34,7 @@
},
"dependencies": {
"@material-ui/core": "^4.11.0",
"@material-ui/styles": "^4.10.0"
"@material-ui/styles": "^4.10.0",
"ipfs-http-client": "^44.3.0"
}
}

157
yarn.lock
View File

@ -1861,6 +1861,13 @@ any-promise@1.3.0:
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
integrity sha1-q8av7tzqUugJzcA3au0845Y10X8=
any-signal@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/any-signal/-/any-signal-1.1.0.tgz#8e43cd0ae03266261d5061595ecf8aaad1828872"
integrity sha512-mtwqpy58ys+/dRdH5Z8VArUluVrfz9/5BXo8tvSZ9kcQr3k9yyOPnGrYCBJQfcC5IlMrr63kDBlf5GyQCFn+Fw==
dependencies:
abort-controller "^3.0.0"
anymatch@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb"
@ -2066,6 +2073,11 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=
at-least-node@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==
atob@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
@ -2781,7 +2793,7 @@ cids@^0.7.1, cids@~0.7.0, cids@~0.7.1:
multicodec "^1.0.0"
multihashes "~0.4.15"
cids@~0.8.0:
cids@^0.8.0, cids@~0.8.0:
version "0.8.3"
resolved "https://registry.yarnpkg.com/cids/-/cids-0.8.3.tgz#aaf48ac8ed857c3d37dad94d8db1d8c9407b92db"
integrity sha512-yoXTbV3llpm+EBGWKeL9xKtksPE/s6DPoDSY4fn8I8TEW1zehWXPSB0pwAXVDlLaOlrw+sNynj995uD9abmPhA==
@ -6013,6 +6025,16 @@ fs-extra@^4.0.2:
jsonfile "^4.0.0"
universalify "^0.1.0"
fs-extra@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
dependencies:
at-least-node "^1.0.0"
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^1.0.0"
fs-minipass@^1.2.5:
version "1.2.7"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.7.tgz#ccff8570841e7fe4265693da88936c55aed7f7c7"
@ -6921,6 +6943,15 @@ ipfs-block@~0.8.1:
cids "~0.7.0"
class-is "^1.1.0"
ipfs-core-utils@^0.2.4:
version "0.2.4"
resolved "https://registry.yarnpkg.com/ipfs-core-utils/-/ipfs-core-utils-0.2.4.tgz#fcbcc774383eb44a4f870834b51f351898f73a8d"
integrity sha512-Spj5xtmdKMAOsPSUEmiKxj5Oa6FLP5QJgdA5w2WGpaUKw9fTU0he+mtb5Fs/PZusbU9GWxYELVZ1f1TnemeVsA==
dependencies:
buffer "^5.6.0"
err-code "^2.0.0"
ipfs-utils "^2.2.2"
ipfs-http-client@40.0.0:
version "40.0.0"
resolved "https://registry.yarnpkg.com/ipfs-http-client/-/ipfs-http-client-40.0.0.tgz#d7f99ca47c557cdccc84d361c5de277b07291093"
@ -6962,6 +6993,39 @@ ipfs-http-client@40.0.0:
peer-info "~0.15.1"
promise-nodeify "^3.0.1"
ipfs-http-client@^44.3.0:
version "44.3.0"
resolved "https://registry.yarnpkg.com/ipfs-http-client/-/ipfs-http-client-44.3.0.tgz#30921a27b480de7015d555049092209a3832d34f"
integrity sha512-oGlD2GkyisCrRPgJYrpo6TYA5VZneCoazEaaKxc1fyGd9TG0Fvc/nBuwW6jB6I5CR78z1JwKr/71QlvZL4jDNg==
dependencies:
abort-controller "^3.0.0"
any-signal "^1.1.0"
bignumber.js "^9.0.0"
buffer "^5.6.0"
cids "^0.8.0"
debug "^4.1.0"
form-data "^3.0.0"
ipfs-core-utils "^0.2.4"
ipfs-utils "^2.2.2"
ipld-block "^0.9.1"
ipld-dag-cbor "^0.15.2"
ipld-dag-pb "^0.18.5"
ipld-raw "^5.0.0"
iso-url "^0.4.7"
it-tar "^1.2.2"
it-to-buffer "^1.0.0"
it-to-stream "^0.1.1"
merge-options "^2.0.0"
multiaddr "^7.4.3"
multiaddr-to-uri "^5.1.0"
multibase "^0.7.0"
multicodec "^1.0.0"
multihashes "^1.0.1"
nanoid "^3.0.2"
node-fetch "^2.6.0"
parse-duration "^0.1.2"
stream-to-it "^0.2.0"
ipfs-utils@^0.4.0:
version "0.4.2"
resolved "https://registry.yarnpkg.com/ipfs-utils/-/ipfs-utils-0.4.2.tgz#01b46c447b99fdca6b85decadc816d0070b884c7"
@ -6979,7 +7043,34 @@ ipfs-utils@^0.4.0:
pull-stream-to-async-iterator "^1.0.2"
readable-stream "^3.4.0"
ipld-dag-cbor@~0.15.0:
ipfs-utils@^2.2.2:
version "2.3.1"
resolved "https://registry.yarnpkg.com/ipfs-utils/-/ipfs-utils-2.3.1.tgz#999951da4461b5901a5ad38329e247b5c14b7bd1"
integrity sha512-Rq2iyFdqNknVtIgq8+/URt5WK94uEqTa2w22t2yrNE9CDifh5jvFaO6lBOMhsqzPkHjjJBoIqk+g5P0T2OHSow==
dependencies:
abort-controller "^3.0.0"
any-signal "^1.1.0"
buffer "^5.6.0"
err-code "^2.0.0"
fs-extra "^9.0.1"
is-electron "^2.2.0"
iso-url "^0.4.7"
it-glob "0.0.8"
merge-options "^2.0.0"
nanoid "^3.1.3"
node-fetch "^2.6.0"
stream-to-it "^0.2.0"
ipld-block@^0.9.1:
version "0.9.2"
resolved "https://registry.yarnpkg.com/ipld-block/-/ipld-block-0.9.2.tgz#d6c702e3c4171ff44e0a7b76c21d337676599196"
integrity sha512-/i99foB+QI8WhyZWu6ZVPFw2sP6kzZSnnjPNlxxrgaJeFX22w2z00nYWafY2YYYP4mZ9xkLZDSS/msli7XXyvw==
dependencies:
buffer "^5.5.0"
cids "~0.8.0"
class-is "^1.1.0"
ipld-dag-cbor@^0.15.2, ipld-dag-cbor@~0.15.0:
version "0.15.3"
resolved "https://registry.yarnpkg.com/ipld-dag-cbor/-/ipld-dag-cbor-0.15.3.tgz#283afdb81d5b07db8e4fff7a10ef5e517e87f299"
integrity sha512-m23nG7ZyoVFnkK55/bLAErc7EfiMgaEQlqHWDTGzPI+O5r6bPfp+qbL5zTVSIT8tpbHmu174dwerVtLoVgeVyA==
@ -6991,7 +7082,7 @@ ipld-dag-cbor@~0.15.0:
multicodec "^1.0.0"
multihashing-async "~0.8.0"
ipld-dag-pb@^0.18.1:
ipld-dag-pb@^0.18.1, ipld-dag-pb@^0.18.5:
version "0.18.5"
resolved "https://registry.yarnpkg.com/ipld-dag-pb/-/ipld-dag-pb-0.18.5.tgz#29e736dcdab10a4dffbef9dec27723e2e56be962"
integrity sha512-8IAPZrkRjgTpkxV9JOwXSBe0GXNxd4B2lubPgbifTGL92rZOEKWutpijsWsWvjXOltDFHKMQIIIhkgLC5RPqbA==
@ -7013,6 +7104,15 @@ ipld-raw@^4.0.0:
multicodec "^1.0.0"
multihashing-async "~0.8.0"
ipld-raw@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/ipld-raw/-/ipld-raw-5.0.0.tgz#06624a9de7a4f5e0cdb3a4e05de3c5ab5bfbb0a8"
integrity sha512-z1Fie224lTtQZbFg+wC5WDY692G3SIpO8vT86yCU83vqpIvasVuV3SzDSv7G36kRxP03PPZOkvKAOFrcjb7gpw==
dependencies:
cids "~0.8.0"
multicodec "^1.0.1"
multihashing-async "~0.8.1"
is-accessor-descriptor@^0.1.6:
version "0.1.6"
resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz#a9e12cb3ae8d876727eeef3843f8a0897b5c98d6"
@ -7373,7 +7473,7 @@ iso-random-stream@^1.1.0:
buffer "^5.4.3"
readable-stream "^3.4.0"
iso-url@~0.4.7:
iso-url@^0.4.7, iso-url@~0.4.7:
version "0.4.7"
resolved "https://registry.yarnpkg.com/iso-url/-/iso-url-0.4.7.tgz#de7e48120dae46921079fe78f325ac9e9217a385"
integrity sha512-27fFRDnPAMnHGLq36bWTpKET+eiXct3ENlCcdcMdk+mjXrb2kw3mhBUg1B7ewAC0kVzlOPhADzQgz1SE6Tglog==
@ -7467,6 +7567,14 @@ it-glob@0.0.7:
fs-extra "^8.1.0"
minimatch "^3.0.4"
it-glob@0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/it-glob/-/it-glob-0.0.8.tgz#b63d24945c18b35de8bb593a8c872fd0257c0cac"
integrity sha512-PmIAgb64aJPM6wwT1UTlNDAJnNgdGrvr0vRr3AYCngcUuq1KaAovuz0dQAmUkaXudDG3EQzc7OttuLW9DaL3YQ==
dependencies:
fs-extra "^8.1.0"
minimatch "^3.0.4"
it-reader@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/it-reader/-/it-reader-2.1.0.tgz#b1164be343f8538d8775e10fb0339f61ccf71b0f"
@ -7474,7 +7582,7 @@ it-reader@^2.0.0:
dependencies:
bl "^4.0.0"
it-tar@^1.1.1:
it-tar@^1.1.1, it-tar@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/it-tar/-/it-tar-1.2.2.tgz#8d79863dad27726c781a4bcc491f53c20f2866cf"
integrity sha512-M8V4a9I+x/vwXTjqvixcEZbQZHjwDIb8iUQ+D4M2QbhAdNs3WKVSl+45u5/F2XFx6jYMFOGzMVlKNK/uONgNIA==
@ -7486,6 +7594,13 @@ it-tar@^1.1.1:
it-reader "^2.0.0"
p-defer "^3.0.0"
it-to-buffer@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/it-to-buffer/-/it-to-buffer-1.0.2.tgz#758141b4ead7f34881cc9ca1b54e2a38c48ffcbe"
integrity sha512-mTuceNC6deSbANZSQFxNRwFlVPvIZkjzxX10mOBxgzzhBGOkih2+OkOyGbhhcGNu/jxd4hk8qkjjOipx+tNIGA==
dependencies:
buffer "^5.5.0"
it-to-stream@^0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/it-to-stream/-/it-to-stream-0.1.2.tgz#7163151f75b60445e86b8ab1a968666acaacfe7b"
@ -7650,6 +7765,15 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"
jsonfile@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.0.1.tgz#98966cba214378c8c84b82e085907b40bf614179"
integrity sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==
dependencies:
universalify "^1.0.0"
optionalDependencies:
graceful-fs "^4.1.6"
jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
@ -8659,7 +8783,7 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
multiaddr-to-uri@^5.0.0:
multiaddr-to-uri@^5.0.0, multiaddr-to-uri@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/multiaddr-to-uri/-/multiaddr-to-uri-5.1.0.tgz#879b55e4170db37cf05e1bce5831de70084933b9"
integrity sha512-rIlMLkw3yk3RJmf2hxYYzeqPXz4Vx7C4M/hg7BVWhmksDW0rDVNMEyoVb0H1A+sh3deHOh5EAFK87XcW+mFimA==
@ -8678,7 +8802,7 @@ multiaddr@^6.0.3, multiaddr@^6.0.6, multiaddr@^6.1.0:
is-ip "^2.0.0"
varint "^5.0.0"
multiaddr@^7.2.1, multiaddr@^7.3.0:
multiaddr@^7.2.1, multiaddr@^7.3.0, multiaddr@^7.4.3:
version "7.5.0"
resolved "https://registry.yarnpkg.com/multiaddr/-/multiaddr-7.5.0.tgz#976c88e256e512263445ab03b3b68c003d5f485e"
integrity sha512-GvhHsIGDULh06jyb6ev+VfREH9evJCFIRnh3jUt9iEZ6XDbyoisZRFEI9bMvK/AiR6y66y6P+eoBw9mBYMhMvw==
@ -8854,6 +8978,11 @@ nanoid@^1.0.2:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-1.3.4.tgz#ad89f62c9d1f4fd69710d4a90953d2893d2d31f4"
integrity sha512-4ug4BsuHxiVHoRUe1ud6rUFT3WUMmjXt1W0quL0CviZQANdan7D8kqN5/maw53hmAApY/jfzMRkC57BNNs60ZQ==
nanoid@^3.0.2, nanoid@^3.1.3:
version "3.1.10"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.10.tgz#69a8a52b77892de0d11cede96bc9762852145bc4"
integrity sha512-iZFMXKeXWkxzlfmMfM91gw7YhN2sdJtixY+eZh9V6QWJWTOiurhpKhBMgr82pfzgSqglQgqYSCowEYsz8D++6w==
nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
@ -9475,7 +9604,7 @@ parse-code-context@^1.0.0:
resolved "https://registry.yarnpkg.com/parse-code-context/-/parse-code-context-1.0.0.tgz#718c295c593d0d19a37f898473268cc75e98de1e"
integrity sha512-OZQaqKaQnR21iqhlnPfVisFjBWjhnMl5J9MgbP8xC+EwoVqbXrq78lp+9Zb3ahmLzrIX5Us/qbvBnaS3hkH6OA==
parse-duration@^0.1.1:
parse-duration@^0.1.1, parse-duration@^0.1.2:
version "0.1.3"
resolved "https://registry.yarnpkg.com/parse-duration/-/parse-duration-0.1.3.tgz#c2c4d45d49513d544e129b2a5a07b9473545d19a"
integrity sha512-hMOZHfUmjxO5hMKn7Eft+ckP2M4nV4yzauLXiw3PndpkASnx5r8pDAMcOAiqxoemqWjMWmz4fOHQM6n6WwETXw==
@ -11411,6 +11540,13 @@ stream-shift@^1.0.0:
resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.1.tgz#d7088281559ab2778424279b0877da3c392d5a3d"
integrity sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==
stream-to-it@^0.2.0:
version "0.2.2"
resolved "https://registry.yarnpkg.com/stream-to-it/-/stream-to-it-0.2.2.tgz#fb3de7917424c354a987c7bc2aab2d0facbd7d94"
integrity sha512-waULBmQpVdr6TkDzci6t1P7dIaSZ0bHC1TaPXDUeJC5PpSK7U3T0H0Zeo/LWUnd6mnhXOmGGDKAkjUCHw5IOng==
dependencies:
get-iterator "^1.0.2"
strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
@ -12102,6 +12238,11 @@ universalify@^0.1.0:
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
universalify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-1.0.0.tgz#b61a1da173e8435b2fe3c67d29b9adf8594bd16d"
integrity sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==
unpipe@1.0.0, unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"