bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.83k stars 2.08k forks source link

Allow me to make a transaction in BTC not satoshi #1275

Closed benzmuircroft closed 9 years ago

benzmuircroft commented 9 years ago

I would like to construct a raw tx to ( push tx ) send to blockr.io or blockchain.info but they reconize BTC not satoshi

My unspent outputs:

var unspents= [ { txid: '67b22a1874be2e748b2c904e2181f85a9d6503001e58c374e17b1161cd5a36f4', outputIndex: 0, address: 'myHtNgECiAjBctWz1tM2TN2PupWwfHtgMi', scriptPubKey: '76a914c2f9958848c465efd042fcb76fef519f03a83ec288ac', amount: 0.001, confirmations: 54 } ];

var transaction=new bitcore.Transaction()
        .from(unspents)
        .to(bitcore.Address.fromString(payToAddress),0.001)
        .change(address)
        .sign(privateKey)

Causes error:

bitcore.ErrorInvalidArgument: Invalid Argument: Amount is expected to be a positive integer

But if I set it all up with satoshis instead I get no error message but the prevTxId outputs are completely messed up

'{"version":1,"inputs":
[{"prevTxId":"67b22a1874be2e748b2c904e2181f85a9d6503001e58c374e17b1161cd5a36f4"
,"outputIndex":0
,"sequenceNumber":4294967295
,"script":"47304402204f5950a8f8e7346e96101da811452246d51741131310f58d5cfdc064e547d3dc02202b817a442bb043a1174fe0703082ebbd7bc622afc00084710be21fa143444d83012102bbe798da887604bdd360dd059061b48816634e44d9f54563af2c727b8bcb3e2f"
,"scriptString":"71 0x304402204f5950a8f8e7346e96101da811452246d51741131310f58d5cfdc064e547d3dc02202b817a442bb043a1174fe0703082ebbd7bc622afc00084710be21fa143444d8301 33 0x02bbe798da887604bdd360dd059061b48816634e44d9f54563af2c727b8bcb3e2f"
,"output":{
"satoshis":10000000000000
,"script":"76a914c2f9958848c465efd042fcb76fef519f03a83ec288ac"
}}]
,"outputs":[
{"satoshis":100000,"script":"a9149ffdff1f3eea0deb21ed967b91c7ef3cce65b2d687"},
{"satoshis":9999999890000,"script":"76a914c2f9958848c465efd042fcb76fef519f03a83ec288ac"}
]
,"nLockTime":0
,"changeScript":"OP_DUP OP_HASH160 20 0xc2f9958848c465efd042fcb76fef519f03a83ec2 OP_EQUALVERIFY OP_CHECKSIG"
,"changeIndex":1
,"fee":10000
}'

can you see "satoshis":10000000000000 and "satoshis":9999999890000 they are messed up massive numbers.

They should be: Total Inputs 1.10000000 tBTC Total Outputs 1.09990000 tBTC

as seen here (testnet) https://www.blocktrail.com/tBTC/tx/67b22a1874be2e748b2c904e2181f85a9d6503001e58c374e17b1161cd5a36f4

braydonf commented 9 years ago

Previous outputs "amount" is expected to be in BTC units for backwards compatibility, you can specify "satoshis", which is preferred.

Floating point numbers are not supported as units: https://github.com/bitpay/bitcore/blob/master/CONTRIBUTING.md#g3---internal-amounts-must-be-integers-representing-satoshis

This is due to unsafe representation with floating point, example:

0.1 * 0.1
fanatid commented 9 years ago

@benzmuircroft Transaction.to should have amount in satoshis @braydonf satoshis in UnspentOutput isn't preffered...

benzmuircroft commented 9 years ago

@fanatid How am I suppost to get the new bicore.Transaction to be in BTC using Transaction.to?

braydonf commented 9 years ago

"amount" is converted into "satoshis" in UnspentOutput and at some point we may want to deprecate the "amount" field just so it's clear, though several explorers will have that format, and hence why it exists there.

benzmuircroft commented 9 years ago

Why does bitcore expect me to add amounts in satoshi then automatically mess up the prevTx amounts ??!

benzmuircroft commented 9 years ago

The output raw transaction is not accepted on blockchain.info or blockr.io with messed up amounts

braydonf commented 9 years ago

These are equivalent:

var unspents = [ 
  { 
    txid: '67b22a1874be2e748b2c904e2181f85a9d6503001e58c374e17b1161cd5a36f4',
    outputIndex: 0,
    address: 'myHtNgECiAjBctWz1tM2TN2PupWwfHtgMi',
    scriptPubKey: '76a914c2f9958848c465efd042fcb76fef519f03a83ec288ac',
    amount: 0.001,
    confirmations: 54 
  } 
];

(from various block explorers)

var unspents = [ 
  { 
    txId: '67b22a1874be2e748b2c904e2181f85a9d6503001e58c374e17b1161cd5a36f4',
    outputIndex: 0,
    script: '76a914c2f9958848c465efd042fcb76fef519f03a83ec288ac',
    satoshis: 100000
  } 
];

(bitcore internal representation)

Entering satoshis into the "amount" property will then multiply 100000 by 1e8 and give the incorrect value 10000000000000 from above, and is likely the source of the problem.

braydonf commented 9 years ago

Also there is a community forum at https://labs.bitpay.com/ for questions and discussion.

eniola99 commented 2 years ago

hello i need a verdit on this close argument