Last active
May 13, 2020 05:19
-
-
Save Tschuck/5b9dbf7813464b56f9f5f5342388587c to your computer and use it in GitHub Desktop.
Create new profile with identity
This file contains 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
module.exports = { | |
// ============================================================================================== | |
// account that holds the money | |
account: { | |
accountId: '', | |
privateKey: '', | |
encryptionKey: '', | |
}, | |
// ============================================================================================== | |
// | |
// runtimeConfig: { | |
// ipfsConfig: { | |
// host: 'ipfs.evan.network', | |
// port: '443', | |
// protocol: 'https', | |
// }, | |
// web3Provider: 'wss://core.evan.network/ws', | |
// }, | |
runtimeConfig: { | |
ipfsConfig: { | |
host: 'ipfs.test.evan.network', | |
port: '443', | |
protocol: 'https', | |
}, | |
web3Provider: 'wss://testcore.evan.network/ws', | |
}, | |
} |
This file contains 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 Web3 = require('web3') | |
const { createDefaultRuntime, Ipfs, Onboarding, Profile, keystore, } = require('@evan.network/api-blockchain-core'); | |
const config = require('./config') | |
async function getRuntime( | |
{ web3Provider, ipfsConfig }, | |
{ privateKey, encryptionKey, accountId, password } | |
) { | |
const provider = new Web3.providers.WebsocketProvider( | |
web3Provider, | |
{ clientConfig: { keepalive: true, keepaliveInterval: 5000 } | |
}) | |
const web3 = new Web3(provider, { transactionConfirmationBlocks: 1 }) | |
const dfs = new Ipfs({ dfsConfig: ipfsConfig, }) | |
let keyConfig; | |
if (encryptionKey) { | |
const sha3Account = web3.utils.soliditySha3(accountId) | |
const sha9Account = web3.utils.soliditySha3(sha3Account, sha3Account) | |
keyConfig = { | |
[ sha3Account ]: encryptionKey, | |
[ sha9Account ]: encryptionKey, | |
} | |
} else { | |
keyConfig = { | |
[accountId]: password, | |
} | |
} | |
// create runtime | |
return createDefaultRuntime( | |
web3, | |
dfs, | |
{ | |
accountMap: { | |
[ accountId ]: privateKey, | |
}, | |
keyConfig, | |
useIdentity: true, | |
}, | |
) | |
} | |
async function generateNewUser(accountName, mnemonic, password) { | |
console.log(`generating user for`) | |
console.log(` - accountName: ${accountName}`) | |
console.log(` - mnemonic: ${mnemonic}`) | |
console.log(` - password: ${password}`) | |
const baseRuntime = await getRuntime(config.runtimeConfig, config.account); | |
// get vault for accessing mnemonic data | |
const vault = await new Promise((resolve, reject) => { | |
keystore.createVault({ | |
hdPathString: 'm/45\'/62\'/13\'/7', | |
password, | |
seedPhrase: mnemonic, | |
}, (err, vault) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(vault); | |
} | |
}); | |
}); | |
const pwDerivedKey = await new Promise((resolve, reject) => { | |
vault.keyFromPassword(password, (err, pwDerivedKey) => { | |
if (err) { | |
reject(); | |
} else { | |
resolve(pwDerivedKey); | |
} | |
}); | |
}); | |
// get public and private key for mnemonic | |
await vault.generateNewAddress(pwDerivedKey, 1); | |
const addresses = await vault.getAddresses(); | |
const accountId = baseRuntime.web3.utils.toChecksumAddress(addresses[0]); | |
const privateKey = await vault.exportPrivateKey(accountId.toLowerCase(), pwDerivedKey); | |
// generate runtimes | |
let newUserRuntime = await getRuntime(config.runtimeConfig, { | |
accountId, | |
privateKey, | |
password, | |
}); | |
// send eve | |
await baseRuntime.executor.executeSend({ | |
from: baseRuntime.underlyingAccount, | |
to: accountId, | |
value: baseRuntime.web3.utils.toWei('5', 'ether'), | |
}); | |
// create identity contract and register in the user registry | |
const identityContract = await newUserRuntime.executor.createContract( | |
'VerificationHolder', [accountId], { from: accountId, gas: 3000000, }) | |
await newUserRuntime.verifications.ensureStorage() | |
// register the new user in the registry | |
await newUserRuntime.executor.executeContractTransaction( | |
newUserRuntime.verifications.contracts.storage, | |
'registerUser', | |
{ from: accountId, }, | |
identityContract.options.address, | |
) | |
console.log(`registered identity: ${identityContract.options.address}`); | |
newUserRuntime = await getRuntime(config.runtimeConfig, { | |
accountId, | |
privateKey, | |
encryptionKey: newUserRuntime.web3.utils | |
.keccak256(identityContract.options.address + password) | |
.replace(/0x/g, ''), | |
}); | |
// create profile contract and fill it | |
await Onboarding.createProfile( | |
newUserRuntime, | |
{ | |
accountDetails: { | |
accountName, | |
profileType: 'user', | |
} | |
} | |
); | |
const newEncryptionKey = newUserRuntime.web3.utils | |
.keccak256(identityContract.options.address + password) | |
.replace(/0x/g, '') | |
await newUserRuntime.profile.addContactKey(newUserRuntime.activeIdentity, 'dataKey', newEncryptionKey); | |
await newUserRuntime.profile.storeForAccount(newUserRuntime.profile.treeLabels.addressBook); | |
newUserRuntime = await getRuntime(config.runtimeConfig, { | |
accountId, | |
privateKey, | |
encryptionKey: newEncryptionKey, | |
}); | |
console.log('new profile:') | |
console.log(` - mnemonic: ${mnemonic}`) | |
console.log(` - password: ${password}`) | |
console.log(` - account: ${newUserRuntime.underlyingAccount}`) | |
console.log(` - identity: ${newUserRuntime.activeIdentity}`) | |
console.log(` - environment: ${newUserRuntime.environment}`) | |
console.log(` - publicKey: ${await newUserRuntime.profile.getPublicKey()}`) | |
console.log(` - encryptionKey: ${newEncryptionKey}`) | |
} | |
const mnemonic = Onboarding.createMnemonic(); | |
const password = 'Test1234'; | |
generateNewUser( | |
'new backend user', | |
mnemonic, | |
password, | |
); |
This file contains 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
{ | |
"name": "notary-migration", | |
"version": "1.0.0", | |
"description": "", | |
"main": "createProfileWithIdentity.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@evan.network/api-blockchain-core": "^2.18.0", | |
"web3": "2.0.0-alpha" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment