RGB-WG / rgb-std

RGB standard libs for WASM & low-level integrations (no FS/networking)
https://rgb.tech
Apache License 2.0
66 stars 30 forks source link

fix bug: Pay2Vout's to_baid64_payload and try_from #210

Closed eauxxs closed 4 months ago

codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 17.1%. Comparing base (60e86ca) to head (34792e5). Report is 3 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #210 +/- ## ======================================== + Coverage 16.3% 17.1% +0.8% ======================================== Files 37 37 Lines 7296 7334 +38 ======================================== + Hits 1190 1257 +67 + Misses 6106 6077 -29 ``` | [Flag](https://app.codecov.io/gh/RGB-WG/rgb-std/pull/210/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=RGB-WG) | Coverage Δ | | |---|---|---| | [rust](https://app.codecov.io/gh/RGB-WG/rgb-std/pull/210/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=RGB-WG) | `17.1% <100.0%> (+0.8%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=RGB-WG#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

eauxxs commented 4 months ago

example:

wpkh script: 00148833bd1dc92076f2ad60c4061c57667d8a62657d
// origin
&origin = Pay2Vout {
    method: OpretFirst,
    address: Wpkh(
        WPubkeyHash(
            Array<20>(8833bd1dc92076f2ad60c4061c57667d8a62657d),
        ),
    ),
}
// tmp = Pay2Vout::from(&origin.to_string())
&tmp = Pay2Vout {
    method: OpretFirst,
    address: Wpkh(
        WPubkeyHash(
            Array<20>(00148833bd1dc92076f2ad60c4061c57667d8a62),
        ),
    ),
eauxxs commented 4 months ago

@zoedberg thanks for your review. By the way, I have adapted the latest rgb beta6 for rgb-lib, and it has passed all tests after this pr. So if you need it, I can try to raise a PR, and hope to reduce your work.

zoedberg commented 4 months ago

@zoedberg thanks for your review. By the way, I have adapted the latest rgb beta6 for rgb-lib, and it has passed all tests after this pr. So if you need it, I can try to raise a PR, and hope to reduce your work.

Thanks for the offer but I've already updated rgb-lib to the beta 6 (as told you here https://github.com/RGB-Tools/rgb-lib/issues/48). Anyway, we'll not publish that commit since the tests with witness transfers are not working. We'll wait for beta 7

eauxxs commented 4 months ago

@dr-orlovsky Because address.script_pubkey() does not return only the payload as you think, ScriptPubkey::as_slice contains prefixes, such as OP_0 and OP_PUSHBYTE_20.

https://github.com/BP-WG/bp-std/blob/8177aab7c0badf5295b6b9664d2f652a94f7093e/invoice/src/address.rs#L346-L352

So we should use the match method to extract the payload inside. Because of the special nature of the tr address, its processing should apply for a 32-byte memory in the stack to store the payload, and cannot directly as_ref<[u8]>.

dr-orlovsky commented 4 months ago

@eauxxs can you please rebase this on top of the master which now contains the length and offset fixes - to see what specifically we need to fix in order to get P2TR working

eauxxs commented 4 months ago

@dr-orlovsky Sorry, I am very confused. This fix for pay2vout conflicts with #214. I think #214 is wrong, I don't know why you would merge that PR after I explained the main error to you.

here is test code, you can run it:

#[test]
fn pay2vout_parse() {
    use crate::AddressPayload;
    let p = Pay2Vout {
        method: bp::dbc::Method::OpretFirst,
        address: AddressPayload::Pkh([0xff; 20].into()),
    };
    assert_eq!(Pay2Vout::from_str(&p.to_string()).unwrap(), p);
}
---- parse::test::pay2vout_parse stdout ----
thread 'parse::test::pay2vout_parse' panicked at invoice/src/parse.rs:757:9:
assertion `left == right` failed
  left: Pay2Vout { method: OpretFirst, address: Pkh(PubkeyHash(Array<20>(14ffffffffffffffffffffffffffffffffffffff))) }
 right: Pay2Vout { method: OpretFirst, address: Pkh(PubkeyHash(Array<20>(ffffffffffffffffffffffffffffffffffffffff))) }

https://github.com/RGB-WG/rgb-std/blob/6874b25273c5761275ddce37428e6e9b5972e960/invoice/src/parse.rs#L256-L257

If you must use self.address.script_pubkey().as_slice(), for p2pkh addresses, you should skip the first 3 bytes, because the prefix of p2pkh is OP_DUP OP_Hash160 OP_PUSHBYTES_20.

eauxxs commented 4 months ago

@dr-orlovsky My pleasure. Doctor. I love rgb and hope it better 💕