-
-
Save jpopesculian/5131377c7cda040a253f9c85d27cd209 to your computer and use it in GitHub Desktop.
evan create identity test
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
PRIVATE_KEY="PARENT PRIVATE KEY" | |
ACTIVE_PROFILE_ID="PARENT ACTIVE PROFILE ID" | |
ACCOUNT_ID="PARENT ACCOUNT ID" | |
CHILD_ACCOUNT="CHILD ACCOUNT ID (generated by register-identity.js)" | |
CHILD_PRIVATE_KEY="CHILD PRIVATE KEY (generated by register-identity.js)" | |
DBCP_LOGLEVEL="debug" |
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": "evan-tests", | |
"version": "1.0.0", | |
"license": "MIT", | |
"dependencies": { | |
"@evan.network/api-blockchain-core": "^2.3.1", | |
"dotenv": "^8.0.0", | |
"ipfs-api": "^26.1.2", | |
"request": "^2.88.0", | |
"request-promise-native": "^1.0.7", | |
"web3": "^1.0.0-beta.55" | |
}, | |
"devDependencies": {} | |
} |
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
require("dotenv").config(); | |
const Web3 = require("web3"); | |
const request = require("request-promise-native"); | |
// Endpoints | |
const WEB3_ENDPOINT = "wss://testcore.evan.network/ws"; | |
const IDENTITY_ENDPOINT = | |
"https://agents.test.evan.network/api/smart-agents/faucet/identity/create"; | |
// Credentials | |
const ACTIVE_PROFILE_ID = process.env.ACTIVE_PROFILE_ID; | |
const PRIVATE_KEY = process.env.PRIVATE_KEY; | |
// web3 lib | |
const web3 = new Web3(WEB3_ENDPOINT); | |
// create value for Authorization header | |
const createIdentityAuthorization = (accountId, privateKey) => { | |
const timestamp = Date.now(); | |
const message = `${timestamp}`; // just timestamp as message | |
const signature = web3.eth.accounts.sign(message, `0x${privateKey}`) | |
.signature; | |
return `EvanAuth ${accountId},EvanMessage ${message},EvanSignedMessage ${signature}`; | |
}; | |
// send identity creation request | |
const createIdentity = async (accountIdChild, accountIdParent, privateKey) => { | |
const options = { | |
// GET | |
uri: IDENTITY_ENDPOINT, | |
// query string | |
qs: { | |
accountIdParent: accountIdParent, | |
accountIdChild: accountIdChild | |
}, | |
// Authorization header | |
headers: { | |
Authorization: createIdentityAuthorization(accountIdParent, privateKey) | |
}, | |
// accept json | |
json: true | |
}; | |
return request(options); | |
}; | |
// run | |
(async () => { | |
const { address, privateKey } = web3.eth.accounts.create(); | |
try { | |
const result = await createIdentity( | |
address, | |
ACTIVE_PROFILE_ID, | |
PRIVATE_KEY | |
); | |
console.log("SUCCESS"); | |
console.log(result); | |
console.log({ address, privateKey }); | |
process.exit(0); | |
} catch (err) { | |
console.log("FAILED"); | |
console.log(err.error); | |
process.exit(1); | |
} | |
})(); |
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
require("dotenv").config(); | |
const IpfsApi = require("ipfs-api"); | |
const Web3 = require("web3"); | |
const { | |
Ipfs, | |
createDefaultRuntime | |
} = require("@evan.network/api-blockchain-core"); | |
const PARENT_ACCOUNT = process.env.ACTIVE_PROFILE_ID; | |
const CHILD_ACCOUNT = process.env.CHILD_ACCOUNT; | |
const CHILD_PRIVATE_KEY = process.env.CHILD_PRIVATE_KEY; | |
const runtimeConfig = { | |
// account map to blockchain accounts with their private key | |
accountMap: { | |
[CHILD_ACCOUNT]: CHILD_PRIVATE_KEY | |
}, | |
// ipfs configuration for evan.network storage | |
ipfs: { host: "ipfs.test.evan.network", port: "443", protocol: "https" }, | |
// web3 provider config (currently evan.network testcore) | |
web3Provider: "wss://testcore.evan.network/ws" | |
}; | |
// initialize dependencies | |
const provider = new Web3.providers.WebsocketProvider( | |
runtimeConfig.web3Provider, | |
{ | |
clientConfig: { keepalive: true, keepaliveInterval: 5000 } | |
} | |
); | |
const web3 = new Web3(provider, null, { transactionConfirmationBlocks: 1 }); | |
const dfs = new Ipfs({ remoteNode: new IpfsApi(runtimeConfig.ipfs) }); | |
// create runtime | |
(async () => { | |
const runtime = await createDefaultRuntime(web3, dfs, { | |
accountMap: runtimeConfig.accountMap | |
}); | |
try { | |
const response = await runtime.executor.executeSend({ | |
from: CHILD_ACCOUNT, | |
to: PARENT_ACCOUNT, | |
value: web3.utils.toWei("0.025") | |
}); | |
console.log("SUCCESS"); | |
console.log(response); | |
process.exit(0); | |
} catch (err) { | |
console.log("FAILED"); | |
console.log(err); | |
process.exit(1); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment