Skip to content

Instantly share code, notes, and snippets.

@CHAOWEICHIU
Last active March 7, 2019 23:45
Show Gist options
  • Save CHAOWEICHIU/0b6df290ff3bb32b5768df85f95be94c to your computer and use it in GitHub Desktop.
Save CHAOWEICHIU/0b6df290ff3bb32b5768df85f95be94c to your computer and use it in GitHub Desktop.
implementation for storing encrypted zip file on the blockchain - 1/2 encrypt and upload
const IpfsHttpClient = require('ipfs-http-client')
const EthCrypto = require('eth-crypto')
const intoStream = require('into-stream')
const ipfs = IpfsHttpClient({
host: 'ipfs.infura.io',
port: '5001',
protocol: 'https',
})
const keyPrivate = '0x61ce8b95ca5fd6f55cd97ac60817777bdf64f1670e903758ce53efc32c3dffeb'
const keyPublic = 'fff49b58b83104ff16875452852466a46c7169ba4e368d11830c9170624e0a9509080a05a38c18841718ea4fc13483ac467d3e2d728d41ff16b73b9c943734f8'
function encryptStringWithPublicKey({ message, publicKey }) {
return EthCrypto
.encryptWithPublicKey(publicKey, message)
.then(dataObject => EthCrypto
.cipher
.stringify(dataObject))
}
function decryptStringWithPrivateKey({ encryptedString, privateKey }) {
const decompressedString = EthCrypto.cipher.parse(encryptedString)
return EthCrypto.decryptWithPrivateKey(privateKey, decompressedString)
}
function handleFileSelected(e) {
const [file] = Array.from(e.target.files)
const reader = new FileReader()
reader.readAsBinaryString(file)
reader.onload = (event) => {
encryptStringWithPublicKey({
message: event.target.result,
publicKey: keyPublic,
})
.then(encryptedString => ipfs.add(
intoStream(encryptedString),
{},
(err, data) => {
const hash = data[0].hash
// store this hash from ipfs on smart contract
},
))
}
}
<input
onChange={this.handleFileSelected}
type="file"
accept=".zip"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment