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

change method does not work #3609

Closed
Sequoya42 opened this issue Jul 13, 2023 · 1 comment
Closed

change method does not work #3609

Sequoya42 opened this issue Jul 13, 2023 · 1 comment

Comments

@Sequoya42
Copy link

Sequoya42 commented Jul 13, 2023

Assuming for example that from contains 1 doge, send 0.5 to to, expect from to end up at 0.5 (minus fees).
All parameters are valid.
Does not work. Rest of the funds are sent to miner.

https://sochain.com/tx/DOGE/35d551e393f5c6822fcd0f0989cce980f75d40824b136f31980e3393810a49a4

Using the following code

  const transactionFee = 2e6; // 0.02 doge

  let tx = new Transaction().fee(transactionFee)
    .from(from.utxos)
    .to(to.address, Math.floor(amount))
    .change(from.address)
    .sign(from.privkey);

Force me to do something like

  let tx = new Transaction().fee(transactionFee)
    .from(from.utxos)
    .to(to.address, Math.floor(amount))
    .to(from.address, balance - amount - transactionFee - taxTransfer)
    .change(from.address)
    .sign(from.privkey);

For it to work.
According to the documentation :

var transaction = new Transaction()
    .from(utxos)          // Feed information about what unspent outputs one can use
    .to(address, amount)  // Add an output with the given amount of satoshis
    .change(address)      // Sets up a change address where the rest of the funds will go
    .sign(privkeySet)     // Signs all the inputs it can
    ```
    
    Change function should send the rest of the fund back to the from address.
@kajoseph
Copy link
Collaborator

I'm not able to reproduce this issue.

const lib = require('bitcore-lib-doge');
const crypto = require('node:crypto');

const pk = new lib.PrivateKey();
const pk2 = new lib.PrivateKey();

const fromAddr = pk.publicKey.toAddress().toString();
const toAddr = pk2.publicKey.toAddress().toString();

const txHex = new lib.Transaction()
  .from({
    script: lib.Script.fromAddress(fromAddr),
    txid: crypto.randomBytes(32).toString('hex'),
    vout: 0,
    satoshis: 1e9
  })
  .to(toAddr, 1e8 * 5)
  .change(fromAddr)
  .sign(pk)
  .serialize();

Produces a valid tx:

0100000001eb270bca0533b021eebe82913535c204c50c87dbbd42ab92eb8680badf832616000000006a4730440220697af9bab91673617cfb50b0a1b51682fe97d5bf030c7a6f06ae0e01b1824a73022001130c4243fbe62034e087dda12f83047ec27126f1f86b1eccd0b7eb09a8734e01210256f30b869f996cbce28e0f8fbad4a9b41d8938502b7175d81010245a6eed1490ffffffff0280f0fa02000000001976a914d8c41ceb822081c4cc8452cb6af062b8fbe1612588ac4017a201000000001976a9142928f92405b440234854c965d944c913b0fc755c88ac00000000

which decodes to

{
  "txid": "a648ff71c2c6a640c34c5634f78f383db1c15c19e2126c122fcebaa01ad688f5",
  "hash": "a648ff71c2c6a640c34c5634f78f383db1c15c19e2126c122fcebaa01ad688f5",
  "size": 225,
  "vsize": 225,
  "version": 1,
  "locktime": 0,
  "vin": [
    {
      "txid": "162683dfba8086eb92ab42bddb870cc504c235359182beee21b03305ca0b27eb",
      "vout": 0,
      "scriptSig": {
        "asm": "30440220697af9bab91673617cfb50b0a1b51682fe97d5bf030c7a6f06ae0e01b1824a73022001130c4243fbe62034e087dda12f83047ec27126f1f86b1eccd0b7eb09a8734e[ALL] 0256f30b869f996cbce28e0f8fbad4a9b41d8938502b7175d81010245a6eed1490",
        "hex": "4730440220697af9bab91673617cfb50b0a1b51682fe97d5bf030c7a6f06ae0e01b1824a73022001130c4243fbe62034e087dda12f83047ec27126f1f86b1eccd0b7eb09a8734e01210256f30b869f996cbce28e0f8fbad4a9b41d8938502b7175d81010245a6eed1490"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.50000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 d8c41ceb822081c4cc8452cb6af062b8fbe16125 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914d8c41ceb822081c4cc8452cb6af062b8fbe1612588ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "noxK9sg6Cx44ekGcMh3QkMb8cDFMWuXrQX"
        ]
      }
    },
    {
      "value": 0.27400000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 2928f92405b440234854c965d944c913b0fc755c OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a9142928f92405b440234854c965d944c913b0fc755c88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "nXwo41DPMay4MxvC36yqgaP7L9G6u1BScS"
        ]
      }
    }
  ]
}

As you can see, there are 2 outputs: 1 for 0.5 DOGE and 1 for change.

Since this is an old issue, I'm going to assume this has already been fixed and am going to close the issue. Feel free to reply if you're still experiencing this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants