-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/safari-support
- Loading branch information
Showing
40 changed files
with
760 additions
and
201 deletions.
There are no files selected for viewing
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
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
4 changes: 2 additions & 2 deletions
4
packages/extension/src/providers/bitcoin/libs/activity-handlers/index.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import haskoinHandler from "./providers/haskoin"; | ||
|
||
export { haskoinHandler }; | ||
import ssHandler from "./providers/ss"; | ||
export { haskoinHandler, ssHandler }; |
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
108 changes: 108 additions & 0 deletions
108
packages/extension/src/providers/bitcoin/libs/activity-handlers/providers/ss/index.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import MarketData from "@/libs/market-data"; | ||
import { SSTxType } from "@/providers/bitcoin/types"; | ||
import { | ||
Activity, | ||
ActivityStatus, | ||
ActivityType, | ||
BTCRawInfo, | ||
} from "@/types/activity"; | ||
import { BaseNetwork } from "@/types/base-network"; | ||
export default async ( | ||
network: BaseNetwork, | ||
pubkey: string | ||
): Promise<Activity[]> => { | ||
return fetch( | ||
`${network.node}/api/v1/account/${network.displayAddress( | ||
pubkey | ||
)}/txs?pageSize=40` | ||
) | ||
.then((res) => res.json()) | ||
.then(async (txs: { txs: SSTxType[] }) => { | ||
if ((txs as any).message) return []; | ||
let tokenPrice = "0"; | ||
if (network.coingeckoID) { | ||
const marketData = new MarketData(); | ||
await marketData | ||
.getTokenPrice(network.coingeckoID) | ||
.then((mdata) => (tokenPrice = mdata || "0")); | ||
} | ||
|
||
const address = network.displayAddress(pubkey); | ||
const cleanedTxs = txs.txs.map((tx) => { | ||
return { | ||
...tx, | ||
vin: tx.vin.filter((vi) => vi.addresses), | ||
vout: tx.vout.filter((vo) => vo.addresses), | ||
}; | ||
}); | ||
return cleanedTxs.map((tx) => { | ||
const isIncoming = !tx.vin.find((i) => i.addresses![0] === address); | ||
console.log(isIncoming, tx.vin, tx.vout, address); | ||
let toAddress = ""; | ||
let value = 0; | ||
|
||
if (isIncoming) { | ||
const relevantOut = tx.vout.find( | ||
(tx) => tx.addresses![0] === address | ||
); | ||
if (relevantOut) { | ||
toAddress = relevantOut.addresses![0]; | ||
value = Number(relevantOut.value); | ||
} | ||
} else { | ||
const relevantOut = tx.vout.find( | ||
(tx) => tx.addresses![0] !== address | ||
); | ||
if (relevantOut) { | ||
toAddress = relevantOut.addresses![0]; | ||
value = Number(relevantOut.value); | ||
} else { | ||
toAddress = tx.vout[0].addresses![0]; | ||
value = Number(tx.vout[0].value); | ||
} | ||
} | ||
|
||
const rawInfo: BTCRawInfo = { | ||
blockNumber: tx.blockHeight!, | ||
fee: Number(tx.fee), | ||
inputs: tx.vin.map((input) => ({ | ||
address: input.addresses![0], | ||
value: Number(input.value), | ||
})), | ||
outputs: tx.vout.map((output) => ({ | ||
address: output.addresses![0], | ||
value: Number(output.value), | ||
})), | ||
transactionHash: tx.txid, | ||
timestamp: tx.timestamp * 1000, | ||
}; | ||
const act: Activity = { | ||
from: tx.vin[0].addresses![0], | ||
isIncoming, | ||
network: network.name, | ||
status: | ||
tx.blockHeight > 0 | ||
? ActivityStatus.success | ||
: ActivityStatus.pending, | ||
timestamp: tx.timestamp * 1000, | ||
to: toAddress, | ||
token: { | ||
decimals: network.decimals, | ||
icon: network.icon, | ||
name: network.name_long, | ||
symbol: network.currencyName, | ||
coingeckoID: network.coingeckoID, | ||
price: tokenPrice, | ||
}, | ||
transactionHash: tx.txid, | ||
type: ActivityType.transaction, | ||
value: value.toString(), | ||
rawInfo: rawInfo, | ||
}; | ||
return act; | ||
}); | ||
}) | ||
.catch(() => { | ||
return []; | ||
}); | ||
}; |
123 changes: 123 additions & 0 deletions
123
packages/extension/src/providers/bitcoin/libs/api-ss.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { BTCRawInfo } from "@/types/activity"; | ||
import { ProviderAPIInterface } from "@/types/provider"; | ||
import { | ||
BitcoinNetworkInfo, | ||
HaskoinUnspentType, | ||
SSTxType, | ||
SSUnspentType, | ||
} from "../types"; | ||
import { toBN } from "web3-utils"; | ||
import cacheFetch from "@/libs/cache-fetch"; | ||
import { getAddress as getBitcoinAddress } from "../types/bitcoin-network"; | ||
|
||
class API implements ProviderAPIInterface { | ||
node: string; | ||
networkInfo: BitcoinNetworkInfo; | ||
|
||
constructor(node: string, networkInfo: BitcoinNetworkInfo) { | ||
this.node = node; | ||
this.networkInfo = networkInfo; | ||
} | ||
|
||
public get api() { | ||
return this; | ||
} | ||
private getAddress(pubkey: string) { | ||
return getBitcoinAddress(pubkey, this.networkInfo); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
async init(): Promise<void> {} | ||
async getTransactionStatus(hash: string): Promise<BTCRawInfo | null> { | ||
return fetch(`${this.node}/api/v1/tx/${hash}`) | ||
.then((res) => res.json()) | ||
.then((tx: SSTxType) => { | ||
if ((tx as any).message) return null; | ||
if (tx.blockHeight < 0) return null; | ||
const rawInfo: BTCRawInfo = { | ||
blockNumber: tx.blockHeight, | ||
fee: Number(tx.fee), | ||
inputs: tx.vin | ||
.filter((t) => t.addresses && t.addresses.length) | ||
.map((input) => ({ | ||
address: input.addresses![0], | ||
value: Number(input.value), | ||
})), | ||
outputs: tx.vout | ||
.filter((t) => t.addresses && t.addresses.length) | ||
.map((output) => ({ | ||
address: output.addresses![0], | ||
value: Number(output.value), | ||
})), | ||
transactionHash: tx.txid, | ||
timestamp: tx.timestamp * 1000, | ||
}; | ||
return rawInfo; | ||
}); | ||
} | ||
async getBalance(pubkey: string): Promise<string> { | ||
const address = pubkey.length < 64 ? pubkey : this.getAddress(pubkey); | ||
return fetch(`${this.node}/api/v1/account/${address}`) | ||
.then((res) => res.json()) | ||
.then((balance: { balance: string; unconfirmedBalance: string }) => { | ||
if ((balance as any).message) return "0"; | ||
return toBN(balance.balance) | ||
.add(toBN(balance.unconfirmedBalance)) | ||
.toString(); | ||
}); | ||
} | ||
async broadcastTx(rawtx: string): Promise<boolean> { | ||
return fetch(`${this.node}/api/v1/send`, { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ hex: rawtx }), | ||
}) | ||
.then((res) => res.json()) | ||
.then((response) => { | ||
if (response.error) { | ||
return Promise.reject(response.message); | ||
} | ||
return true; | ||
}); | ||
} | ||
async SSToHaskoinUTXOs( | ||
SSUTXOs: SSUnspentType[], | ||
address: string | ||
): Promise<HaskoinUnspentType[]> { | ||
const ret: HaskoinUnspentType[] = []; | ||
for (const utx of SSUTXOs) { | ||
const res = (await cacheFetch({ | ||
url: `${this.node}/api/v1/tx/${utx.txid}`, | ||
})) as SSTxType; | ||
ret.push({ | ||
address, | ||
block: { | ||
height: utx.height, | ||
position: 0, | ||
}, | ||
index: utx.vout, | ||
pkscript: res.vout[utx.vout].scriptPubKey.hex, | ||
txid: utx.txid, | ||
value: Number(utx.value), | ||
raw: res.hex, | ||
}); | ||
} | ||
ret.sort((a, b) => { | ||
return a.value - b.value; | ||
}); | ||
return ret; | ||
} | ||
|
||
async getUTXOs(pubkey: string): Promise<HaskoinUnspentType[]> { | ||
const address = pubkey.length < 64 ? pubkey : this.getAddress(pubkey); | ||
return fetch(`${this.node}/api/v1/account/${address}/utxos`) | ||
.then((res) => res.json()) | ||
.then((utxos: SSUnspentType[]) => { | ||
if ((utxos as any).message || !utxos.length) return []; | ||
return this.SSToHaskoinUTXOs(utxos, address); | ||
}); | ||
} | ||
} | ||
export default API; |
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
33 changes: 33 additions & 0 deletions
33
packages/extension/src/providers/bitcoin/libs/ss-fee-handler.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { GasPriceTypes } from "@/providers/common/types"; | ||
|
||
interface FeeType { | ||
fast: { | ||
satsPerKiloByte: number; | ||
}; | ||
average: { | ||
satsPerKiloByte: number; | ||
}; | ||
slow: { | ||
satsPerKiloByte: number; | ||
}; | ||
} | ||
const SSFeeHandler = async ( | ||
url: string | ||
): Promise<Record<GasPriceTypes, number>> => { | ||
return fetch(url) | ||
.then((res) => res.json()) | ||
.then((json: FeeType) => { | ||
if (json.fast.satsPerKiloByte < 0) | ||
json.fast.satsPerKiloByte = json.average.satsPerKiloByte; | ||
return { | ||
[GasPriceTypes.FASTEST]: | ||
Math.ceil(json.fast.satsPerKiloByte / 1024) + 5, | ||
[GasPriceTypes.FAST]: Math.ceil(json.fast.satsPerKiloByte / 1024) + 3, | ||
[GasPriceTypes.REGULAR]: | ||
Math.ceil(json.average.satsPerKiloByte / 1024) + 2, | ||
[GasPriceTypes.ECONOMY]: Math.ceil(json.slow.satsPerKiloByte / 1024), | ||
}; | ||
}); | ||
}; | ||
|
||
export default SSFeeHandler; |
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
Oops, something went wrong.
5676d73
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Virus total analysis
chrome:
https://www.virustotal.com/gui/file/54b347c9c4f7e29711f4eedb09591267bc114e8740cceaedcc28f87d6e7c2557
firefox:
https://www.virustotal.com/gui/file/df975225687e7d55b70ac81ad8cc9aa38fa76a59ecb545a97b9ba28bc4a3a7fa