Merge pull request #1479 from Luis-Fernando-Molina/develop
Contribution: Governance: Mnemonic import implemented for creating profile.
This commit is contained in:
6 files changed
+351
-4207
No files matched your search
@@ -160,6 +160,15 @@ exports.newHttpInterface = function newHttpInterface(
|
||||
respondWithContent(JSON.stringify(serverResponse), httpResponse)
|
||||
return
|
||||
}
|
||||
case 'mnemonicToPrivateKey': {
|
||||
|
||||
let serverResponse = await WEB3_SERVER.mnemonicToPrivateKey(
|
||||
params.mnemonic
|
||||
)
|
||||
|
||||
respondWithContent(JSON.stringify(serverResponse), httpResponse)
|
||||
return
|
||||
}
|
||||
default: {
|
||||
respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse)
|
||||
}
|
||||
|
||||
Generated
+274
-4190
File diff suppressed because it is too large.
Load diff
+3
-2
@@ -10,12 +10,13 @@
|
||||
"@octokit/rest": "^18.9.0",
|
||||
"ccxt": "^1.51.98",
|
||||
"child_process": "^1.0.2",
|
||||
"ethers": "^5.4.4",
|
||||
"lookpath": "^1.2.2",
|
||||
"node-fetch": "2.6.1",
|
||||
"open": "^6.4.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"simple-git": "^2.42.0",
|
||||
"web3": "^1.5.0",
|
||||
"ws": "^7.4.6",
|
||||
"node-fetch": "2.6.1"
|
||||
"ws": "^7.4.6"
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ exports.newWeb3Server = function newWeb3Server() {
|
||||
getWalletBalances: getWalletBalances,
|
||||
signData: signData,
|
||||
recoverAddress: recoverAddress,
|
||||
mnemonicToPrivateKey: mnemonicToPrivateKey,
|
||||
initialize: initialize,
|
||||
finalize: finalize,
|
||||
run: run
|
||||
@@ -236,4 +237,29 @@ exports.newWeb3Server = function newWeb3Server() {
|
||||
return { error: 'Could not recover address. ' + err.stack }
|
||||
}
|
||||
}
|
||||
|
||||
async function mnemonicToPrivateKey(mnemonic) {
|
||||
try {
|
||||
const ethers = require('ethers')
|
||||
let wallet = ethers.Wallet.fromMnemonic(mnemonic)
|
||||
|
||||
if (wallet.privateKey !== undefined) {
|
||||
return {
|
||||
address: wallet.address,
|
||||
privateKey: wallet.privateKey,
|
||||
result: 'Ok'
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
address: undefined,
|
||||
privateKey: undefined,
|
||||
result: 'Fail'
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
return { error: 'Could not convert to Private Key. ' + err.stack }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@
|
||||
"config": true
|
||||
},
|
||||
"initialValues": {
|
||||
"config": "{ \"githubUsername\": \"\"}"
|
||||
"config": "{ \"githubUsername\": \"\", \"mnemonic\": \"\"}"
|
||||
},
|
||||
"addLeftIcons": true,
|
||||
"level": 2,
|
||||
|
||||
@@ -13,24 +13,32 @@ function newGovernanceFunctionLibraryProfileConstructor() {
|
||||
Some validations first...
|
||||
*/
|
||||
let githubUsername = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(node.payload, 'githubUsername')
|
||||
let mnemonic = UI.projects.foundations.utilities.nodeConfig.loadConfigProperty(node.payload, 'mnemonic')
|
||||
|
||||
if (githubUsername === undefined || githubUsername === "") {
|
||||
node.payload.uiObject.setErrorMessage("githubUsername config property missing.")
|
||||
return
|
||||
}
|
||||
|
||||
let userProfile = UI.projects.foundations.functionLibraries.uiObjectsFromNodes.addUIObject(
|
||||
node,
|
||||
'User Profile',
|
||||
UI.projects.foundations.spaces.designSpace.workspace.workspaceNode.rootNodes
|
||||
)
|
||||
createWallet()
|
||||
|
||||
createNewAccount()
|
||||
function createWallet() {
|
||||
let params
|
||||
|
||||
function createNewAccount() {
|
||||
let params = {
|
||||
method: 'createWalletAccount',
|
||||
entropy: node.id + (new Date()).valueOf()
|
||||
/*
|
||||
If the user provides a mnemonic then we will get the private key and address from it,
|
||||
otherwise, we will create a new private key and address.
|
||||
*/
|
||||
if (mnemonic === undefined || mnemonic === "") {
|
||||
params = {
|
||||
method: 'createWalletAccount',
|
||||
entropy: node.id + (new Date()).valueOf()
|
||||
}
|
||||
} else {
|
||||
params = {
|
||||
method: 'mnemonicToPrivateKey',
|
||||
mnemonic: mnemonic
|
||||
}
|
||||
}
|
||||
|
||||
let url = 'WEB3' // We will access the default Client WEB3 endpoint.
|
||||
@@ -41,7 +49,6 @@ function newGovernanceFunctionLibraryProfileConstructor() {
|
||||
/* Lets check the result of the call through the http interface */
|
||||
if (err.result !== GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
node.payload.uiObject.setErrorMessage('Call via HTTP Interface failed.')
|
||||
walletAccountNode.payload.uiObject.menu.internalClick('Delete UI Object')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,8 +56,16 @@ function newGovernanceFunctionLibraryProfileConstructor() {
|
||||
|
||||
/* Lets check the result of the method call */
|
||||
if (response.result !== GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
node.payload.uiObject.setErrorMessage('Call to WEB3 Server failed. ' + response.error)
|
||||
return
|
||||
|
||||
if (mnemonic === undefined || mnemonic === "") {
|
||||
node.payload.uiObject.setErrorMessage('Call to WEB3 Server failed. ' + response.error)
|
||||
console.log('Call to WEB3 Server failed. ' + response.error)
|
||||
return
|
||||
} else {
|
||||
node.payload.uiObject.setErrorMessage('Call to WEB3 Server failed. Most likely the Mnemonic provided is not correct.' + response.error)
|
||||
console.log('Call to WEB3 Server failed. Most likely the Mnemonic provided is not correct.' + response.error)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
signUserProfileData(response.address, response.privateKey)
|
||||
@@ -85,6 +100,11 @@ function newGovernanceFunctionLibraryProfileConstructor() {
|
||||
console.log('Call to WEB3 Server failed. ' + response.error)
|
||||
return
|
||||
}
|
||||
let userProfile = UI.projects.foundations.functionLibraries.uiObjectsFromNodes.addUIObject(
|
||||
node,
|
||||
'User Profile',
|
||||
UI.projects.foundations.spaces.designSpace.workspace.workspaceNode.rootNodes
|
||||
)
|
||||
/*
|
||||
We store at the User Profile the Signed githubUsername
|
||||
*/
|
||||
@@ -103,7 +123,11 @@ function newGovernanceFunctionLibraryProfileConstructor() {
|
||||
/*
|
||||
Show nice message.
|
||||
*/
|
||||
node.payload.uiObject.setInfoMessage("Profile Private Key has been successfully created. User Profile installed as a plugin and saved. Use the Private Key at a crypto wallet and delete this node once done.", 10000)
|
||||
if (mnemonic === undefined || mnemonic === "") {
|
||||
node.payload.uiObject.setInfoMessage("Profile Private Key has been successfully created. User Profile installed as a plugin and saved. Use the Private Key at a crypto wallet and delete this node once done.", 10000)
|
||||
} else {
|
||||
node.payload.uiObject.setInfoMessage("Mnemonic successfully imported. User Profile installed as a plugin and saved. Your external wallet was sucessfully linked to your profile.", 10000)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user