Skip to content

Commit

Permalink
fixed tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Dec 5, 2023
1 parent b348e64 commit 25e346d
Showing 1 changed file with 91 additions and 89 deletions.
180 changes: 91 additions & 89 deletions contracts/tasks/ten-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ task("ten:gateway:start:local")
.addParam('rpcUrl', "Node rpc endpoint where the Ten gateway should connect to.")
.addOptionalParam('port', "Port that the Ten gateway will open for incoming requests.", "3001")
.setAction(async function(args, hre) {
const nodeUrl = url.parse(args.rpcUrl)
const nodeUrl = url.parse(args.rpcUrl);
const tenGatewayPath = path.resolve(hre.config.paths.root, "../tools/walletextension/bin/wallet_extension_linux");
const weProcess = spawn(tenGatewayPath, [
`-portWS`, `${args.port}`,
Expand Down Expand Up @@ -123,111 +123,113 @@ task("ten:gateway:stop:docker", "Stops the docker container with matching image
await container?.stop()
});

const { URL } = require('url');

task("ten:gateway:join-authenticate", "Joins and authenticates the gateway for a specific address")
.addParam("address", "The address which to use for authentication")
.setAction(async function(args, hre) {
async function joinGateway() : Promise<string> {
return new Promise((resolve, fail)=> {
const req = http.request({
host: '127.0.0.1',
port: 3000,
path: '/join',
method: 'get',
}, (response)=>{
if (response.statusCode != 200) {
console.error(response);
fail(response.statusCode);
return;
async function joinGateway(url = 'http://127.0.0.1:3000/v1/join') {
return new Promise((resolve, reject) => {
http.get(url, (response) => {
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
// Resolve the new location against the original URL
const newUrl = new URL(response.headers.location, url).toString();
return resolve(joinGateway(newUrl));
}

let chunks : string[] = []
response.on('data', (chunk)=>{
chunks.push(chunk);
});
if (response.statusCode !== 200) {
return reject(new Error(`Server responded with status code: ${response.statusCode}`));
}

response.on('end', ()=> {
resolve(chunks.join(''));
});
});
req.end()
setTimeout(resolve, 15_000);
let chunks = [];
response.on('data', (chunk) => chunks.push(chunk));
response.on('end', () => resolve(Buffer.concat(chunks).toString()));
}).on('error', reject);
});
}

interface SignedData { signature: string, address: string }

async function authenticate(signedData: SignedData, encryptionToken: string) : Promise<string> {
const queryString = `?token=${encodeURIComponent(encryptionToken)}`;
return await new Promise((resolve, fail) => {
const req = http.request({
host: '127.0.0.1',
port: 3000,
path: '/authenticate' + queryString,
method: 'post',
headers: {
'Content-Type': 'application/json'
}
}, (response) => {
let responseData = '';

response.on('data', (chunk) => {
responseData += chunk;
});
// authenticateWithGateway function to authenticate using the token
async function authenticateWithGateway(encryptionToken) {
const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
],
Authentication: [
{ name: "Encryption Token", type: "address" },
],
},
primaryType: "Authentication",
domain: {
name: "Ten",
version: "1.0",
chainId: 443, // TODO @ziga - can we get this from some config in typescript?
},
message: {
"Encryption Token": "0x"+encryptionToken
},
};

const messageData = JSON.stringify(typedData);
const signer = await hre.ethers.getSigner(args.address);

const signature = await signer.provider.send('eth_signTypedData_v4', [
signer.address,
messageData
]);

const signedData = { "signature": signature, "address": args.address };

// Call the authenticate function with the new URL and signed data
const url = `http://127.0.0.1:3000/v1/authenticate?token=${encryptionToken}`;
return authenticate(url, signedData);
}

response.on('end', () => {
if (response.statusCode === 200) {
resolve(responseData); // Resolving with the response data
// authenticate function to make the POST request
async function authenticate(url, signedData) {
return new Promise((resolve, reject) => {
const makeRequest = (url) => {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};

const req = http.request(url, options, (response) => {
if (response.statusCode === 301 || response.statusCode === 302) {
// Handle redirect
const newUrl = new URL(response.headers.location, url).toString();
makeRequest(newUrl); // Resend POST request to new URL
} else if (response.statusCode < 200 || response.statusCode >= 300) {
reject(new Error(`Server responded with status code: ${response.statusCode}`));
} else {
console.log(response.statusMessage);
fail(responseData); // Failing with the response data for more context
let chunks = [];
response.on('data', (chunk) => chunks.push(chunk));
response.on('end', () => resolve(Buffer.concat(chunks).toString()));
}
});
});

req.on('error', (error) => {
fail(error); // Handling request errors
});
req.on('error', reject);
req.write(JSON.stringify(signedData));
req.end();
};

req.write(JSON.stringify(signedData));
req.end();
makeRequest(url);
});
}
}

// get encryption token(userID) from the gateway
const encryptionToken = await joinGateway();

// generate and sign a message
const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
],
Authentication: [
{ name: "Encryption Token", type: "address" },
],
},
primaryType: "Authentication",
domain: {
name: "Ten",
version: "1.0",
chainId: 443, // TODO @ziga - can we get this from some config in typescript?
},
message: {
"Encryption Token": "0x"+encryptionToken
},
};
const messageData = JSON.stringify(typedData);
const signer = await hre.ethers.getSigner(args.address);

const signature = await signer.provider.send('eth_signTypedData_v4', [
signer.address,
messageData
]);

const signedData = { 'signature': signature, 'address': args.address };
const result = await authenticate(signedData, encryptionToken)
console.log("Authentication result is: ", result)
});
try {
let encryptionToken = await joinGateway();
console.log("Encryption token: ", encryptionToken);

let authenticationResult = await authenticateWithGateway(encryptionToken);
console.log("Authentication result: ", authenticationResult);
} catch (error) {
console.error("Error: ", error);
}
});

0 comments on commit 25e346d

Please sign in to comment.