Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Geneste committed Mar 26, 2021
2 parents 9a21b9f + 95871fd commit 209bf51
Show file tree
Hide file tree
Showing 31 changed files with 1,288 additions and 699 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ecto",
"version": "1.0.2",
"version": "1.1.0",
"private": true,
"scripts": {
"build": "vue-cli-service build",
Expand Down
Binary file added public/assets/game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/gfest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
136 changes: 120 additions & 16 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/// <reference types="chrome"/>
import Vue from "vue";
import { PhantasmaAPI } from "@/phan-js";
import { state } from "@/popup/PopupState";
import { state, WalletAccount } from "@/popup/PopupState";
import VueI18n from "vue-i18n";
import { messages, defaultLocale } from "@/i18n";
import { getEthBalances } from "@/ethereum";
import { getNeoBalances } from "@/neo";

Vue.use(VueI18n);

Expand Down Expand Up @@ -53,6 +55,14 @@ interface ISignTxResponse extends IWalletLinkResponse {
}

let authorizations: IAuthorization[] = [];
let accounts: WalletAccount[] = [];
let currentAccountIndex = 0;

function currentAccount() {
return currentAccountIndex < accounts.length
? accounts[currentAccountIndex]
: null;
}

chrome.tabs.onUpdated.addListener(function(activeInfo) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
Expand Down Expand Up @@ -114,6 +124,11 @@ function isValidRequest(args: string[]): boolean {
return false;
}

function getAddressFromToken(token: string): string | undefined {
const auth = authorizations.find((a) => a.token == token);
return auth?.address;
}

function getRequestAddress(
args: string[]
): { address: string; version: string } | undefined {
Expand All @@ -129,13 +144,25 @@ function getRequestAddress(

chrome.storage.local.get((items) => {
authorizations = items.authorizations ? items.authorizations : [];
currentAccountIndex = items.currentAccountIndex
? items.currentAccountIndex
: 0;
accounts = items.accounts
? items.accounts.filter((a: WalletAccount) => a.type !== "wif")
: [];
});

chrome.storage.onChanged.addListener((changes, area) => {
if (area == "local") {
if (changes.authorizations) {
authorizations = changes.authorizations.newValue;
}
if (changes.accounts) {
accounts = changes.accounts.newValue;
}
if (changes.currentAccountIndex) {
currentAccountIndex = changes.currentAccountIndex.newValue;
}
}
});

Expand Down Expand Up @@ -175,12 +202,20 @@ chrome.runtime.onMessage.addListener(async function(msg, sender, sendResponse) {
const url = tab.url || "http://unknown";
const favicon = tab.favIconUrl || "unknown";

const authToken = getAuthorizationToken(
let authToken = getAuthorizationToken(
dapp,
new URL(url).hostname,
version
);

// if authorization doesn't match current address, request new one
if (
authToken &&
getAddressFromToken(authToken) !== currentAccount()?.address
) {
authToken = undefined;
}

if (authToken) {
console.log("Valid authorization token: " + authToken);

Expand All @@ -194,7 +229,7 @@ chrome.runtime.onMessage.addListener(async function(msg, sender, sendResponse) {
token: authToken,
// new in v2
nexus: state.nexus,
version: "1",
version: "2",
id,
success: true,
},
Expand Down Expand Up @@ -248,7 +283,6 @@ chrome.runtime.onMessage.addListener(async function(msg, sender, sendResponse) {
console.log("nexus", state.nexus);
console.log("getting account " + address);
let account = await state.api.getAccount(address);

if (!account.balances) {
account.balances = [];
}
Expand All @@ -261,22 +295,65 @@ chrome.runtime.onMessage.addListener(async function(msg, sender, sendResponse) {
decimals: 8,
});

let balances = account.balances.map((x) => {
return {
value: x.amount,
decimals: x.decimals,
symbol: x.symbol,
};
});

let external = ""; // external address (neo or eth) if platform is not phantasma

const curAccount = currentAccount();
if (platform == "ethereum") {
let ethAddress = curAccount?.ethAddress;
if (ethAddress) {
external = ethAddress;
const ethBals = await getEthBalances(
ethAddress,
state.nexus == "mainnet"
);
balances = ethBals.map((b: any) => {
return {
symbol: b.symbol,
value: b.amount.toString(),
decimals: state.decimals(b.symbol),
};
});
} else {
platform = "phantasma";
}
}

if (platform == "neo") {
let neoAddress = curAccount?.neoAddress;
if (neoAddress) {
external = neoAddress;
const neoBals = await getNeoBalances(
neoAddress,
state.nexus == "mainnet"
);
balances = neoBals.map((b: any) => {
return {
symbol: b.symbol,
value: b.amount,
decimals: state.decimals(b.symbol),
};
});
} else {
platform = "phantasma";
}
}

console.log("got account: " + JSON.stringify(account));
let response: IGetAccountResponse = {
name: account.name,
address: account.address,
avatar: "",
platform,
external,
balances: account.balances.map((x) => {
return {
value: x.amount,
decimals: x.decimals,
symbol: x.symbol,
};
}),
balances,
id,
success: true,
};
Expand All @@ -297,13 +374,36 @@ chrome.runtime.onMessage.addListener(async function(msg, sender, sendResponse) {
const version = req.version;
const token = args[args.length - 1];

const nexus = args[1];
const chain = args[2];
const script = args[3];
let payload = args[4];
let nexus = "";
let payload = "";
let chain = "";
let script = "";
let platform = "phantasma";
let signature = "Ed25519";

if (version == "1") {
nexus = args[1];
chain = args[2];
script = args[3];
payload = args[4];
} else if (version == "2") {
chain = args[1];
script = args[2];
payload = args[3];
signature = args[4];
platform = args[5];
}

payload = payload == null || payload == "" ? state.payload : payload;

let txdata = JSON.stringify({ nexus, chain, script, payload });
let txdata = JSON.stringify({
nexus,
chain,
script,
payload,
signature,
platform,
});
let b64txdata = btoa(txdata);

chrome.tabs.get(msg.tabid, (tab) => {
Expand Down Expand Up @@ -349,6 +449,10 @@ chrome.runtime.onMessage.addListener(async function(msg, sender, sendResponse) {

const hexdata = args[1];
const signKind = args[2];
let platform = "phantasma";

if (version == "2" && args.length > 3)
platform = args[3].toLocaleLowerCase();

chrome.tabs.get(msg.tabid, (tab) => {
const url = tab.url || "http://unknown";
Expand Down
37 changes: 3 additions & 34 deletions src/components/TransactionComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,7 @@ import {
import { state } from "@/popup/PopupState";
function isFungible(symbol: string) {
return symbol !== "TTRS" && symbol !== "GHOST" && symbol !== "CROWN";
}
function decimals(symbol: string) {
switch (symbol) {
case "KCAL":
return 10;
case "SOUL":
return 8;
case "NEO":
return 0;
case "GAS":
return 8;
case "GOATI":
return 3;
case "ETH":
return 18;
case "MKNI":
return 0;
case "DYT":
return 18;
case "MUU":
return 18;
case "DANK":
return 18;
default:
return 0;
}
return !state.isNFT(symbol);
}
function formatNumber(num: any) {
Expand All @@ -127,7 +100,7 @@ function formatBalance(amount: string, decimals: number): string {
}
function formatSymbol(amount: string, symbol: string): string {
return formatBalance(amount, decimals(symbol)) + " " + symbol;
return formatBalance(amount, state.decimals(symbol)) + " " + symbol;
}
function formatAddress(addr: string) {
Expand Down Expand Up @@ -472,11 +445,7 @@ export default class extends Vue {
// if (ev.address == this.address) {
{
const nftId = data.TokenID;
if (
data.InfusedSymbol === "TTRS" ||
data.InfusedSymbol === "GHOST" ||
data.InfusedSymbol === "CROWN"
) {
if (state.isNFT(data.InfusedSymbol)) {
res.push({
icon: "mdi-basket-fill",
postIcon: "mdi-eye-outline",
Expand Down
Loading

0 comments on commit 209bf51

Please sign in to comment.