Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dogecoin-js to 0.1.2-dev.0 #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"watch": "nodemon --exec \"yarn run build\""
},
"dependencies": {
"@mydogeofficial/dogecoin-js": "^0.1.0-beta.4",
"@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",
Expand Down
13 changes: 11 additions & 2 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
generateAddress,
generateChild,
generatePhrase,
generateRawTx,
generateRoot,
initDogecoinJS,
signRawTx,
} from './helpers/wallet';

Expand All @@ -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;
Expand Down
99 changes: 65 additions & 34 deletions scripts/helpers/wallet.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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 };
}
2 changes: 1 addition & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "rollup --config"
},
"dependencies": {
"@mydogeofficial/dogecoin-js": "^0.1.0-beta.4",
"@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",
Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const common = {
format: 'iife',
dir: './compiled',
},
moduleContext: {
'node_modules/@mydogeofficial/dogecoin-js/dist/index.js': 'globalThis',
},
plugins: [
resolve({
browser: true,
Expand Down
9 changes: 4 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1537,11 +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/[email protected]":
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/[email protected]":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@napi-rs/triples/-/triples-1.0.3.tgz#76d6d0c3f4d16013c61e45dfca5ff1e6c31ae53c"
Expand Down