@neuraiproject/neurai-sign-esp32 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,7 +44,7 @@ asset name, transferred amount, destination address, and fee more clearly.
44
44
 
45
45
  Before supplying UTXOs or PSBT inputs to this signing library, the host
46
46
  application must verify `getblockhash(0)` against
47
- `0000008b384aeffecdab182575dc4e86c9f07f90318c65088532660ed9a8a021`.
47
+ `000000c710413a4265a996d30c86043aedef6a563900f1bc43fea61d5b2dc77d`.
48
48
  The device does not query the chain. Discard UTXOs and PSBTs assembled from
49
49
  the previous testnet. All three AuthScript address families activate on the
50
50
  reset testnet at block 10; outputs included earlier have no AuthScript spend
@@ -958,6 +958,32 @@ ArduinoJson parser was checked on the host up to MAX_MONEY; this does not replac
958
958
  an on-device signing and display check of the firmware installed on your ESP32.
959
959
  No global bigint JSON serializer is required.
960
960
 
961
+ ## Upgrading to 0.8.2
962
+
963
+ Previous transactions are read with `@neuraiproject/neurai-create-transaction`
964
+ `^0.10.0`, which follows the node's NIP-014 encoding (Neurai DePIN-Test
965
+ `d4ff4ef`, live on the testnet reset of 2026-11-05): reference inputs are
966
+ announced by flag `0x02` of the extended format, and a version 3 transaction
967
+ without them has the plain layout. With 0.8.1 a PSBT whose previous
968
+ transaction is version 3 in that encoding is unreadable. Version 3
969
+ transactions of the previous testnet (an empty reference list after the
970
+ outputs) no longer read. What the device signs does not change: NeuraiHW still
971
+ refuses a version 3 transaction or previous transaction.
972
+
973
+ ## Upgrading to 0.8.1
974
+
975
+ React Native (Hermes): Buffer methods are no longer called on `subarray()`
976
+ results. Under Hermes a subarray of the bundled Buffer is a plain `Uint8Array`,
977
+ whose `toString("hex")` is not hex:
978
+
979
+ - reading the firmware's minimal PSBT refused a valid device answer as "a PSBT
980
+ for a different transaction";
981
+ - the DePIN device identity could not read the `s` value of a device signature
982
+ (`derSignatureToRecoverable`, message signatures), and failed.
983
+
984
+ `src/psbt-device.test.ts` and `src/depin-identity.test.ts` run those paths with
985
+ Hermes' `subarray`.
986
+
961
987
  ## Upgrading to 0.8.0
962
988
 
963
989
  The PSBT helpers no longer use bitcoinjs-lib's `Psbt`: they are built on the
@@ -9241,12 +9241,19 @@ var NeuraiSignESP32Bundle = (function () {
9241
9241
  for (let i = 0; i < voutCount; i += 1)
9242
9242
  outputs.push(readOutput(reader));
9243
9243
  }
9244
- // NIP-014: vrefin sits between vout and witness, v3 only (even when empty).
9244
+ // NIP-014: reference inputs sit between vout and witness, announced by flag
9245
+ // 0x02. Without the flag a version 3 transaction reads as 1.0.7 reads it.
9245
9246
  const vrefin = [];
9246
- if (version === 3) {
9247
+ const hasRefInputs = (flags & 2) !== 0;
9248
+ if (hasRefInputs) {
9249
+ flags ^= 2;
9247
9250
  const refCount = reader.readCount(36, 'refinput');
9248
9251
  for (let i = 0; i < refCount; i += 1)
9249
9252
  vrefin.push(readOutpoint(reader));
9253
+ if (vrefin.length === 0) {
9254
+ // One encoding per transaction: the flag means some.
9255
+ throw new Error('Superfluous reference inputs record (flag 0x02 with no reference inputs)');
9256
+ }
9250
9257
  }
9251
9258
  if (flags & 1) {
9252
9259
  flags ^= 1;
@@ -9259,6 +9266,11 @@ var NeuraiSignESP32Bundle = (function () {
9259
9266
  }
9260
9267
  input.witness = stack;
9261
9268
  }
9269
+ if (hasRefInputs && !inputs.some(inputHasWitness)) {
9270
+ // With reference inputs the witness flag means some witness; 1.0.7 reads
9271
+ // an empty witness record without them, and so does the node.
9272
+ throw new Error('Superfluous witness record (flag 0x01 with no witness data)');
9273
+ }
9262
9274
  }
9263
9275
  if (flags) {
9264
9276
  throw new Error(`Unknown transaction optional data (flags 0x${flags.toString(16)})`);
@@ -9296,10 +9308,13 @@ var NeuraiSignESP32Bundle = (function () {
9296
9308
  throw new Error(`vrefin requires transaction version 3 (got version ${tx.version})`);
9297
9309
  }
9298
9310
  const withWitness = (options.includeWitness ?? true) && tx.inputs.some(inputHasWitness);
9311
+ // NIP-014: reference inputs keep the extended format without witnesses too,
9312
+ // because the txid commits to them.
9313
+ const flags = (withWitness ? 1 : 0) | (vrefin.length > 0 ? 2 : 0);
9299
9314
  const parts = [u32LE(tx.version >>> 0)];
9300
- if (withWitness) {
9315
+ if (flags) {
9301
9316
  // Extended format: dummy empty vin + flags byte.
9302
- parts.push(Uint8Array.of(0x00, 0x01));
9317
+ parts.push(Uint8Array.of(0x00, flags));
9303
9318
  }
9304
9319
  parts.push(compactSize(tx.inputs.length));
9305
9320
  for (const input of tx.inputs)
@@ -9307,7 +9322,7 @@ var NeuraiSignESP32Bundle = (function () {
9307
9322
  parts.push(compactSize(tx.outputs.length));
9308
9323
  for (const output of tx.outputs)
9309
9324
  parts.push(serializeCodecOutput(output));
9310
- if (tx.version === 3) {
9325
+ if (flags & 2) {
9311
9326
  parts.push(compactSize(vrefin.length));
9312
9327
  for (const ref of vrefin)
9313
9328
  parts.push(serializeOutpoint(ref));
@@ -11038,10 +11053,12 @@ var NeuraiSignESP32Bundle = (function () {
11038
11053
  entries.push({ key, value: readBytes(valueLen.value) });
11039
11054
  }
11040
11055
  };
11056
+ // A copy: under Hermes a subarray of the bundled Buffer is a plain Uint8Array,
11057
+ // without toString("hex") / readUInt32LE.
11041
11058
  const readBytes = (length) => {
11042
11059
  if (offset + length > buffer.length)
11043
11060
  throw new Error("Format Error: Unexpected End of PSBT");
11044
- const bytes = buffer.subarray(offset, offset + length);
11061
+ const bytes = bufferExports.Buffer.from(buffer.subarray(offset, offset + length));
11045
11062
  offset += length;
11046
11063
  return bytes;
11047
11064
  };
@@ -11057,7 +11074,7 @@ var NeuraiSignESP32Bundle = (function () {
11057
11074
  const partialSig = [];
11058
11075
  for (const { key, value } of readMap()) {
11059
11076
  if (key[0] === PSBT_IN_PARTIAL_SIG && key.length > 1) {
11060
- partialSig.push({ pubkeyHex: key.subarray(1).toString("hex"), signatureHex: value.toString("hex") });
11077
+ partialSig.push({ pubkeyHex: key.toString("hex", 1), signatureHex: value.toString("hex") });
11061
11078
  }
11062
11079
  else if (key.length === 1 && key[0] === PSBT_IN_SIGHASH_TYPE && value.length === 4) {
11063
11080
  if (value.readUInt32LE(0) !== input.sighashType) {
@@ -17047,7 +17064,9 @@ var NeuraiSignESP32Bundle = (function () {
17047
17064
  if (signature$1.length !== 64) {
17048
17065
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", `Compact signature must be 64 bytes; got ${signature$1.length}`);
17049
17066
  }
17050
- const s = BigInt(`0x${signature$1.subarray(32).toString("hex")}`);
17067
+ // Not subarray(32).toString("hex"): under Hermes that subarray is a plain
17068
+ // Uint8Array, whose toString is not hex.
17069
+ const s = BigInt(`0x${signature$1.toString("hex", 32)}`);
17051
17070
  if (s === 0n || s >= SECP256K1_ORDER) {
17052
17071
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", "The device returned a signature with an invalid s scalar");
17053
17072
  }
@@ -17059,7 +17078,8 @@ var NeuraiSignESP32Bundle = (function () {
17059
17078
  }
17060
17079
  function compactToDer(compact) {
17061
17080
  const encoded = bufferExports.Buffer.from(signature.encode(compact, 0x01));
17062
- return encoded.subarray(0, encoded.length - 1);
17081
+ // A copy, so it is a Buffer under Hermes too (a subarray there is a plain Uint8Array).
17082
+ return bufferExports.Buffer.from(encoded.subarray(0, encoded.length - 1));
17063
17083
  }
17064
17084
  function assertOperationCount(value) {
17065
17085
  if (!Number.isSafeInteger(value) || value < 0) {
package/dist/browser.js CHANGED
@@ -9238,12 +9238,19 @@ function parseTransaction(hex) {
9238
9238
  for (let i = 0; i < voutCount; i += 1)
9239
9239
  outputs.push(readOutput(reader));
9240
9240
  }
9241
- // NIP-014: vrefin sits between vout and witness, v3 only (even when empty).
9241
+ // NIP-014: reference inputs sit between vout and witness, announced by flag
9242
+ // 0x02. Without the flag a version 3 transaction reads as 1.0.7 reads it.
9242
9243
  const vrefin = [];
9243
- if (version === 3) {
9244
+ const hasRefInputs = (flags & 2) !== 0;
9245
+ if (hasRefInputs) {
9246
+ flags ^= 2;
9244
9247
  const refCount = reader.readCount(36, 'refinput');
9245
9248
  for (let i = 0; i < refCount; i += 1)
9246
9249
  vrefin.push(readOutpoint(reader));
9250
+ if (vrefin.length === 0) {
9251
+ // One encoding per transaction: the flag means some.
9252
+ throw new Error('Superfluous reference inputs record (flag 0x02 with no reference inputs)');
9253
+ }
9247
9254
  }
9248
9255
  if (flags & 1) {
9249
9256
  flags ^= 1;
@@ -9256,6 +9263,11 @@ function parseTransaction(hex) {
9256
9263
  }
9257
9264
  input.witness = stack;
9258
9265
  }
9266
+ if (hasRefInputs && !inputs.some(inputHasWitness)) {
9267
+ // With reference inputs the witness flag means some witness; 1.0.7 reads
9268
+ // an empty witness record without them, and so does the node.
9269
+ throw new Error('Superfluous witness record (flag 0x01 with no witness data)');
9270
+ }
9259
9271
  }
9260
9272
  if (flags) {
9261
9273
  throw new Error(`Unknown transaction optional data (flags 0x${flags.toString(16)})`);
@@ -9293,10 +9305,13 @@ function serializeTransaction(tx, options = {}) {
9293
9305
  throw new Error(`vrefin requires transaction version 3 (got version ${tx.version})`);
9294
9306
  }
9295
9307
  const withWitness = (options.includeWitness ?? true) && tx.inputs.some(inputHasWitness);
9308
+ // NIP-014: reference inputs keep the extended format without witnesses too,
9309
+ // because the txid commits to them.
9310
+ const flags = (withWitness ? 1 : 0) | (vrefin.length > 0 ? 2 : 0);
9296
9311
  const parts = [u32LE(tx.version >>> 0)];
9297
- if (withWitness) {
9312
+ if (flags) {
9298
9313
  // Extended format: dummy empty vin + flags byte.
9299
- parts.push(Uint8Array.of(0x00, 0x01));
9314
+ parts.push(Uint8Array.of(0x00, flags));
9300
9315
  }
9301
9316
  parts.push(compactSize(tx.inputs.length));
9302
9317
  for (const input of tx.inputs)
@@ -9304,7 +9319,7 @@ function serializeTransaction(tx, options = {}) {
9304
9319
  parts.push(compactSize(tx.outputs.length));
9305
9320
  for (const output of tx.outputs)
9306
9321
  parts.push(serializeCodecOutput(output));
9307
- if (tx.version === 3) {
9322
+ if (flags & 2) {
9308
9323
  parts.push(compactSize(vrefin.length));
9309
9324
  for (const ref of vrefin)
9310
9325
  parts.push(serializeOutpoint(ref));
@@ -11035,10 +11050,12 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11035
11050
  entries.push({ key, value: readBytes(valueLen.value) });
11036
11051
  }
11037
11052
  };
11053
+ // A copy: under Hermes a subarray of the bundled Buffer is a plain Uint8Array,
11054
+ // without toString("hex") / readUInt32LE.
11038
11055
  const readBytes = (length) => {
11039
11056
  if (offset + length > buffer.length)
11040
11057
  throw new Error("Format Error: Unexpected End of PSBT");
11041
- const bytes = buffer.subarray(offset, offset + length);
11058
+ const bytes = bufferExports.Buffer.from(buffer.subarray(offset, offset + length));
11042
11059
  offset += length;
11043
11060
  return bytes;
11044
11061
  };
@@ -11054,7 +11071,7 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11054
11071
  const partialSig = [];
11055
11072
  for (const { key, value } of readMap()) {
11056
11073
  if (key[0] === PSBT_IN_PARTIAL_SIG && key.length > 1) {
11057
- partialSig.push({ pubkeyHex: key.subarray(1).toString("hex"), signatureHex: value.toString("hex") });
11074
+ partialSig.push({ pubkeyHex: key.toString("hex", 1), signatureHex: value.toString("hex") });
11058
11075
  }
11059
11076
  else if (key.length === 1 && key[0] === PSBT_IN_SIGHASH_TYPE && value.length === 4) {
11060
11077
  if (value.readUInt32LE(0) !== input.sighashType) {
@@ -17044,7 +17061,9 @@ function derToCompactLowS(derHex) {
17044
17061
  if (signature$1.length !== 64) {
17045
17062
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", `Compact signature must be 64 bytes; got ${signature$1.length}`);
17046
17063
  }
17047
- const s = BigInt(`0x${signature$1.subarray(32).toString("hex")}`);
17064
+ // Not subarray(32).toString("hex"): under Hermes that subarray is a plain
17065
+ // Uint8Array, whose toString is not hex.
17066
+ const s = BigInt(`0x${signature$1.toString("hex", 32)}`);
17048
17067
  if (s === 0n || s >= SECP256K1_ORDER) {
17049
17068
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", "The device returned a signature with an invalid s scalar");
17050
17069
  }
@@ -17056,7 +17075,8 @@ function derToCompactLowS(derHex) {
17056
17075
  }
17057
17076
  function compactToDer(compact) {
17058
17077
  const encoded = bufferExports.Buffer.from(signature.encode(compact, 0x01));
17059
- return encoded.subarray(0, encoded.length - 1);
17078
+ // A copy, so it is a Buffer under Hermes too (a subarray there is a plain Uint8Array).
17079
+ return bufferExports.Buffer.from(encoded.subarray(0, encoded.length - 1));
17060
17080
  }
17061
17081
  function assertOperationCount(value) {
17062
17082
  if (!Number.isSafeInteger(value) || value < 0) {
package/dist/index.cjs CHANGED
@@ -9240,12 +9240,19 @@ function parseTransaction(hex) {
9240
9240
  for (let i = 0; i < voutCount; i += 1)
9241
9241
  outputs.push(readOutput(reader));
9242
9242
  }
9243
- // NIP-014: vrefin sits between vout and witness, v3 only (even when empty).
9243
+ // NIP-014: reference inputs sit between vout and witness, announced by flag
9244
+ // 0x02. Without the flag a version 3 transaction reads as 1.0.7 reads it.
9244
9245
  const vrefin = [];
9245
- if (version === 3) {
9246
+ const hasRefInputs = (flags & 2) !== 0;
9247
+ if (hasRefInputs) {
9248
+ flags ^= 2;
9246
9249
  const refCount = reader.readCount(36, 'refinput');
9247
9250
  for (let i = 0; i < refCount; i += 1)
9248
9251
  vrefin.push(readOutpoint(reader));
9252
+ if (vrefin.length === 0) {
9253
+ // One encoding per transaction: the flag means some.
9254
+ throw new Error('Superfluous reference inputs record (flag 0x02 with no reference inputs)');
9255
+ }
9249
9256
  }
9250
9257
  if (flags & 1) {
9251
9258
  flags ^= 1;
@@ -9258,6 +9265,11 @@ function parseTransaction(hex) {
9258
9265
  }
9259
9266
  input.witness = stack;
9260
9267
  }
9268
+ if (hasRefInputs && !inputs.some(inputHasWitness)) {
9269
+ // With reference inputs the witness flag means some witness; 1.0.7 reads
9270
+ // an empty witness record without them, and so does the node.
9271
+ throw new Error('Superfluous witness record (flag 0x01 with no witness data)');
9272
+ }
9261
9273
  }
9262
9274
  if (flags) {
9263
9275
  throw new Error(`Unknown transaction optional data (flags 0x${flags.toString(16)})`);
@@ -9295,10 +9307,13 @@ function serializeTransaction(tx, options = {}) {
9295
9307
  throw new Error(`vrefin requires transaction version 3 (got version ${tx.version})`);
9296
9308
  }
9297
9309
  const withWitness = (options.includeWitness ?? true) && tx.inputs.some(inputHasWitness);
9310
+ // NIP-014: reference inputs keep the extended format without witnesses too,
9311
+ // because the txid commits to them.
9312
+ const flags = (withWitness ? 1 : 0) | (vrefin.length > 0 ? 2 : 0);
9298
9313
  const parts = [u32LE(tx.version >>> 0)];
9299
- if (withWitness) {
9314
+ if (flags) {
9300
9315
  // Extended format: dummy empty vin + flags byte.
9301
- parts.push(Uint8Array.of(0x00, 0x01));
9316
+ parts.push(Uint8Array.of(0x00, flags));
9302
9317
  }
9303
9318
  parts.push(compactSize(tx.inputs.length));
9304
9319
  for (const input of tx.inputs)
@@ -9306,7 +9321,7 @@ function serializeTransaction(tx, options = {}) {
9306
9321
  parts.push(compactSize(tx.outputs.length));
9307
9322
  for (const output of tx.outputs)
9308
9323
  parts.push(serializeCodecOutput(output));
9309
- if (tx.version === 3) {
9324
+ if (flags & 2) {
9310
9325
  parts.push(compactSize(vrefin.length));
9311
9326
  for (const ref of vrefin)
9312
9327
  parts.push(serializeOutpoint(ref));
@@ -11037,10 +11052,12 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11037
11052
  entries.push({ key, value: readBytes(valueLen.value) });
11038
11053
  }
11039
11054
  };
11055
+ // A copy: under Hermes a subarray of the bundled Buffer is a plain Uint8Array,
11056
+ // without toString("hex") / readUInt32LE.
11040
11057
  const readBytes = (length) => {
11041
11058
  if (offset + length > buffer.length)
11042
11059
  throw new Error("Format Error: Unexpected End of PSBT");
11043
- const bytes = buffer.subarray(offset, offset + length);
11060
+ const bytes = bufferExports.Buffer.from(buffer.subarray(offset, offset + length));
11044
11061
  offset += length;
11045
11062
  return bytes;
11046
11063
  };
@@ -11056,7 +11073,7 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11056
11073
  const partialSig = [];
11057
11074
  for (const { key, value } of readMap()) {
11058
11075
  if (key[0] === PSBT_IN_PARTIAL_SIG && key.length > 1) {
11059
- partialSig.push({ pubkeyHex: key.subarray(1).toString("hex"), signatureHex: value.toString("hex") });
11076
+ partialSig.push({ pubkeyHex: key.toString("hex", 1), signatureHex: value.toString("hex") });
11060
11077
  }
11061
11078
  else if (key.length === 1 && key[0] === PSBT_IN_SIGHASH_TYPE && value.length === 4) {
11062
11079
  if (value.readUInt32LE(0) !== input.sighashType) {
@@ -17046,7 +17063,9 @@ function derToCompactLowS(derHex) {
17046
17063
  if (signature$1.length !== 64) {
17047
17064
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", `Compact signature must be 64 bytes; got ${signature$1.length}`);
17048
17065
  }
17049
- const s = BigInt(`0x${signature$1.subarray(32).toString("hex")}`);
17066
+ // Not subarray(32).toString("hex"): under Hermes that subarray is a plain
17067
+ // Uint8Array, whose toString is not hex.
17068
+ const s = BigInt(`0x${signature$1.toString("hex", 32)}`);
17050
17069
  if (s === 0n || s >= SECP256K1_ORDER) {
17051
17070
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", "The device returned a signature with an invalid s scalar");
17052
17071
  }
@@ -17058,7 +17077,8 @@ function derToCompactLowS(derHex) {
17058
17077
  }
17059
17078
  function compactToDer(compact) {
17060
17079
  const encoded = bufferExports.Buffer.from(signature.encode(compact, 0x01));
17061
- return encoded.subarray(0, encoded.length - 1);
17080
+ // A copy, so it is a Buffer under Hermes too (a subarray there is a plain Uint8Array).
17081
+ return bufferExports.Buffer.from(encoded.subarray(0, encoded.length - 1));
17062
17082
  }
17063
17083
  function assertOperationCount(value) {
17064
17084
  if (!Number.isSafeInteger(value) || value < 0) {
package/dist/index.js CHANGED
@@ -9238,12 +9238,19 @@ function parseTransaction(hex) {
9238
9238
  for (let i = 0; i < voutCount; i += 1)
9239
9239
  outputs.push(readOutput(reader));
9240
9240
  }
9241
- // NIP-014: vrefin sits between vout and witness, v3 only (even when empty).
9241
+ // NIP-014: reference inputs sit between vout and witness, announced by flag
9242
+ // 0x02. Without the flag a version 3 transaction reads as 1.0.7 reads it.
9242
9243
  const vrefin = [];
9243
- if (version === 3) {
9244
+ const hasRefInputs = (flags & 2) !== 0;
9245
+ if (hasRefInputs) {
9246
+ flags ^= 2;
9244
9247
  const refCount = reader.readCount(36, 'refinput');
9245
9248
  for (let i = 0; i < refCount; i += 1)
9246
9249
  vrefin.push(readOutpoint(reader));
9250
+ if (vrefin.length === 0) {
9251
+ // One encoding per transaction: the flag means some.
9252
+ throw new Error('Superfluous reference inputs record (flag 0x02 with no reference inputs)');
9253
+ }
9247
9254
  }
9248
9255
  if (flags & 1) {
9249
9256
  flags ^= 1;
@@ -9256,6 +9263,11 @@ function parseTransaction(hex) {
9256
9263
  }
9257
9264
  input.witness = stack;
9258
9265
  }
9266
+ if (hasRefInputs && !inputs.some(inputHasWitness)) {
9267
+ // With reference inputs the witness flag means some witness; 1.0.7 reads
9268
+ // an empty witness record without them, and so does the node.
9269
+ throw new Error('Superfluous witness record (flag 0x01 with no witness data)');
9270
+ }
9259
9271
  }
9260
9272
  if (flags) {
9261
9273
  throw new Error(`Unknown transaction optional data (flags 0x${flags.toString(16)})`);
@@ -9293,10 +9305,13 @@ function serializeTransaction(tx, options = {}) {
9293
9305
  throw new Error(`vrefin requires transaction version 3 (got version ${tx.version})`);
9294
9306
  }
9295
9307
  const withWitness = (options.includeWitness ?? true) && tx.inputs.some(inputHasWitness);
9308
+ // NIP-014: reference inputs keep the extended format without witnesses too,
9309
+ // because the txid commits to them.
9310
+ const flags = (withWitness ? 1 : 0) | (vrefin.length > 0 ? 2 : 0);
9296
9311
  const parts = [u32LE(tx.version >>> 0)];
9297
- if (withWitness) {
9312
+ if (flags) {
9298
9313
  // Extended format: dummy empty vin + flags byte.
9299
- parts.push(Uint8Array.of(0x00, 0x01));
9314
+ parts.push(Uint8Array.of(0x00, flags));
9300
9315
  }
9301
9316
  parts.push(compactSize(tx.inputs.length));
9302
9317
  for (const input of tx.inputs)
@@ -9304,7 +9319,7 @@ function serializeTransaction(tx, options = {}) {
9304
9319
  parts.push(compactSize(tx.outputs.length));
9305
9320
  for (const output of tx.outputs)
9306
9321
  parts.push(serializeCodecOutput(output));
9307
- if (tx.version === 3) {
9322
+ if (flags & 2) {
9308
9323
  parts.push(compactSize(vrefin.length));
9309
9324
  for (const ref of vrefin)
9310
9325
  parts.push(serializeOutpoint(ref));
@@ -11035,10 +11050,12 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11035
11050
  entries.push({ key, value: readBytes(valueLen.value) });
11036
11051
  }
11037
11052
  };
11053
+ // A copy: under Hermes a subarray of the bundled Buffer is a plain Uint8Array,
11054
+ // without toString("hex") / readUInt32LE.
11038
11055
  const readBytes = (length) => {
11039
11056
  if (offset + length > buffer.length)
11040
11057
  throw new Error("Format Error: Unexpected End of PSBT");
11041
- const bytes = buffer.subarray(offset, offset + length);
11058
+ const bytes = bufferExports.Buffer.from(buffer.subarray(offset, offset + length));
11042
11059
  offset += length;
11043
11060
  return bytes;
11044
11061
  };
@@ -11054,7 +11071,7 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11054
11071
  const partialSig = [];
11055
11072
  for (const { key, value } of readMap()) {
11056
11073
  if (key[0] === PSBT_IN_PARTIAL_SIG && key.length > 1) {
11057
- partialSig.push({ pubkeyHex: key.subarray(1).toString("hex"), signatureHex: value.toString("hex") });
11074
+ partialSig.push({ pubkeyHex: key.toString("hex", 1), signatureHex: value.toString("hex") });
11058
11075
  }
11059
11076
  else if (key.length === 1 && key[0] === PSBT_IN_SIGHASH_TYPE && value.length === 4) {
11060
11077
  if (value.readUInt32LE(0) !== input.sighashType) {
@@ -17044,7 +17061,9 @@ function derToCompactLowS(derHex) {
17044
17061
  if (signature$1.length !== 64) {
17045
17062
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", `Compact signature must be 64 bytes; got ${signature$1.length}`);
17046
17063
  }
17047
- const s = BigInt(`0x${signature$1.subarray(32).toString("hex")}`);
17064
+ // Not subarray(32).toString("hex"): under Hermes that subarray is a plain
17065
+ // Uint8Array, whose toString is not hex.
17066
+ const s = BigInt(`0x${signature$1.toString("hex", 32)}`);
17048
17067
  if (s === 0n || s >= SECP256K1_ORDER) {
17049
17068
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", "The device returned a signature with an invalid s scalar");
17050
17069
  }
@@ -17056,7 +17075,8 @@ function derToCompactLowS(derHex) {
17056
17075
  }
17057
17076
  function compactToDer(compact) {
17058
17077
  const encoded = bufferExports.Buffer.from(signature.encode(compact, 0x01));
17059
- return encoded.subarray(0, encoded.length - 1);
17078
+ // A copy, so it is a Buffer under Hermes too (a subarray there is a plain Uint8Array).
17079
+ return bufferExports.Buffer.from(encoded.subarray(0, encoded.length - 1));
17060
17080
  }
17061
17081
  function assertOperationCount(value) {
17062
17082
  if (!Number.isSafeInteger(value) || value < 0) {
@@ -9238,12 +9238,19 @@ function parseTransaction(hex) {
9238
9238
  for (let i = 0; i < voutCount; i += 1)
9239
9239
  outputs.push(readOutput(reader));
9240
9240
  }
9241
- // NIP-014: vrefin sits between vout and witness, v3 only (even when empty).
9241
+ // NIP-014: reference inputs sit between vout and witness, announced by flag
9242
+ // 0x02. Without the flag a version 3 transaction reads as 1.0.7 reads it.
9242
9243
  const vrefin = [];
9243
- if (version === 3) {
9244
+ const hasRefInputs = (flags & 2) !== 0;
9245
+ if (hasRefInputs) {
9246
+ flags ^= 2;
9244
9247
  const refCount = reader.readCount(36, 'refinput');
9245
9248
  for (let i = 0; i < refCount; i += 1)
9246
9249
  vrefin.push(readOutpoint(reader));
9250
+ if (vrefin.length === 0) {
9251
+ // One encoding per transaction: the flag means some.
9252
+ throw new Error('Superfluous reference inputs record (flag 0x02 with no reference inputs)');
9253
+ }
9247
9254
  }
9248
9255
  if (flags & 1) {
9249
9256
  flags ^= 1;
@@ -9256,6 +9263,11 @@ function parseTransaction(hex) {
9256
9263
  }
9257
9264
  input.witness = stack;
9258
9265
  }
9266
+ if (hasRefInputs && !inputs.some(inputHasWitness)) {
9267
+ // With reference inputs the witness flag means some witness; 1.0.7 reads
9268
+ // an empty witness record without them, and so does the node.
9269
+ throw new Error('Superfluous witness record (flag 0x01 with no witness data)');
9270
+ }
9259
9271
  }
9260
9272
  if (flags) {
9261
9273
  throw new Error(`Unknown transaction optional data (flags 0x${flags.toString(16)})`);
@@ -9293,10 +9305,13 @@ function serializeTransaction(tx, options = {}) {
9293
9305
  throw new Error(`vrefin requires transaction version 3 (got version ${tx.version})`);
9294
9306
  }
9295
9307
  const withWitness = (options.includeWitness ?? true) && tx.inputs.some(inputHasWitness);
9308
+ // NIP-014: reference inputs keep the extended format without witnesses too,
9309
+ // because the txid commits to them.
9310
+ const flags = (withWitness ? 1 : 0) | (vrefin.length > 0 ? 2 : 0);
9296
9311
  const parts = [u32LE(tx.version >>> 0)];
9297
- if (withWitness) {
9312
+ if (flags) {
9298
9313
  // Extended format: dummy empty vin + flags byte.
9299
- parts.push(Uint8Array.of(0x00, 0x01));
9314
+ parts.push(Uint8Array.of(0x00, flags));
9300
9315
  }
9301
9316
  parts.push(compactSize(tx.inputs.length));
9302
9317
  for (const input of tx.inputs)
@@ -9304,7 +9319,7 @@ function serializeTransaction(tx, options = {}) {
9304
9319
  parts.push(compactSize(tx.outputs.length));
9305
9320
  for (const output of tx.outputs)
9306
9321
  parts.push(serializeCodecOutput(output));
9307
- if (tx.version === 3) {
9322
+ if (flags & 2) {
9308
9323
  parts.push(compactSize(vrefin.length));
9309
9324
  for (const ref of vrefin)
9310
9325
  parts.push(serializeOutpoint(ref));
@@ -11035,10 +11050,12 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11035
11050
  entries.push({ key, value: readBytes(valueLen.value) });
11036
11051
  }
11037
11052
  };
11053
+ // A copy: under Hermes a subarray of the bundled Buffer is a plain Uint8Array,
11054
+ // without toString("hex") / readUInt32LE.
11038
11055
  const readBytes = (length) => {
11039
11056
  if (offset + length > buffer.length)
11040
11057
  throw new Error("Format Error: Unexpected End of PSBT");
11041
- const bytes = buffer.subarray(offset, offset + length);
11058
+ const bytes = bufferExports.Buffer.from(buffer.subarray(offset, offset + length));
11042
11059
  offset += length;
11043
11060
  return bytes;
11044
11061
  };
@@ -11054,7 +11071,7 @@ function applyMinimalDevicePsbt(originalPsbtBase64, signedPsbtBase64) {
11054
11071
  const partialSig = [];
11055
11072
  for (const { key, value } of readMap()) {
11056
11073
  if (key[0] === PSBT_IN_PARTIAL_SIG && key.length > 1) {
11057
- partialSig.push({ pubkeyHex: key.subarray(1).toString("hex"), signatureHex: value.toString("hex") });
11074
+ partialSig.push({ pubkeyHex: key.toString("hex", 1), signatureHex: value.toString("hex") });
11058
11075
  }
11059
11076
  else if (key.length === 1 && key[0] === PSBT_IN_SIGHASH_TYPE && value.length === 4) {
11060
11077
  if (value.readUInt32LE(0) !== input.sighashType) {
@@ -17044,7 +17061,9 @@ function derToCompactLowS(derHex) {
17044
17061
  if (signature$1.length !== 64) {
17045
17062
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", `Compact signature must be 64 bytes; got ${signature$1.length}`);
17046
17063
  }
17047
- const s = BigInt(`0x${signature$1.subarray(32).toString("hex")}`);
17064
+ // Not subarray(32).toString("hex"): under Hermes that subarray is a plain
17065
+ // Uint8Array, whose toString is not hex.
17066
+ const s = BigInt(`0x${signature$1.toString("hex", 32)}`);
17048
17067
  if (s === 0n || s >= SECP256K1_ORDER) {
17049
17068
  throw new DepinDeviceIdentityError("INVALID_DEVICE_SIGNATURE", "The device returned a signature with an invalid s scalar");
17050
17069
  }
@@ -17056,7 +17075,8 @@ function derToCompactLowS(derHex) {
17056
17075
  }
17057
17076
  function compactToDer(compact) {
17058
17077
  const encoded = bufferExports.Buffer.from(signature.encode(compact, 0x01));
17059
- return encoded.subarray(0, encoded.length - 1);
17078
+ // A copy, so it is a Buffer under Hermes too (a subarray there is a plain Uint8Array).
17079
+ return bufferExports.Buffer.from(encoded.subarray(0, encoded.length - 1));
17060
17080
  }
17061
17081
  function assertOperationCount(value) {
17062
17082
  if (!Number.isSafeInteger(value) || value < 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neuraiproject/neurai-sign-esp32",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "Create and sign PSBTs for Neurai via ESP32 hardware wallet",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -61,7 +61,7 @@
61
61
  "homepage": "https://github.com/neuraiproject/neurai-sign-esp32#readme",
62
62
  "dependencies": {
63
63
  "@bitcoinerlab/secp256k1": "1.2.0",
64
- "@neuraiproject/neurai-create-transaction": "^0.9.3",
64
+ "@neuraiproject/neurai-create-transaction": "^0.10.0",
65
65
  "bech32": "^2.0.0",
66
66
  "bitcoinjs-lib": "7.0.1",
67
67
  "buffer": "^6.0.3",