Skip to content

Commit

Permalink
initial commit - baseic firebase project
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagosouza committed Feb 28, 2019
1 parent 957fdba commit 864e24c
Show file tree
Hide file tree
Showing 5 changed files with 955 additions and 15 deletions.
4 changes: 2 additions & 2 deletions functions/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 6
"ecmaVersion": 2017
},
"plugins": [
"promise"
Expand Down Expand Up @@ -120,4 +120,4 @@
// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}
}
40 changes: 40 additions & 0 deletions functions/ethereumAccountCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const admin = require('firebase-admin');

const { Accounts } = require('web3-eth-accounts') //Web3js 1.0 dependencies
const accounts = new Accounts('https://kovan.infura.io/v3/f1be797452774133a11f7688f5316a47');

const path = require('path');

const 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("Error while encrypting account"));

} catch (error) {
console.error(new Error("Error while creating account", error));
}

let fileData = JSON.stringify(accEncrypted);
let fileName = `${context.params.uid}.json`;
let 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;
}
23 changes: 17 additions & 6 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
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 admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });

const { ethereumAccountCreate } = require("./ethereumAccountCreate");

const runtimeOpts = { /* runtime options: https://firebase.google.com/docs/functions/manage-functions */
timeoutSeconds: 120, /* 1-540 (9 minutes) */
memory: '256MB' /* 128MB 256MB 512MB 1GB 2GB */
};

exports.ethereum = {
accountCreate: functions.runWith(runtimeOpts).firestore.document('/users/{uid}')
.onCreate((snap, context) => ethereumAccountCreate(snap, context))
}
Loading

0 comments on commit 864e24c

Please sign in to comment.