Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Merge branch 'fxg/algod-expand' into tdb/e2e-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Baldwin committed Jul 28, 2020
2 parents 330e7ca + 37a4386 commit 975835e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/common/src/messaging/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum JsonRpcMethod {
SignAllow = "sign-allow",
SignDeny = "sign-deny",
SignTransaction = "sign-transaction",
SendTransaction = "send-transaction",
Algod = "algod",
Indexer = "indexer",
Accounts = "accounts",
Expand Down
1 change: 1 addition & 0 deletions packages/dapp/src/algosigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Wrapper {

public connect: Function = this.task.connect;
public sign: Function = this.task.sign;
public send: Function = this.task.send;
public accounts: Function = this.task.accounts;
public algod: Function = this.task.algod;
public indexer: Function = this.task.indexer;
Expand Down
1 change: 0 additions & 1 deletion packages/dapp/src/fn/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import {JsonPayload,SupportedAlgod} from '@algosigner/common/messaging/types';

export interface ITask {
sign(p: Transaction, e: RequestErrors): Promise<JsonPayload>;
query(m: SupportedAlgod, p: JsonPayload, e: RequestErrors): Promise<JsonPayload>;
}
18 changes: 5 additions & 13 deletions packages/dapp/src/fn/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class Task extends Runtime implements ITask {
);
}

// TODO needs json and raw payload support and complete argument support
sign(
params: Transaction,
error: RequestErrors = RequestErrors.None
Expand All @@ -47,20 +46,13 @@ export class Task extends Runtime implements ITask {
);
}

query(
method: SupportedAlgod,
params: JsonPayload,
send(
params: Transaction,
error: RequestErrors = RequestErrors.None
): Promise<JsonPayload>{

let request: JsonPayload = {
method: method,
params: params
};

): Promise<JsonPayload> {
return MessageBuilder.promise(
JsonRpcMethod.Algod,
request as JsonPayload,
JsonRpcMethod.SendTransaction,
params,
error
);
}
Expand Down
4 changes: 1 addition & 3 deletions packages/extension/src/background/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export class Settings {
port: ""
},
},
apiKey: {
'X-API-key' : 'ZgqaehGkvP6pSNSaoNoy31Nr61BZlhU29E9ERPRU',
}
apiKey: {}
}
}

Expand Down
100 changes: 86 additions & 14 deletions packages/extension/src/background/messaging/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class Task {
setTimeout(function(){
console.log('SENDING MESSAGE AFTER WINDOW CREATION', d)
extensionBrowser.runtime.sendMessage(d);
},100);
}, 300);
}
});
}
Expand All @@ -87,29 +87,74 @@ export class Task {
// Send message with tx info
setTimeout(function(){
extensionBrowser.runtime.sendMessage(d);
},100);
}, 300);
}
});
},
// algod
[JsonRpcMethod.Algod]: (
[JsonRpcMethod.SendTransaction]: (
d: any,
resolve: Function, reject: Function
) => {
const { params } = d.body;
const conn = Settings.getBackendParams(params.ledger, API.Algod);
const sendPath = '/v2/transactions';
let fetchParams : any = {
headers: {
...conn.apiKey,
'Content-Type': 'application/x-binary',
},
method: 'POST',
};
const tx = atob(params.tx).split("").map(x => x.charCodeAt(0));
fetchParams.body = new Uint8Array(tx);


let url = conn.url;
if (conn.port.length > 0)
url += ':' + conn.port;

fetch(`${url}${params.path}`, {
headers: conn.apiKey

fetch(`${url}${sendPath}`, fetchParams)
.then(async (response) => {
d.response = await response.json();
resolve(d);
}).catch((error) => {
d.error = error.message;
reject(d);
})
},
// algod
[JsonRpcMethod.Algod]: (
d: any,
resolve: Function, reject: Function
) => {
const { params } = d.body;
const conn = Settings.getBackendParams(params.ledger, API.Algod);

const contentType = params.contentType ? params.contentType : '';

let fetchParams : any = {
headers: {
...conn.apiKey,
'Content-Type': contentType,
},
method: params.method || 'GET',
};
if (params.body)
fetchParams.body = params.body;

let url = conn.url;
if (conn.port.length > 0)
url += ':' + conn.port;

fetch(`${url}${params.path}`, fetchParams)
.then(async (response) => {
d.response = await response.json();
resolve(d);
}).catch((error) => {
reject(error);
d.error = error.message;
reject(d);
})
},
// Indexer
Expand All @@ -119,18 +164,30 @@ export class Task {
) => {
const { params } = d.body;
const conn = Settings.getBackendParams(params.ledger, API.Indexer);

const contentType = params.contentType ? params.contentType : '';

let fetchParams : any = {
headers: {
...conn.apiKey,
'Content-Type': contentType,
},
method: params.method || 'GET',
};
if (params.body)
fetchParams.body = params.body;

let url = conn.url;
if (conn.port.length > 0)
url += ':' + conn.port;

fetch(`${url}${params.path}`, {
headers: conn.apiKey
})
fetch(`${url}${params.path}`, fetchParams)
.then(async (response) => {
d.response = await response.json();
resolve(d);
}).catch((error) => {
reject(error);
d.error = error.message;
reject(d);
})
},
// Accounts
Expand Down Expand Up @@ -181,15 +238,24 @@ export class Task {
const { from,
to,
fee,
ledger,
amount,
firstRound,
lastRound,
genesisID,
genesisHash,
note } = message.body.params;
const { passphrase } = request.body.params;
console.log('signing, or at least trying')

let ledger
switch (genesisID) {
case "mainnet-v1.0":
ledger = Ledger.MainNet
break;
case "testnet-v1.0":
ledger = Ledger.TestNet
break;
}

const params = Settings.getBackendParams(ledger, API.Algod);
const algod = new algosdk.Algodv2(params.apiKey, params.url, params.port);

Expand Down Expand Up @@ -229,10 +295,16 @@ export class Task {

let signedTxn = algosdk.signTransaction(txn, recoveredAccount.sk);

// Clean class saved request
Task.request = {};

message.response = signedTxn;
console.log('RESPONSING WITH MESSAGE', message)
console.log('signedTxn.blob', signedTxn.blob);
console.log('signedTxn.blob', algosdk.decodeObj(signedTxn.blob));

message.response = {
txID: signedTxn.txID,
blob: btoa(String.fromCharCode(...signedTxn.blob))
};
MessageApi.send(message);
});
return true;
Expand Down

0 comments on commit 975835e

Please sign in to comment.