Closed marcianovc closed 3 years ago
MarteXcoin has extra data in the serialized transaction called nTime
Bitcoin does not have that data, so the Transaction parser for Bitcoin chokes on the nTime value.
It would be easy to fork bitcoinjs-lib to support MarteXcoin, but unfortunately, this library is Bitcoin-only, and the network interface is merely a convenience feature for other altcoins that have the same transaction structure as Bitcoin to use.
However, we do not officially "support" these coins, and if they add / remove some feature we will not change to match them.
MarteXcoin does not have the same transaction structure as Bitcoin, so it is not possible to use bitcoinjs-lib without forking.
I think this should work, but I haven't tested. From a quick look at the source for your coin, it only puts time in the serialization and never uses it for sighash.
Also, it seems like Segwit is not supported for your coin... so just don't use any segwit functions.
I won't help any further, you can figure it out on your own.
Good luck!
diff --git a/src/transaction.js b/src/transaction.js
index b47cfe90..dd420190 100644
--- a/src/transaction.js
+++ b/src/transaction.js
@@ -41,6 +41,7 @@ function isOutput(out) {
class Transaction {
constructor() {
this.version = 1;
+ this.time = 0;
this.locktime = 0;
this.ins = [];
this.outs = [];
@@ -49,6 +50,7 @@ class Transaction {
const bufferReader = new bufferutils_1.BufferReader(buffer);
const tx = new Transaction();
tx.version = bufferReader.readInt32();
+ tx.time = bufferReader.readInt32();
const marker = bufferReader.readUInt8();
const flag = bufferReader.readUInt8();
let hasWitnesses = false;
@@ -153,10 +155,14 @@ class Transaction {
virtualSize() {
return Math.ceil(this.weight() / 4);
}
- byteLength(_ALLOW_WITNESS = true) {
+ byteLength(_ALLOW_WITNESS = true, _INCLUDE_TIME = true) {
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
+ let base = hasWitnesses ? 10 : 8;
+ if (_INCLUDE_TIME) {
+ base += 4;
+ }
return (
- (hasWitnesses ? 10 : 8) +
+ base +
varuint.encodingLength(this.ins.length) +
varuint.encodingLength(this.outs.length) +
this.ins.reduce((sum, input) => {
@@ -252,9 +258,9 @@ class Transaction {
txTmp.ins[inIndex].script = ourScript;
}
// serialize and hash
- const buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
+ const buffer = Buffer.allocUnsafe(txTmp.byteLength(false, false) + 4);
buffer.writeInt32LE(hashType, buffer.length - 4);
- txTmp.__toBuffer(buffer, 0, false);
+ txTmp.__toBuffer(buffer, 0, false, false);
return bcrypto.hash256(buffer);
}
hashForWitnessV0(inIndex, prevOutScript, value, hashType) {
@@ -352,13 +358,21 @@ class Transaction {
typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
this.ins[index].witness = witness;
}
- __toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
+ __toBuffer(
+ buffer,
+ initialOffset,
+ _ALLOW_WITNESS = false,
+ _WRITE_TIME = true,
+ ) {
if (!buffer) buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS));
const bufferWriter = new bufferutils_1.BufferWriter(
buffer,
initialOffset || 0,
);
bufferWriter.writeInt32(this.version);
+ if (_WRITE_TIME) {
+ bufferWriter.writeInt32(this.time);
+ }
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
if (hasWitnesses) {
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
diff --git a/ts_src/transaction.ts b/ts_src/transaction.ts
index 561ee8ae..061b912a 100644
--- a/ts_src/transaction.ts
+++ b/ts_src/transaction.ts
@@ -71,6 +71,7 @@ export class Transaction {
const tx = new Transaction();
tx.version = bufferReader.readInt32();
+ tx.time = bufferReader.readInt32();
const marker = bufferReader.readUInt8();
const flag = bufferReader.readUInt8();
@@ -136,6 +137,7 @@ export class Transaction {
}
version: number = 1;
+ time: number = 0;
locktime: number = 0;
ins: Input[] = [];
outs: Output[] = [];
@@ -206,11 +208,19 @@ export class Transaction {
return Math.ceil(this.weight() / 4);
}
- byteLength(_ALLOW_WITNESS: boolean = true): number {
+ byteLength(
+ _ALLOW_WITNESS: boolean = true,
+ _INCLUDE_TIME: boolean = true,
+ ): number {
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
+ let base = hasWitnesses ? 10 : 8;
+ if (_INCLUDE_TIME) {
+ base += 4;
+ }
+
return (
- (hasWitnesses ? 10 : 8) +
+ base +
varuint.encodingLength(this.ins.length) +
varuint.encodingLength(this.outs.length) +
this.ins.reduce((sum, input) => {
@@ -329,9 +339,11 @@ export class Transaction {
}
// serialize and hash
- const buffer: Buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
+ const buffer: Buffer = Buffer.allocUnsafe(
+ txTmp.byteLength(false, false) + 4,
+ );
buffer.writeInt32LE(hashType, buffer.length - 4);
- txTmp.__toBuffer(buffer, 0, false);
+ txTmp.__toBuffer(buffer, 0, false, false);
return bcrypto.hash256(buffer);
}
@@ -465,6 +477,7 @@ export class Transaction {
buffer?: Buffer,
initialOffset?: number,
_ALLOW_WITNESS: boolean = false,
+ _WRITE_TIME: boolean = true,
): Buffer {
if (!buffer)
buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS)) as Buffer;
@@ -472,6 +485,9 @@ export class Transaction {
const bufferWriter = new BufferWriter(buffer, initialOffset || 0);
bufferWriter.writeInt32(this.version);
+ if (_WRITE_TIME) {
+ bufferWriter.writeInt32(this.time);
+ }
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
diff --git a/types/transaction.d.ts b/types/transaction.d.ts
index 6846ea52..6a504e85 100644
--- a/types/transaction.d.ts
+++ b/types/transaction.d.ts
@@ -21,6 +21,7 @@ export declare class Transaction {
static fromHex(hex: string): Transaction;
static isCoinbaseHash(buffer: Buffer): boolean;
version: number;
+ time: number;
locktime: number;
ins: Input[];
outs: Output[];
@@ -30,7 +31,7 @@ export declare class Transaction {
hasWitnesses(): boolean;
weight(): number;
virtualSize(): number;
- byteLength(_ALLOW_WITNESS?: boolean): number;
+ byteLength(_ALLOW_WITNESS?: boolean, _INCLUDE_TIME?: boolean): number;
clone(): Transaction;
/**
* Hash transaction for signing a specific input.
thanks @junderw
/root/node_modules/bitcoinjs-lib/src/transaction.js:91 throw new Error('Transaction has unexpected data'); ^
Error: Transaction has unexpected data at Function.fromBuffer (/root/node_modules/bitcoinjs-lib/src/transaction.js:91:13) at addNonWitnessTxCache (/root/node_modules/bitcoinjs-lib/src/psbt.js:1207:40) at Psbt.addInput (/root/node_modules/bitcoinjs-lib/src/psbt.js:206:7) at Object. (/root/help_github.js:32:6)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
RawTransaction https://blockbook.martexcoin.org/tx/ac89ad0ef3ec8ac8bd120eb86d6372ff2375dd61a13839647f1c32a98fee87f5