Commit 6c5aec49 authored by Mark Harding's avatar Mark Harding

(chore): cli scripts for creating the did's

parent 8cf91290
No related merge requests found
const ADDRESS = "0x619bebd9d803b4f0796c1dd0aece1e9fcadd55d1";
const ABI = [
{
constant: true,
inputs: [
{
name: "",
type: "address"
},
{
name: "",
type: "uint256"
}
],
name: "scores",
outputs: [
{
name: "from",
type: "address"
},
{
name: "to",
type: "address"
},
{
name: "score",
type: "int8"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
inputs: [
{
name: "didRegistryAddress",
type: "address"
}
],
payable: false,
stateMutability: "nonpayable",
type: "constructor"
},
{
constant: true,
inputs: [],
name: "sayHello",
outputs: [
{
name: "",
type: "string"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: false,
inputs: [
{
name: "from",
type: "address"
},
{
name: "to",
type: "address"
},
{
name: "score",
type: "int8"
}
],
name: "issueTrustViaDelegate",
outputs: [
{
name: "",
type: "bool"
}
],
payable: false,
stateMutability: "nonpayable",
type: "function"
},
{
constant: false,
inputs: [
{
name: "to",
type: "address"
},
{
name: "score",
type: "int8"
}
],
name: "issueTrust",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function"
},
{
constant: true,
inputs: [
{
name: "from",
type: "address"
},
{
name: "to",
type: "address"
}
],
name: "getTrust",
outputs: [
{
name: "",
type: "int8"
}
],
payable: false,
stateMutability: "view",
type: "function"
},
{
constant: true,
inputs: [
{
name: "from",
type: "address"
}
],
name: "getTrusts",
outputs: [
{
components: [
{
name: "from",
type: "address"
},
{
name: "to",
type: "address"
},
{
name: "score",
type: "int8"
}
],
name: "",
type: "tuple[]"
}
],
payable: false,
stateMutability: "view",
type: "function"
}
];
module.exports = {
ADDRESS,
ABI
};
const { Credentials } = require("uport-credentials");
const fs = require("fs");
const Web3 = require("web3");
const Tx = require("ethereumjs-tx").Transaction;
const identity = JSON.parse(fs.readFileSync("./identity.json"));
const from = identity.did.split(":")[2];
const simpsons = JSON.parse(fs.readFileSync("./simpsons.json"));
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://rinkeby.infura.io/v3/dcaaa4b596d34bd1867a62bb916a0b31"
)
);
const sendVotes = async (actor, user, score) => {
const from = actor.keypair.did.split(":")[2];
const to = user.keypair.did.split(":")[2];
console.log("from " + from);
privateKey = Buffer.from(actor.keypair.privateKey, "hex");
const count = await web3.eth.getTransactionCount(from);
const balance = await web3.eth.getBalance(from);
const contract = new web3.eth.Contract(
require("./contracts/trust").ABI,
require("./contracts/trust").ADDRESS
);
const ADDRESS = require("./contracts/trust").ADDRESS;
// console.log(count, balance);
var rawTransaction = {
chainId: 4,
from,
gasLimit: web3.utils.toHex(225000),
gasPrice: web3.utils.toHex(10e8), // 1 Gwei
to: ADDRESS,
data: contract.methods
.issueTrust(web3.utils.toHex(to), web3.utils.toHex(score))
.encodeABI(),
nonce: count
};
var tx = new Tx(rawTransaction, { chain: "rinkeby" });
tx.sign(privateKey);
const serializedTx = tx.serialize().toString("hex");
console.log(rawTransaction, serializedTx);
await web3.eth.sendSignedTransaction("0x" + serializedTx);
};
findCharacter = name => {
for (const character of simpsons) {
console.log(character.name, name);
if (character.name === name) {
return character;
}
}
};
const actor = { keypair: identity };
const user = findCharacter(process.argv[3]);
const score = process.argv[4];
console.log(`will give ${user} a score of ${score}`);
sendVotes(actor, user, score);
const { Credentials } = require("uport-credentials");
const fs = require("fs");
const simpsons = [
{ name: "Homer Simpson" },
{ name: "Marge Simpson" },
{ name: "Bart Simpson" },
{ name: "Lisa Simpson" },
{ name: "Maggie Simpson" },
{ name: "Santa's Little Helper" },
{ name: "Snowball II/V" },
{ name: "Abraham Simpson" },
{ name: "Apu Nahasapeemapetilon" },
{ name: "Barney Gumble" },
{ name: "Bleeding Gums Murphy[B]" },
{ name: "Chief Clancy Wiggum" },
{ name: "Dewey Largo" },
{ name: "Eddie" },
{ name: "Edna Krabappel" },
{ name: "Itchy" },
{ name: "Janey Powell" },
{ name: "Jasper Beardsley" },
{ name: "Kent Brockman" },
{ name: "Krusty The Clown" },
{ name: "Lenny Leonard" },
{ name: "Lou" },
{ name: "Martin Prince" },
{ name: "Marvin Monroe[C]" },
{ name: "Milhouse Van Houten" },
{ name: "Moe Szyslak" },
{ name: "Mr. Burns" },
{ name: "Ned Flanders" },
{ name: "Otto Mann" },
{ name: "Patty Bouvier" },
{ name: "Ralph Wiggum" },
{ name: "Reverend Timothy Lovejoy" },
{ name: "Scratchy" },
{ name: "Selma Bouvier" },
{ name: "Seymour Skinner" },
{ name: "Sherri" },
{ name: "Terri" },
{ name: "Sideshow Bob" }
];
for (let i in simpsons) {
const character = simpsons[i];
const keypair = Credentials.createIdentity();
simpsons[i] = { ...character, keypair };
}
fs.writeFileSync("./simpsons.json", JSON.stringify(simpsons, null, 2));
const { Credentials } = require("uport-credentials");
const fs = require("fs");
const Web3 = require("web3");
const Tx = require("ethereumjs-tx").Transaction;
const identity = JSON.parse(fs.readFileSync("./identity.json"));
const from = identity.did.split(":")[2];
const simpsons = JSON.parse(fs.readFileSync("./simpsons.json"));
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://rinkeby.infura.io/v3/dcaaa4b596d34bd1867a62bb916a0b31"
)
);
const sendFunds = async address => {
console.log("from " + from);
privateKey = Buffer.from(identity.privateKey, "hex");
const count = await web3.eth.getTransactionCount(from);
const balance = await web3.eth.getBalance(from);
console.log(count, balance);
var rawTransaction = {
chainId: 4,
from,
gasLimit: web3.utils.toHex(25000),
gasPrice: web3.utils.toHex(10e8), // 1 Gwei
to: address,
value: web3.utils.toHex("50000000000000000"),
// data: contract.methods.transfer(toAddress, amount).encodeABI(),
nonce: count
};
var tx = new Tx(rawTransaction, { chain: "rinkeby" });
tx.sign(privateKey);
const serializedTx = tx.serialize().toString("hex");
console.log(rawTransaction, serializedTx);
await web3.eth.sendSignedTransaction("0x" + serializedTx);
//.on("transactionHash", console.log);
};
(async () => {
for (const character of simpsons) {
await sendFunds(character.keypair.did.split(":")[2]);
}
})();
const { Credentials } = require("uport-credentials");
const fs = require("fs");
const Web3 = require("web3");
const Tx = require("ethereumjs-tx").Transaction;
const identity = JSON.parse(fs.readFileSync("./identity.json"));
const from = identity.did.split(":")[2];
const simpsons = JSON.parse(fs.readFileSync("./simpsons.json"));
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://rinkeby.infura.io/v3/dcaaa4b596d34bd1867a62bb916a0b31"
)
);
const get = async actor => {
const address = actor.keypair.did.split(":")[2];
const contract = new web3.eth.Contract(
require("./contracts/trust").ABI,
require("./contracts/trust").ADDRESS
);
console.log(await contract.methods.getTrusts(address).call());
};
findCharacter = name => {
for (const character of simpsons) {
if (character.name === name) {
return character;
}
}
};
const actor = findCharacter(process.argv[2]);
console.log(`${actor.name} has the following trust scores`);
get(actor);
const { Credentials } = require("uport-credentials");
const fs = require("fs");
const Web3 = require("web3");
const Tx = require("ethereumjs-tx").Transaction;
const identity = JSON.parse(fs.readFileSync("./identity.json"));
const from = identity.did.split(":")[2];
const simpsons = JSON.parse(fs.readFileSync("./simpsons.json"));
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://rinkeby.infura.io/v3/dcaaa4b596d34bd1867a62bb916a0b31"
)
);
const sendVotes = async (actor, user, score) => {
const from = actor.keypair.did.split(":")[2];
const to = user.keypair.did.split(":")[2];
console.log("from " + from);
privateKey = Buffer.from(actor.keypair.privateKey, "hex");
const count = await web3.eth.getTransactionCount(from);
const balance = await web3.eth.getBalance(from);
const contract = new web3.eth.Contract(
require("./contracts/trust").ABI,
require("./contracts/trust").ADDRESS
);
const ADDRESS = require("./contracts/trust").ADDRESS;
// console.log(count, balance);
var rawTransaction = {
chainId: 4,
from,
gasLimit: web3.utils.toHex(225000),
gasPrice: web3.utils.toHex(10e8), // 1 Gwei
to: ADDRESS,
data: contract.methods
.issueTrust(web3.utils.toHex(to), web3.utils.toHex(score))
.encodeABI(),
nonce: count
};
var tx = new Tx(rawTransaction, { chain: "rinkeby" });
tx.sign(privateKey);
const serializedTx = tx.serialize().toString("hex");
console.log(rawTransaction, serializedTx);
await web3.eth.sendSignedTransaction("0x" + serializedTx);
};
findCharacter = name => {
for (const character of simpsons) {
console.log(character.name, name);
if (character.name === name) {
return character;
}
}
};
const actor = findCharacter(process.argv[2]);
const user = findCharacter(process.argv[3]);
const score = process.argv[4];
console.log(`${actor} will give ${user} a score of ${score}`);
sendVotes(actor, user, score);
const express = require("express");
const bodyParser = require("body-parser");
const ngrok = require("ngrok");
const decodeJWT = require("did-jwt").decodeJWT;
const { Credentials } = require("uport-credentials");
const transports = require("uport-transports").transport;
const message = require("uport-transports").message.util;
const fs = require("fs");
const crypto = require("crypto");
const identity = JSON.parse(fs.readFileSync("./identity.json"));
const EthrDID = require("ethr-did");
const HttpProvider = require("ethjs-provider-http");
const Web3 = require("web3");
let endpoint = "";
const TRUST_CONTRACT_ADDRESS = "0x619bebd9d803b4f0796c1dd0aece1e9fcadd55d1";
const DID_CONTRACT_ADDRESS = "0x6afd852ac0a9bf29e2841453531bba322908732e";
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://rinkeby.infura.io/v3/dcaaa4b596d34bd1867a62bb916a0b31"
)
);
const app = express();
app.use(bodyParser.json({ type: "*/*" }));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
//setup Credentials object with newly created application identity.
const credentials = new Credentials({
...{ appName: "Minds t3" },
...identity
});
const provider = new HttpProvider(
"https://rinkeby.infura.io/v3/dcaaa4b596d34bd1867a62bb916a0b31"
);
const authenticatedRequests = new Map();
/**
* Collect a QR code and return to the client
*/
app.get("/unlock", async (req, res) => {
try {
const sessionId = crypto.randomBytes(64).toString("hex");
const requestToken = await credentials.createDisclosureRequest({
requested: ["name", "avatar"],
notifications: true,
accountType: "keypair",
network_id: "0x4",
callbackUrl: endpoint + "/callback/unlock?sessionId=" + sessionId
});
res.send({
requestToken,
sessionId
});
} catch (err) {}
});
app.post("/vote", async (req, res) => {
const to = req.body.to;
const score = req.body.score;
const profile = req.body.profile;
const from = req.body.profile.did.split(":")[2];
const push = transports.push.send(profile.pushToken, profile.boxPub);
const txObject = {
to: TRUST_CONTRACT_ADDRESS,
net: "0x4",
value: null,
fn:
"issueTrustViaDelegate(address " +
from +
", address " +
to +
", int8 " +
score +
")"
};
const token = await credentials.createTxRequest(txObject, {
callbackUrl: `${endpoint}/callback/vote`,
callback_type: "post",
networkId: "0x4"
});
console.log(token);
push(token);
res.send({
token
});
});
// uPort will call this endpoint directly
app.post("/callback/unlock", async (req, res) => {
const jwt = req.body.access_token;
const sessionId = req.query.sessionId;
console.log(`[callback/unlock]: ${sessionId} `);
try {
const profile = await credentials.authenticateDisclosureResponse(jwt);
const identityAddress = profile.did.split(":")[2]; // Nasty
const delegateAddress = identity.did.split(":")[2]; // Nastier
console.log("[credentials]: ", profile);
authenticatedRequests.set(sessionId, profile);
res.send({
status: "success"
});
} catch (err) {
console.log(err);
res.send({
status: "error",
message: err.toString(),
jwt
});
}
});
app.post("/callback/vote", async (req, res) => {
console.log("vote callback received", req);
});
app.post("/poll/unlock", async (req, res) => {
const sessionId = req.body.sessionId;
console.log(`[poll/unlock]: ${sessionId} `);
const profile = authenticatedRequests.get(sessionId.toString());
if (profile) {
console.log(`[poll/unlock]: ${sessionId}: success`);
res.send({
status: "ok",
profile
});
} else {
res.send({
status: "error",
message: "Not yet authenticated"
});
}
});
// run the app server and tunneling service
const server = app.listen(8088, () => {
ngrok
.connect({
proto: "http",
subdomain: "t3-backend",
addr: 8088
})
.then(ngrokUrl => {
endpoint = ngrokUrl;
console.log(`Login Service running, open at ${endpoint}`);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment