Page MenuHomePhabricator

[ecash-lib] Add `UnsignedTx` and `UnsignedTxInput`
ClosedPublic

Authored by tobias_ruck on Fri, Apr 12, 01:33.

Details

Reviewers
bytesofman
Group Reviewers
Restricted Project
Commits
rABC07cab0629902: [ecash-lib] Add `UnsignedTx` and `UnsignedTxInput`
Summary

This allows building the sighash preimages required for signing inputs.

We use UnsignedTx to precompute the hashes required for building the preimages, in case we need to sign a lot of inputs.

Only BIP143 sighash preimages are implemented, but not LEGACY; they aren't available on eCash since UAHF. Implementing it might be useful if we want to allow people to use ecash-lib on Dogecoin.

Depends on D15954 and D15952.

Test Plan

npm run test && npm run build

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Tail of the build log:

  Downloaded digest v0.10.7
  Downloaded thiserror-impl v1.0.58
  Downloaded wasm-bindgen-macro v0.2.92
  Downloaded bumpalo v3.16.0
  Downloaded wasm-bindgen-backend v0.2.92
  Downloaded wasm-bindgen v0.2.92
  Downloaded typenum v1.17.0
  Downloaded thiserror v1.0.58
  Downloaded wasm-bindgen-macro-support v0.2.92
  Downloaded ripemd v0.1.3
  Downloaded sha2 v0.10.8
  Downloaded generic-array v0.14.7
  Downloaded crypto-common v0.1.6
  Downloaded cpufeatures v0.2.12
   Compiling proc-macro2 v1.0.79
   Compiling unicode-ident v1.0.12
   Compiling typenum v1.17.0
   Compiling version_check v0.9.4
   Compiling wasm-bindgen-shared v0.2.92
   Compiling cc v1.0.92
   Compiling once_cell v1.19.0
   Compiling log v0.4.21
   Compiling bumpalo v3.16.0
   Compiling cfg-if v1.0.0
   Compiling wasm-bindgen v0.2.92
   Compiling thiserror v1.0.58
   Compiling abc-rust-lint v0.1.0 (/work/chronik/abc-rust-lint)
   Compiling generic-array v0.14.7
   Compiling quote v1.0.36
   Compiling syn v2.0.58
   Compiling secp256k1-sys-abc v0.4.1 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling crypto-common v0.1.6
   Compiling block-buffer v0.10.4
   Compiling digest v0.10.7
   Compiling sha2 v0.10.8
   Compiling ripemd v0.1.3
   Compiling wasm-bindgen-backend v0.2.92
   Compiling secp256k1-abc v0.20.3 (https://github.com/raipay/secp256k1-abc?rev=b23e742#b23e7421)
   Compiling thiserror-impl v1.0.58
   Compiling wasm-bindgen-macro-support v0.2.92
   Compiling wasm-bindgen-macro v0.2.92
   Compiling ecash-lib-wasm v0.1.0 (/work/modules/ecash-lib-wasm)
    Finished release-wasm [optimized] target(s) in 5.05s
/work/modules/ecash-lib /work/modules/ecash-lib-wasm /work/abc-ci-builds/ecash-lib-tests

added 361 packages, and audited 362 packages in 3s

60 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
CI configured to test build. Building...

> ecash-lib@0.1.0 build
> tsc && cp -r ./src/ffi ./dist

src/unsignedTx.test.ts(450,35): error TS2739: Type '{ prevOut: OutPoint; }' is missing the following properties from type 'TxInput': script, sequence
src/unsignedTx.test.ts(459,63): error TS2741: Property 'value' is missing in type '{}' but required in type 'SignData'.
src/unsignedTx.test.ts(472,29): error TS2741: Property 'value' is missing in type '{ outputScript: Script; }' but required in type 'SignData'.
Build ecash-lib-tests failed with exit code 2

fix tsc build, add another test

fix sighash hashSequences for SINGLE and NONE without ANYONECANPAY

...what is a sighash preimage?

...what is a sighash preimage?

A sighash is the hash you have to sign for a signature for OP_CHECKSIG, and a sighash preimage is the preimage (ie “unhashed”) data of that

Maybe this helps: https://learn.saylor.org/mod/book/view.php?id=36341&chapterid=18919

Here’s how we use SIGHASH preimages in covenants: https://read.cash/@pein/bch-covenants-with-spedn-4a980ed3

And here my crazy ideas: https://tobiasruck.com/content/lets-play-chess-on-bch/

modules/ecash-lib/src/unsignedTx.test.ts
316 ↗(On Diff #47218)

Is this a spec behavior? Where is this defined?

modules/ecash-lib/src/unsignedTx.test.ts
316 ↗(On Diff #47218)

Yes, see here:

If sighash type is SINGLE and the input index is smaller than the number of outputs, hashOutputs is the double SHA256 of the output amount with scriptPubKey of the same index as the input;
Otherwise, hashOutputs is a uint256 of 0x0000......0000.

This revision is now accepted and ready to land.Thu, Apr 18, 22:53