Last active
February 28, 2019 04:28
-
-
Save thiagosouza/a090810cebad4c788825160c73aaea01 to your computer and use it in GitHub Desktop.
firebase function for ethereum wallet
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'); | |
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 */ | |
}; | |
//This code exports a group of functions called ethereum, so you can deploy or delete all he functions in a faster way | |
exports.ethereum = { | |
/* function name : accountCreate is the name of the function itself | |
options: this functions runsWith the runtimeOpts var (120 seconds and 256MB) | |
trigger: the trigger is a database trigger (firestore) in a namespace called /users/{uid} where | |
users is a collection and uid is any value you define when saving your user | |
triggerType: onCreate means this function will be called just once | |
vars: snap contains data about data in database and context contains data about auth, connection, params, ids, etc | |
*/ | |
accountCreate: functions.runWith(runtimeOpts).firestore.document('/users/{uid}') | |
.onCreate((snap, context) => ethereumAccountCreate(snap, 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 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment