Last active
March 6, 2018 01:20
-
-
Save elmariachi111/f78beb8b6f7ea41492cf71f6b1cd1078 to your computer and use it in GitHub Desktop.
blockstagram / key generation & manual encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleCryptoJS from 'simple-crypto-js' | |
const blockstack = require( 'blockstack' ); | |
const { getPublicKeyFromPrivate } = require('blockstack'); | |
const { encryptECIES, decryptECIES } = require('blockstack/lib/encryption') | |
... | |
class App extends React.Component { | |
componentDidMount() { | |
if (blockstack.isSignInPending()) { | |
... | |
this.setupKey() | |
... | |
}) | |
} else if (blockstack.isUserSignedIn()) { | |
... | |
this.loadAESKey() | |
... | |
} | |
} | |
setupKey() { | |
const aesKey = SimpleCryptoJS.generateRandom() | |
const publicKey = getPublicKeyFromPrivate(blockstack.loadUserData().appPrivateKey) | |
return blockstack.putFile('key.json', JSON.stringify(publicKey)) | |
.then(() => { | |
const encryptedAesKey = encryptECIES(publicKey, aesKey) | |
return blockstack.putFile(`keys/${this.state.userData.username}`, JSON.stringify(encryptedAesKey)) | |
.then(() => { | |
this.setState({ aesKey }) | |
}) | |
}) | |
.catch(e => { | |
console.log(e); | |
}); | |
} | |
loadAESKey() { | |
blockstack.getFile(`keys/${this.state.userData.username}`) | |
.then((data) => { | |
let encryptedKey = JSON.parse(data) | |
let decryptedKey = decryptECIES(blockstack.loadUserData().appPrivateKey, encryptedKey) | |
this.setState({aesKey: decryptedKey}) | |
}) | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment