BitGo / bitgo-utxo-lib

UTXO coins functions implemented in pure JavaScript
86 stars 147 forks source link

Zcash tx with negative balanceValue #83

Open mroz22 opened 4 years ago

mroz22 commented 4 years ago

Zcash tx with negative balanceValue

if sum of amounts of shielded outputs is greater than sum of amounts of shielded inputs then tx.valueBalance is < 0. Such tx breaks Transaction.prototype.getHash

How to reproduce:

// https://zec1.trezor.io/tx/5dc7af90852a4a99e99822c6bf6996fac081fed3c51ef5912e362868069d8dc2

const hex = '0400008085202f890274e035266a710c06c8f0c93120b0153830c3f93a307e8a5685f49bb84162f6f4000000006b483045022100e7d80c47befab2c9494d64c211f4abd104aab2b75f31443b77438c4e3be4dd07022075bf764129c4775d1df4e26eb997793ed4531c4c27ddf7ff0fd188333ac58d350121033ed695b802987fb44f4401397f051cdaa9260a638f5359f866d776abf6f1549effffffffd9641e046765e49f5d9b2c0e033fc300cad47a6175e081854cc76ae8cc9253cf1c0000006a473044022045911112e79e6e0511850b20c284a21e9089ea4d10821f1a83bac28919c4ecfa022046b027e026c98f3bd0187e797651cef21fef7c6e8f54dbd206f413ee5febb1f40121033ed695b802987fb44f4401397f051cdaa9260a638f5359f866d776abf6f1549effffffff1a8c111100000000001976a914aceb8e7313c293f15bfbcefa130eb88a4fc02f2488acaf761200000000001976a9144be18cbda2883ebbc5061da1bd471600a59a94cf88ac201d0406000000001976a914c0775a0ebd52bb9ca1a998a7a49cf4aa8b7bc9ff88ac0f54fd05000000001976a9144e305867d0f7ac7066df1b00ff4dc9cba51e787088ac52cce800000000001976a914ef0b3b48f9ffad719e2e1406d373c10330972af688acc1a29a00000000001976a914e4d41695fc7b58a1da808b5bb1b79ee05fd8bc4488ac93e49800000000001976a914ef0d489856832ff4d32eaddf50e410917de1b75e88ac3ee30100000000001976a9148ce9e94ee8d6ccf75620b9e1360b8971d170ccfc88ac67120200000000001976a914359e46eb7f9ee15cb09e14a21380a75679749ed388ac00f7da1d000000001976a9143934056b5bfa87cc2a87ebc0a36f916ded16bb2788ac4c939900000000001976a914f21781907c8cb4f494294a1149f065dfbcd618ac88ac9c490f00000000001976a914edb57b3cb63f274e099cda30cde22f50638679e688ac7a070200000000001976a914a30f3d5333580596c3af791cb4fb48236fe8c04d88acff96fc02000000001976a914633270833ce33c26c7d1b92439c71acdf884ddd088ac7c80c400000000001976a914ac800d643af27b64b8d61b3547f92b8341f9c67788acda1e9c00000000001976a914041efd2ca1ba825222a418d495023439ae51ff0288ac1e8c0f00000000001976a91463716dd958c454eb0913a358a993ab6681f3db7688ac91dc5400000000001976a914e78252b0170bd337c5db064ce747b91d3681f13d88acb2baa400000000001976a914e56934520476226a95b630f3f75e3b387c8cbc0288acb67dec02000000001976a91411844f4c56672551aeb8b73813acabd4b4506b2788ac4bb31c00000000001976a914eb6108c6cf37ee21c7d21529651390825716e79e88ac5121ec00000000001976a91446bfd8b111d1fa9e9a1d7abcda6fec27f233482d88ac6e000200000000001976a914a981dde95b9cccb76dd5e73ec79ba2b9a2947a9788ac1f549a00000000001976a914df977d93f6fe0a70dfc06517cd235e795404f3e088ac9e76c0682c0000001976a914c521cb53dc20fb20049b9ab6653775b3c37411a188ac90d00300000000001976a914d995c6ef3070f64b8f29588b4786d6d450b3eff688ac00000000c6940d001877feffffffffff0001958018cae1b8cd422b719f644ab437833a6544e68093cc844fb15be0762e5339c99af38f0805842a3d4f1a50551e9aa4858150eecc1a6c4a818df5ca7c4d3e21c0be81ac4ddcf34516a9396c8a31436d57f90fff5fe6b668bb3ed28cb0fb3aae47becec7243df9d0b3ce90e25c130632763f11d2fbc4fba9b06edb124f1b21087b4c0539660d1b2a05cec8382b4875a9025a1bac5cd56a2dfcd76e0fccb25d0503a180de8acd6abe28309eb24b87c229d5075b885884b49c10ce3f1f60fc2cefb86e8859aea91bb8b7f797c4240944ef4365ecac5427a146c8de33688f2c5343b6b0eb138254c94e99eff302104714e4cd1e14bcc897f89a1fab58a866102cbfdc7839784350bc4e3b35c26a6d77934b69abb06dcd926d66369b2f273d2c4172932ee6e39c9e67f39ed588a80c6240e2b36398673c5b1e45ae477d8edcd9c10e6389d4f54b25a9a13dca4ae89034778a23f6cbbce9572d014d35f6d3fea3868c4eea96ae0f65443df55feb8aea0ae8b28e359aeedc201e921dd900ab941b19c25dfbd29ae1090e79048a938280ca6bb86d386080072933178e589ec9b7e185b1d0ed57b08e96c19a2acfa037a82e497063651ea9df9a9704d6a0a1964df49a1ed3d421dd8a857e57b5f167f0c5934b6a74bb6321dcde1d4df869b4496864013459c9497916a0adbc4649afbcb3b5be8a1b83d6830803538443f29a16796ad6b1e01f2a3cd26d6eb4a4b936d21bf70f2d615048583656b4397532fba5c9ac8fc93d5b804020d898589db1b59a9063da268f793a71297c21fd5936f83a658db9f0bcdfbe1221ddf9586780e0fd7f1eec4f63cb7691f7bab5ce07a2e7fdfced7d07289241fee44c3b809c6a3e3d9ed473419aa1f661e2af1611c57bd3f894c45ddb4db4d0adb405a9edad5807243fdecc236710165d2df68b5f0b9c0a773c8e887d461b6a88f46557ac8d6234405668b237027c0a6835af264be29405d13050a3183fe2e2f223827ee170c1196e03712138b590cdf67cfbcf46c2cae5f7131ccbdf2270c9e332f23803e3207ee8b528d26e5447a31b93deb08fea5f8575d5e9b456ae4afe39e5d154e60bbc22e3dbbecccf1f0e37c8abb1a7e3fabeb14b192c995e2a99456fb1f6eb3834667a59856192267665589c7c1b8206413e57936a2cb379c444ffb4fa7815536a96099914557944a8c428b4191757e58969084fc5cd34b11980580494a138cb56b0106da30d7c85702ad664a2e95ac93463bc3f34d5a8ad89ae4fbe8a5229028c7dd91dfa49cb6c232dac13c95861146940c2f209aee1f46752a30491278a0171ed609e8158ceb873a3c6cb00aa723df9ed6890fde178d0ebd1a45b420c9159c0b927569f789622edbde92fef885c3dfce52b0b4d0dcda8400281a13e947a64591a9ef7226f3e72a22a5e0d08';

// using latest 1.9.1 version
var Transaction = require('./node_modules/bitgo-utxo-lib/src/transaction')
var networks = require('./node_modules/bitgo-utxo-lib/src/networks')

var tx = Transaction.fromHex(hex, networks.zcash)

// this works, but note the value balance property is negative
console.log(tx);

// setting it to non-negative value makes it work
// tx.valueBalance = 0;

// but if it stays negative, this will throw
console.log(tx.getHash())

Problem is that unsigned int is expected here

https://github.com/BitGo/bitgo-utxo-lib/blob/7c91a916dd7318557918439156b87c55a9ff0854/src/transaction.js#L717

Which obviously throws here

https://github.com/BitGo/bitgo-utxo-lib/blob/7c91a916dd7318557918439156b87c55a9ff0854/src/bufferutils.js#L10

GIBR0 commented 4 years ago

I need assistance, I have this issue with my coin stuck and can't send. How can I correct this issue on my trezor one to send my coin? All help greatly appreciated.

prusnak commented 4 years ago

Maybe a trivial fix would be to replace

 bufferWriter.writeUInt64(this.valueBalance) 

with

 bufferWriter.readInt64(this.valueBalance) 
GIBR0 commented 4 years ago

Thanks for the comment. Hopefully there will be a correction for me to release my ZEC coin. I am not a coder so have to rely on friends like you.

prusnak commented 4 years ago

This fixes the issue for us: https://github.com/trezor/trezor-utxo-lib/commit/c41701b69cd8fdac60e0383a63c4a68cabe7eb94

GIBR0 commented 4 years ago

Ok thanks, Sorry I am learning here. How do I get this into the Trezor one to correct it? I have only worked out how to change the .bin and not code the files myself.

prusnak commented 4 years ago

Closing this. I'll send a pull request to this repo once we've tested this.

@GIBR0 please wait for the next update.

GIBR0 commented 4 years ago

ok many thanks Pavol

seperatis commented 4 years ago

I have this same issue. Can't send any ZEC transactions with my Trezor Model T. Updated to latest firmware (August 5th).

Please push update soon as possible, I need to move some coins.

Failed to send transaction Error details: specified a negative value for writing an unsigned value

prusnak commented 4 years ago

@seperatis please send me an email to stick@satoshilabs.com - I'll give you URL where you can test our fix

@GIBR0 please send me an email to stick@satoshilabs.com - I'll give you URL where you can test our fix

seperatis commented 4 years ago

@seperatis please send me an email to stick@satoshilabs.com - I'll give you URL where you can test our fix

@GIBR0 please send me an email to stick@satoshilabs.com - I'll give you URL where you can test our fix

Emailed you... I'll let you know results after I try it.