Skip to content

Instantly share code, notes, and snippets.

@elmariachi111
Last active March 6, 2018 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmariachi111/f78beb8b6f7ea41492cf71f6b1cd1078 to your computer and use it in GitHub Desktop.
Save elmariachi111/f78beb8b6f7ea41492cf71f6b1cd1078 to your computer and use it in GitHub Desktop.
blockstagram / key generation & manual encryption
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