Skip to content

Commit

Permalink
fix(data): 👽 CORS bypass proxy & use DCD RPC Proxy instead of Insight…
Browse files Browse the repository at this point in the history
… API
  • Loading branch information
jojobyte committed Dec 27, 2024
1 parent 69af198 commit 88b7d12
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 33 deletions.
19 changes: 14 additions & 5 deletions src/components/transactions-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ const initialState = {
let txDate = new Date(tx.time * 1000)
let user = cnt?.alias || cnt?.info?.preferred_username || tx?.alias || ''
let name = cnt?.info?.name
let addr = tx?.vout?.[0]?.scriptPubKey?.addresses?.[0]
let addr = tx?.vout?.[0]?.scriptPubKey?.address || tx?.vout?.[0]?.scriptPubKey?.addresses?.[0]
let itemDir = `To <strong>${name}</strong>`

if (!['sent', 'outgoing'].includes(tx?.dir)) {
addr = tx?.vin?.[0]?.addr
addr = tx?.vin?.[0]?.address
}
if (tx.time) {
time = timeago(Date.now() - txDate.getTime())
Expand All @@ -91,22 +92,30 @@ const initialState = {
) {
name = `@${user}`
} else if (
!name && !user
!name && !user && addr
) {
name = html`<span title="${addr}">${addr.substring(0,3)}...${addr.substring(addr.length - 3)}</span>`
} else if (
!name && !user && !addr
) {
itemDir = html`<span title="${tx.txid}">${'Unknown TX Type'}</span>`
}

let itemAmount = tx.receivedAmount || tx.valueOut || 0
if (name) {
itemDir = `To <strong>${name}</strong>`
}
let itemAmount = tx.sentAmount || tx.valueOut || 0

let itemCtrls = html`<aside class="inline row dang" title="-${itemAmount}">
-${itemAmount}
</aside>`
let itemTitle = `Sent on`
let itemDir = `To <strong>${name}</strong>`
// itemDir = `To <strong>${name}</strong>`

if (!['sent', 'outgoing'].includes(tx?.dir)) {
itemTitle = `Received on`
itemDir = `From <strong>${name}</strong>`
itemAmount = tx.receivedAmount || tx.valueIn || 0
itemCtrls = html`<aside class="inline row succ" title="+${itemAmount}">
+${itemAmount}
</aside>`
Expand Down
5 changes: 3 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,16 +1017,17 @@ async function main() {
Object.values(appState.transactions || {})
)

// Load Cached TXs
await transactionsList.render({
userInfo,
contacts: appState.contacts,
transactions: Object.values(txs.byTx),
})

// Update TX Cache
txs = await getTxs(appState)

console.log('main getTxs', txs)

// Re-Render TXs with Updated Cache
transactionsList.render({
userInfo,
contacts: appState.contacts,
Expand Down
8 changes: 6 additions & 2 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export const DIALOG_STATUS = {
ERROR
}

export const DCD_RPC_ENDPOINT = 'https://rpc.digitalcash.dev/'

export const CORS_BYPASS = 'https://wallet.dashing.trade/api/cors'

export const CROWDNODE = {
offset: 20000,
duffs: 100000000,
Expand All @@ -165,12 +169,12 @@ export const CROWDNODE = {
network: {
main: {
// baseUrl: "https://app.crowdnode.io",
baseUrl: "https://wallet.dashincubator.dev/api/cors/app.crowdnode.io",
baseUrl: `${CORS_BYPASS}/app.crowdnode.io`,
hotwallet: "XjbaGWaGnvEtuQAUoBgDxJWe8ZNv45upG2",
},
test: {
// baseUrl: "https://test.crowdnode.io",
baseUrl: "https://wallet.dashincubator.dev/api/cors/test.crowdnode.io",
baseUrl: `${CORS_BYPASS}/test.crowdnode.io`,
hotwallet: "yMY5bqWcknGy5xYBHSsh2xvHZiJsRucjuy",
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/utils/dash/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,8 @@ export function selectOptimalUtxos(utxos, output) {
}

export function sortIncomingAndOutgoingTxs({
conAddr, tx, addr, dir, sentAmount = null, receivedAmount = null,
conAddr, tx, addr, dir,
sentAmount = null, receivedAmount = null,
byAlias = {}, byAddress = {}, byTx = {},
}) {
let alias = byTx?.[tx.txid]?.alias || conAddr.alias
Expand Down
121 changes: 99 additions & 22 deletions src/utils/dash/network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
OIDC_CLAIMS,
DCD_RPC_ENDPOINT,
} from '../constants.js'

import {
Expand Down Expand Up @@ -622,30 +623,98 @@ export async function sendTx(



export async function rpcAddrsTransactions({
addresses,
txs = [],
}) {
let basicAuth = btoa(`user:pass`);
let txidPayload = JSON.stringify({
method: "getaddresstxids",
params: [
{
addresses,
}
]
});
let txidResp = await fetch(DCD_RPC_ENDPOINT, {
method: "POST",
headers: {
"Authorization": `Basic ${basicAuth}`,
"Content-Type": "application/json",
},
body: txidPayload,
});
let txidData = await txidResp.json();
if (txidData.error) {
let err = new Error(txidData.error.message);
Object.assign(err, txidData.error);
throw err;
}

let txids = txidData?.result || []

let VERBOSE_INFO = true

let txInfoPayload = JSON.stringify({
method: "getrawtransactionmulti",
params: [
{
"0": txids,
},
VERBOSE_INFO
]
});
let txInfoResp = await fetch(DCD_RPC_ENDPOINT, {
method: "POST",
headers: {
"Authorization": `Basic ${basicAuth}`,
"Content-Type": "application/json",
},
body: txInfoPayload,
});
let txInfoData = await txInfoResp.json();
if (txInfoData.error) {
let err = new Error(txInfoData.error.message);
Object.assign(err, txInfoData.error);
throw err;
}
txs = Object.values(txInfoData?.result || {})

console.log('rpcAddrsTransactions', {
txInfoResp,
txInfoData,
txids,
txs,
})

return txs;
}

export async function getAddrsTransactions({
appState, addrs, contactAddrs = {},
appState,
addrs,
contactAddrs = {},
txs = [],
}) {
let storeAddrs = await loadStoreObject(store.addresses)
if (txs.length === 0) {
txs = await dashsight.getAllTxs(addrs)
txs = await rpcAddrsTransactions({
addresses: addrs,
txs,
})
}
let byAddress = {}
let byAlias = {}
let byTx = {}

// console.log('getAddrsTransactions', {
// txs, addrs, contactAddrs, appT: appState.transactions
// })

for await (let tx of txs) {
let dir = 'received'
let conAddr
let sentAmount = 0
let receivedAmount = 0

for await (let vin of tx.vin) {
let addr = vin.addr
let addr = vin.address
conAddr = contactAddrs[addr]

if(storeAddrs[addr]) {
Expand All @@ -661,30 +730,38 @@ export async function getAddrsTransactions({
}
}

function voutSort({ addr, vout, }) {
let conAddr = contactAddrs[addr]

if(storeAddrs[addr]) {
receivedAmount += Number(vout.value)
} else {
// sentAmount -= Number(vout.value)
}

if (conAddr) {
sortIncomingAndOutgoingTxs({
tx, addr, conAddr, dir, receivedAmount,
byAlias, byAddress, byTx,
})
}
}

for await (let vout of tx.vout) {
if (vout?.scriptPubKey?.address) {
let addr = vout?.scriptPubKey?.address
voutSort({ addr, vout })
}
if (vout?.scriptPubKey?.addresses) {
for await (let addr of vout.scriptPubKey.addresses) {
// let addr = vout.scriptPubKey.addresses[0]
conAddr = contactAddrs[addr]

if(storeAddrs[addr]) {
receivedAmount += Number(vout.value)
} else {
// sentAmount -= Number(vout.value)
}

if (conAddr) {
sortIncomingAndOutgoingTxs({
tx, addr, conAddr, dir, receivedAmount,
byAlias, byAddress, byTx,
})
}
voutSort({ addr, vout })
}
}
}

byTx[tx.txid] = {
...byTx[tx.txid],
...tx,
receivedAmount,
sentAmount,
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function isEmpty(value) {
* https://www.freecodecamp.org/news/javascript-debounce-example/
*
* @example
* const change = debounce((a) => console.log('Saving data', a));
* const change = debouncePromise((a) => console.log('Saving data', a));
* change('b');change('c');change('d');
* 'Saving data d'
*
Expand Down

0 comments on commit 88b7d12

Please sign in to comment.