Last active
February 25, 2019 20:45
-
-
Save thiagosouza/4e2d43c1489b1ed935328d4424747cd2 to your computer and use it in GitHub Desktop.
[MVP Blockchain Serverless] #blockchain #serverless #firebase #google-cloud
This file contains hidden or 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
#node 8 | |
#https://nodejs.org/en/download/ | |
#nvm | |
#https://github.com/creationix/nvm#install-script | |
cd /tmp | |
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash | |
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash | |
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
source ~/.bashrc | |
#nvm use node 8 | |
nvm use 8 | |
#npm | |
#https://docs.npmjs.com/cli/install | |
curl -L https://www.npmjs.com/install.sh | sh |
This file contains hidden or 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
#firebase | |
#https://firebase.google.com | |
mkdir ~/mvp-serverless-blockchain | |
#initialize npm package | |
npm init | |
#install firebase globally | |
npm install -g firebase-tools | |
firebase login | |
firebase init | |
#functions | |
cd ~/mvp-serverless-blockchain/functions | |
npm install firebase-functions@latest firebase-admin@latest --save | |
#firebase login | |
firebase login | |
firebase init #or firebase init functions | |
#select functions | |
#language = javascript | |
#ESLint = y | |
#install packages = y | |
#Project creation is only available from the Firebase Console | |
#Please visit https://console.firebase.google.com to create a new project, then run firebase use --add | |
#dashboard | |
#https://console.firebase.google.com/project/mvp-blockchain-serverless/overview | |
#https://console.cloud.google.com/home/dashboard?project=mvp-blockchain-serverless&folder=&organizationId= | |
#firebase functions | |
#https://console.firebase.google.com/u/0/project/mvp-blockchain-serverless/functions | |
#https://console.cloud.google.com/functions/list?project=mvp-blockchain-serverless&folder&organizationId | |
#firebase pricing | |
#https://cloud.google.com/functions/pricing | |
firebase use --add mvp-blockchain-serverless | |
#https://firebase.google.com/docs/functions/get-started |
This file contains hidden or 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
#https://etherscan.io/address/0xba484a032606c801177b89f59e36deea310764c1 |
This file contains hidden or 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
cd ~/mvp-serverless-blockchain | |
firebase init hosting |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>FRONT MVP</title> | |
<script src="https://www.gstatic.com/firebasejs/ui/3.5.2/firebase-ui-auth__en.js"></script> | |
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/3.5.2/firebase-ui-auth.css" /> | |
<script src="https://www.gstatic.com/firebasejs/5.8.2/firebase.js"></script> | |
<script> | |
// Initialize Firebase | |
var config = { | |
apiKey: "AIzaSyB6f02YMMZuIqTanUnH4-jV-N44ItTUr5c", | |
authDomain: "mvp-blockchain-serverless.firebaseapp.com", | |
databaseURL: "https://mvp-blockchain-serverless.firebaseio.com", | |
projectId: "mvp-blockchain-serverless", | |
storageBucket: "mvp-blockchain-serverless.appspot.com", | |
messagingSenderId: "775565731884" | |
}; | |
firebase.initializeApp(config); | |
</script> | |
<script type="text/javascript"> | |
initApp = function () { | |
firebase.auth().onAuthStateChanged(function (user) { | |
if (user) { | |
// User is signed in. | |
var displayName = user.displayName; | |
var email = user.email; | |
var emailVerified = user.emailVerified; | |
var photoURL = user.photoURL; | |
var uid = user.uid; | |
var phoneNumber = user.phoneNumber; | |
var providerData = user.providerData; | |
user.getIdToken().then(function (accessToken) { | |
document.getElementById('sign-in-status').textContent = 'Signed in'; | |
document.getElementById('sign-in').textContent = 'Sign out'; | |
document.getElementById('account-details').textContent = JSON.stringify({ | |
displayName: displayName, | |
email: email, | |
emailVerified: emailVerified, | |
phoneNumber: phoneNumber, | |
photoURL: photoURL, | |
uid: uid, | |
accessToken: accessToken, | |
providerData: providerData | |
}, null, ' '); | |
}); | |
} else { | |
// User is signed out. | |
document.getElementById('sign-in-status').textContent = 'Signed out'; | |
document.getElementById('sign-in').textContent = 'Sign in'; | |
document.getElementById('account-details').textContent = 'null'; | |
} | |
}, function (error) { | |
console.log(error); | |
}); | |
}; | |
window.addEventListener('load', function () { | |
initApp() | |
}); | |
</script> | |
</head> | |
<body> | |
<script type="text/javascript" src="web3.min.js"></script> | |
<h1>Welcome to My Awesome App</h1> | |
<div id="firebaseui-auth-container"></div> | |
<h1>Welcome to My Awesome App</h1> | |
<div id="sign-in-status"></div> | |
<div id="sign-in"></div> | |
<div id="account-details"></div> | |
<script> | |
window.addEventListener('load', async () => { | |
// Modern dapp browsers... | |
if (window.ethereum) { | |
window.web3020 = new Web3(ethereum); | |
try { | |
// Request account access if needed | |
await ethereum.enable(); | |
// Acccounts now exposed | |
// web3.eth.sendTransaction({/* ... */ }); | |
console.log("aaa"); | |
var accounts = await web3.eth.accounts; | |
console.log("bbb"); | |
setInterval(() => { | |
web3.eth.getBalance(document.querySelector("#account").value, (error, result) => { | |
document.querySelector("#balance").value = result; | |
}) | |
web3.eth.getBalance(document.querySelector("#destination").value, (error, result) => { | |
document.querySelector("#destinationBalance").value = result; | |
}) | |
}, 3 * 1000) | |
document.querySelector("#account").value = accounts[0]; | |
document.querySelector("#transfer").addEventListener("click", async (e) => { | |
var transactionObject = { | |
from: document.querySelector("#account").value, | |
to: document.querySelector("#destination").value, | |
value: document.querySelector("#value").value * Math.pow(10, 18) | |
} | |
// e.target.value | |
try { | |
// var { error, hash } = web3.eth.sendTransaction(transactionObject, (error, result) => { | |
// console.log(error, result); | |
// }); | |
} catch (error) { | |
} | |
}) | |
// var balance = web3.eth.getBalance(accounts[0]); | |
} catch (error) { | |
// User denied account access... | |
} | |
} | |
// Legacy dapp browsers... | |
else if (window.web3) { | |
window.web3 = new Web3(web3.currentProvider); | |
// Acccounts always exposed | |
web3.eth.sendTransaction({/* ... */ }); | |
} | |
// Non-dapp browsers... | |
else { | |
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!'); | |
} | |
}); | |
</script> | |
<!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script> --> | |
<input type="text" id="account" name="account" placeholder="ethereum wallet address"> | |
<br> | |
<input type="text" id="balance" name="balance" placeholder="balance"> | |
<br /> | |
<br /> | |
<input type="text" id="destination" name="destination" placeholder="destination"> | |
<br> | |
<input type="text" id="destinationBalance" name="balance" placeholder="balance"> | |
<br /> | |
<br /> | |
<input type="text" id="value" name="value" placeholder="value"> | |
<br /> | |
<br /> | |
<button id="transfer" name="transferir">Transferir</button> | |
<script type="text/javascript"> | |
// FirebaseUI config. | |
var uiConfig = { | |
signInSuccessUrl: '#', | |
signInOptions: [ | |
// Leave the lines as is for the providers you want to offer your users. | |
firebase.auth.GoogleAuthProvider.PROVIDER_ID, | |
firebase.auth.EmailAuthProvider.PROVIDER_ID | |
], | |
// tosUrl and privacyPolicyUrl accept either url string or a callback | |
// function. | |
// Terms of service url/callback. | |
tosUrl: '<your-tos-url>', | |
// Privacy policy url/callback. | |
privacyPolicyUrl: function () { | |
window.location.assign('<your-privacy-policy-url>'); | |
} | |
}; | |
// Initialize the FirebaseUI Widget using Firebase. | |
var ui = new firebaseui.auth.AuthUI(firebase.auth()); | |
// The start method will wait until the DOM is loaded. | |
ui.start('#firebaseui-auth-container', uiConfig); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
#https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md |
This file contains hidden or 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
//const functions = require('firebase-functions'); | |
// // Create and Deploy Your First Cloud Functions | |
// // https://firebase.google.com/docs/functions/write-firebase-functions | |
// | |
// exports.helloWorld = functions.https.onRequest((request, response) => { | |
// response.send("Hello from Firebase!"); | |
// }); | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
var auth = admin.auth(); | |
var db = admin.firestore(); | |
db.settings({ timestampsInSnapshots: true }); | |
// const { userCreate } = require("./userCreate"); | |
const { ethereumAccountCreate } = require("./ethereumAccountCreate"); | |
// runtime options: https://firebase.google.com/docs/functions/manage-functions | |
var runtimeOpts = { | |
timeoutSeconds: 120, /* 1-540 (9 minutes) */ | |
memory: '256MB' /* 128MB 256MB 512MB 1GB 2GB */ | |
}; | |
// exports.workflows = { | |
// userCreate: functions.runWith(runtimeOpts).auth.user().onCreate(user => userCreate(user)) | |
// } | |
exports.ethereum = { | |
// userCreate: functions.runWith(runtimeOpts).auth.user().onCreate(user => ethereumUserCreate(user)), | |
accountCreate: functions.firestore.document('/users/{uid}').onCreate((snap, context) => ethereumAccountCreate(snap, context)) | |
// accountDecrypt: functions.firestore.document('/users/{uid}').onWrite((change, context) => ethereumDecryptWallet(change, context)), | |
// deployContract: functions.runWith(runtimeOpts).firestore.document('/policies/{policyId}') | |
// .onWrite((change, context) => ethereumDeployContract(change, context)), | |
// getContractAddress: functions.runWith(runtimeOpts).firestore.document('/policies/{policyId}') | |
// .onWrite((change, context) => ethereumGetContractAddress(change, context)), | |
} |
This file contains hidden or 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
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const Storage = require('@google-cloud/storage'); | |
const db = admin.firestore(); | |
//Web3js 1.0 dependencies | |
const { Accounts } = require('web3-eth-accounts') | |
const accounts = new Accounts('https://kovan.infura.io/v3/f1be797452774133a11f7688f5316a47'); | |
const fs = require('fs'); | |
const path = require('path'); | |
var bucket = admin.storage().bucket('gs://mvp-blockchain-serverless'); | |
exports.ethereumAccountCreate = async function ethereumAccountCreate(snap, context) { | |
const pass = "12345678"; | |
try { | |
var accCreated = accounts.create(); | |
var accEncrypted = accounts.encrypt(accCreated.privateKey, pass); | |
if (!accEncrypted) | |
console.error(new Error("Erro ao encryptar account")); | |
} catch (error) { | |
console.log("catch"); | |
return snap.ref.collection('/errors/').add(Object.assign({}, { error: error })); | |
} | |
const fileData = JSON.stringify(accEncrypted); | |
const fileName = context.params.uid + '.json'; | |
const bucketfilePath = path.join(`/users/${context.params.uid}/ethereum/`, fileName); | |
let file = bucket.file(bucketfilePath); | |
await file.save(fileData, { contentType: "application/json" }); | |
await snap.ref.update({ "ethereum": { "address": accCreated.address, "privateKey": accCreated.privateKey, walletFile: accEncrypted } }); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment