Hi, I'm developing an rn project with bitcoinjs-lib. Previously, I have fixed compatible issues by rn-nodeify.
Unfortunately, this latest version which contains template module cannot be resolved as before.
Transaction creation is always failed when called bscript.compile, because script which was required by out.js is {}.
But the Types is required well,like the code in out.js as below:
var bscript = require('../../script')
var types = require('../../types')
I have changed and compared, But got nothing.
could anyone please give me some suggestion?Thanks in advance
Can you please post what your code is and the exact error you receive.
Ok ,let me show you:
My code:
toOutputScript_ext: function (address, curr_type) {
var network = bitcoin.getNetwork(curr_type);//network || networks.bitcoin
var decode = AddressGenService.fromBase58Check_ext(address);
if (decode.version === network.pubKeyHash){
return bitcoin.script.pubKeyHash.output.encode(decode.hash);//**error occurs at here**
}
if (decode.version === network.scriptHash) {
return bitcoin.script.scriptHash.output.encode(decode.hash);
}
//鍏煎
if( (curr_type === "LTC") && ( decode.version === network.scriptHash2)){
return bitcoin.script.scriptHash.output.encode(decode.hash);
}
throw new Error(address + ' has no matching Script');
},
Error:

After struggled with this problem for hours, I found that the script in out.js is {}
Paste bitcoinjs-lib's code also:
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('bitcoin-ops')
console.info(bscript);//it should not be null
function check (script) {
var buffer = bscript.compile(script)
return buffer.length === 25 &&
buffer[0] === OPS.OP_DUP &&
buffer[1] === OPS.OP_HASH160 &&
buffer[2] === 0x14 &&
buffer[23] === OPS.OP_EQUALVERIFY &&
buffer[24] === OPS.OP_CHECKSIG
}
check.toJSON = function () { return 'pubKeyHash output' }
function encode (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)
return bscript.compile([
OPS.OP_DUP,
OPS.OP_HASH160,
pubKeyHash,
OPS.OP_EQUALVERIFY,
OPS.OP_CHECKSIG
])
}
function decode (buffer) {
typeforce(check, buffer)
return buffer.slice(3, 23)
}
module.exports = {
check: check,
decode: decode,
encode: encode
}
@dcousens for your infomation
Sounds like a require issue, circular dependency or something.
How is your project structured?
Sorry, I'm not sure what did you say, are there any references? In my project, only require bitcoinjs-lib rather than it's sub modules.@dcousens
This is require detail in the js file which required bitcoinjs-lib
var bitcoin = require('./bitcoin.js');
var bs58check = require('bs58check');
If you run node bitcoinjs-lib/src/templates/pubkeyhash/output.js with your added console.log(bscript)... is it still {}?
@maxcloudwgh wait, you have browserified it already? You don't need to browserify unless you are deploying to a web page, and even then, browserify your entire project, not bitcoinjs-lib specifically.
var bitcoin = require('bitcoinjs-lib')
// ...
No, I'm not use browserify, It was only compressed by the tool build in Rn
Be more clear,built in react native
@maxcloudwgh I think your build tool has had a problem somewhere.
According to your question, I just review my code again, maybe the code in bitcoin.js have some problem? the code of bitcoin.js as below
var bitcoin = require('bitcoinjs-lib');
bitcoin.getNetwork = function (curr_type) {
//do something
};
bitcoinjs-lib is required in bitcoin.js and add some custom functions to it, is it a bad use?
@maxcloudwgh no, that should be fine.
Are you testing this with node or react-native?
Yeah, i had test it's send method under node environment, it works. Same method failed in RN
FYI, The compressed Tool built in RN only take effect when assemble an release build,the error I mentioned was occurs in debug build.@dcousens
so far I'm not thinks it's an issue of bitcoinjs-lib , it should be an compatible issue between RN and bitcoinjs-lib :).
@maxcloudwgh if you find a solution, please post the solution here incase we can help alleviate the underlying problem, react-native or otherwise
My pleasure @dcousens
@maxcloudwgh, it seems @WillCoco has the same issue.
Did this issue only appear in the latest release (3.2.0) for the two of you? Or are you both new and only having this issue now?
@dcousens I'm sure it works fine on old release.just now I test it on old release again, it still works well
FYI, the old version number is

@maxcloudwgh what about 3.0.0 (not ^3.0.0, not 3.2.0)?
Ok ,I will have a try, but I think template modules cause this issue.@dcousens
@maxcloudwgh the template modules have a circular dependency with bitcoin.script, which is probably the reason it is failing.
I might move the template export injection from bitcoin.script to index.js, as it is purely a convenience for users, and not necessary in bitcoin.script.
This wouldn't be a breaking change.
yeah, it failed on 3.0.0 as expected just now, maybe circular dependency is the real reason.@dcousens
@dcousens if you finished, please let me know
@maxcloudwgh please see #907, download and test :+1: