From 4847e77b898774ed3896ab75d7c1d8702306a643 Mon Sep 17 00:00:00 2001 From: Alex Lewis Date: Tue, 31 Jan 2023 13:36:32 -0800 Subject: [PATCH 1/2] Attempt to debug dogecoin-js in production bundle --- package.json | 2 +- scripts/background.js | 13 ++++- scripts/helpers/wallet.js | 99 +++++++++++++++++++++++++-------------- scripts/package.json | 2 +- scripts/rollup.config.mjs | 3 ++ yarn.lock | 8 ++-- 6 files changed, 85 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index 9d09e6fa..076e1b61 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "watch": "nodemon --exec \"yarn run build\"" }, "dependencies": { - "@mydogeofficial/dogecoin-js": "^0.1.0-beta.4", + "@mydogeofficial/dogecoin-js": "0.1.1-dev.0", "@native-base/icons": "^0.0.11", "@native-base/next-adapter": "^1.1.9", "axios": "^1.2.2", diff --git a/scripts/background.js b/scripts/background.js index 78f9688c..ca7919cf 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -25,7 +25,9 @@ import { generateAddress, generateChild, generatePhrase, + generateRawTx, generateRoot, + initDogecoinJS, signRawTx, } from './helpers/wallet'; @@ -50,14 +52,21 @@ function onCreateTransaction({ data = {}, sendResponse } = {}) { nownodes .get(`/utxo/${data.senderAddress}`) - .json((response) => { + .json(async (response) => { // Sort by smallest + oldest const utxos = response.sort((a, b) => { const aValue = sb.toBitcoin(a.value); const bValue = sb.toBitcoin(b.value); return aValue > bValue ? 1 : aValue < bValue ? -1 : a.height - b.height; }); - + // DEBUG bundling of libdogecoin wrapper + await initDogecoinJS(); + // await generateRawTx( + // data.senderAddress, + // data.recipientAddress, + // amount, + // utxos + // ); const feePerInput = 0.0012; // estimate fee per input let fee = feePerInput; let total = 0; diff --git a/scripts/helpers/wallet.js b/scripts/helpers/wallet.js index 9cb7e9d3..7072f61e 100644 --- a/scripts/helpers/wallet.js +++ b/scripts/helpers/wallet.js @@ -1,4 +1,4 @@ -// import { DogecoinJS } from '@mydogeofficial/dogecoin-js'; +import DogecoinJS from '@mydogeofficial/dogecoin-js'; import * as bip32 from 'bip32'; import * as bip39 from 'bip39'; import * as bitcoin from 'bitcoinjs-lib'; @@ -20,6 +20,14 @@ const network = { scriptHash: 0x16, wif: 0x80, }; +let libdogecoin = null; + +export async function initDogecoinJS() { + if (libdogecoin === null) { + console.log('bundled dogecoin-js:', DogecoinJS); + libdogecoin = await DogecoinJS.init(); + } +} export function generatePhrase() { return bip39.generateMnemonic(128); @@ -78,36 +86,59 @@ export function signRawTx(rawTx, wif) { return txb.build().toHex(); } -// export async function generateRawTx(sender, recipient, amount, utxos) { -// const dogecoin = await DogecoinJS.init(); -// const index = dogecoin.startTransaction(); -// let total = 0; -// const fee = 0.01; - -// for (let i = 0; i < utxos.length; i++) { -// const utxo = utxos[i]; -// console.log('utxo', utxo); -// const value = sb.toBitcoin(utxo.value); -// total += value; -// console.log( -// `added tx value ${value} for total ${total} > ${amount} + ${fee}` -// ); -// dogecoin.addUTXO(index, utxo.txid, utxo.vout); - -// if (total > amount + fee) { -// break; -// } -// } - -// dogecoin.addOutput(index, recipient, `${amount}`); -// const rawTx = dogecoin.finalizeTransaction( -// index, -// recipient, -// `${fee}`, -// `${total}`, -// sender -// ); - -// console.log('rawTx', rawTx); -// return { rawTx, fee }; -// } +export async function generateRawTx(sender, recipient, amount, utxos) { + const index = libdogecoin.startTransaction(); + const minFee = 0.0015; + let fee = 0; + let total = 0; + + for (let i = 0; i < utxos.length; i++) { + const utxo = utxos[i]; + console.log('utxo', utxo); + const value = sb.toBitcoin(utxo.value); + total += value; + fee += minFee; + console.log( + `added tx value ${value} for total ${total} > ${amount} + ${fee}` + ); + libdogecoin.addUTXO(index, utxo.txid, utxo.vout); + + if (total >= amount + fee) { + break; + } + } + + // Check for max send and update amount and fee + if (total >= amount + fee) { + libdogecoin.addOutput(index, recipient, `${amount}`); + + let rawTx = libdogecoin.finalizeTransaction( + index, + recipient, + `${fee}`.toFixed(8), // estimated fee + `${total}`, + sender + ); + + const size = rawTx.length / 2; + + console.log('estimated fee', fee); + + fee = Math.max(parseFloat(((size / 1000) * 0.01).toFixed(8)), minFee); + + console.log('actual fee', fee); + + rawTx = libdogecoin.finalizeTransaction( + index, + recipient, + `${fee}`, // actual fee + `${total}`, + sender + ); + + console.log('rawTx', rawTx); + return { rawTx, fee }; + } + + return { rawTx: '0', fee: 0 }; +} diff --git a/scripts/package.json b/scripts/package.json index de1e4f89..67834033 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -6,7 +6,7 @@ "build": "rollup --config" }, "dependencies": { - "@mydogeofficial/dogecoin-js": "^0.1.0-beta.4", + "@mydogeofficial/dogecoin-js": "0.1.1-dev.0", "@rollup/plugin-commonjs": "^24.0.0", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.0.1", diff --git a/scripts/rollup.config.mjs b/scripts/rollup.config.mjs index 05c373fd..2d799ff7 100644 --- a/scripts/rollup.config.mjs +++ b/scripts/rollup.config.mjs @@ -13,6 +13,9 @@ const common = { format: 'iife', dir: './compiled', }, + moduleContext: { + 'node_modules/@mydogeofficial/dogecoin-js/dist/index.js': 'globalThis', + }, plugins: [ resolve({ browser: true, diff --git a/yarn.lock b/yarn.lock index 098f0be0..f374cfb8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1537,10 +1537,10 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" -"@mydogeofficial/dogecoin-js@^0.1.0-beta.4": - version "0.1.0-beta.4" - resolved "https://registry.yarnpkg.com/@mydogeofficial/dogecoin-js/-/dogecoin-js-0.1.0-beta.4.tgz#1cf0b155553624180ce767c10423110d092deb55" - integrity sha512-Ouaka4/Y1b+LL5fShLfOY49/pKlrnY91Rz/D9/W4AiDFfaOPjhVxr7YpH0xN5Lt9+HF4B4VdGM/G/wjzc1LjZw== +"@mydogeofficial/dogecoin-js@0.1.1-dev.0": + version "0.1.1-dev.0" + resolved "https://registry.yarnpkg.com/@mydogeofficial/dogecoin-js/-/dogecoin-js-0.1.1-dev.0.tgz#121cf18dedd25d3c497ece9a2246618603e2babd" + integrity sha512-a5owk56YKXwy9H/ch3C8bYN9UWMVgzw/VtlAhUWGqK8/Ekxi+JfiNmcKwig0pK2wMU9pLW0HcA+ZYQp/5k4BbA== "@napi-rs/triples@1.0.3": version "1.0.3" From 9486d391d78eca71000afa8d6efff1e964882ec1 Mon Sep 17 00:00:00 2001 From: Ed Tubbs Date: Wed, 30 Aug 2023 02:21:40 +0000 Subject: [PATCH 2/2] Update dogecoin-js to 0.1.2-dev.0 --- package.json | 2 +- scripts/package.json | 2 +- yarn.lock | 9 ++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 076e1b61..34e00c67 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "watch": "nodemon --exec \"yarn run build\"" }, "dependencies": { - "@mydogeofficial/dogecoin-js": "0.1.1-dev.0", + "@mydogeofficial/dogecoin-js": "0.1.2-dev.0", "@native-base/icons": "^0.0.11", "@native-base/next-adapter": "^1.1.9", "axios": "^1.2.2", diff --git a/scripts/package.json b/scripts/package.json index 67834033..e7dc3841 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -6,7 +6,7 @@ "build": "rollup --config" }, "dependencies": { - "@mydogeofficial/dogecoin-js": "0.1.1-dev.0", + "@mydogeofficial/dogecoin-js": "0.1.2-dev.0", "@rollup/plugin-commonjs": "^24.0.0", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.0.1", diff --git a/yarn.lock b/yarn.lock index f374cfb8..569213c2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1537,11 +1537,10 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" -"@mydogeofficial/dogecoin-js@0.1.1-dev.0": - version "0.1.1-dev.0" - resolved "https://registry.yarnpkg.com/@mydogeofficial/dogecoin-js/-/dogecoin-js-0.1.1-dev.0.tgz#121cf18dedd25d3c497ece9a2246618603e2babd" - integrity sha512-a5owk56YKXwy9H/ch3C8bYN9UWMVgzw/VtlAhUWGqK8/Ekxi+JfiNmcKwig0pK2wMU9pLW0HcA+ZYQp/5k4BbA== - +"@mydogeofficial/dogecoin-js@0.1.2-dev.0": + version "0.1.2-dev.0" + resolved "https://registry.yarnpkg.com/@mydogeofficial/dogecoin-js/-/dogecoin-js-0.1.2-dev.0.tgz#448ec079deb4b33ebfea218dea110e0c9942e0f5" + integrity sha512-d5Wn9734CuhDYpKFvJ+k/NIoP+OusBgdT5qfL3xiz1cUUmzRocGNmKNRH0vCSMCxbluZ0wVCgIbUZSu1wUiXoQ== "@napi-rs/triples@1.0.3": version "1.0.3" resolved "https://registry.yarnpkg.com/@napi-rs/triples/-/triples-1.0.3.tgz#76d6d0c3f4d16013c61e45dfca5ff1e6c31ae53c"