-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
address: add toScript method and expose to api #269
Conversation
This converts the Address into a Script. This same functionality occurs in Script.exeucte, as the Address.hash needs to be templated into a Bitcoin Script to run through the interpreter. In reality, Address.hash does not need to be a hash. To know what the script will be, the size of Address.hash must be observed. In the case of 20 bytes, it is known to be a pay to pubkey hash, which can be created with Script.fromPubkeyHash. If the Address.hash is 32 bytes, the witness must be present to build the script correctly. In the case of Address.version === 31, it is an OP_RETURN script. Unknown Address types will return `null`.
Seeing an address alone isn't particularly helpful in understanding what it does unless you can tell the size difference between the pay to pubkeyhash and pay to script hash. This will be useful for block explorers.
Codecov Report
@@ Coverage Diff @@
## master #269 +/- ##
=========================================
+ Coverage 53.12% 53.22% +0.1%
=========================================
Files 129 129
Lines 35751 35776 +25
Branches 6023 6035 +12
=========================================
+ Hits 18993 19043 +50
+ Misses 16758 16733 -25
Continue to review full report at Codecov.
|
See example
Looking for acks |
Updated with tests |
I think this may be more problematic than its worth.
I think this is a pretty good sign that this method doesn't belong in |
There is no good way to distinguish what actual bitcoin script will be executed without knowing the protocol. People are not going to know to look at line 2200+ in I also updated what you quoted above, as that applies in the p2wsh case. In the pay to pubkey hash case, you can always get back the correct script. |
Actually, probably a more important point is that when spending from a witness pubkey hash output - there is no script at all. |
The For outputs, It is not guaranteed that you will have the program in this case, depending on the type of UTXO it is. If it is pay to pubkey hash, then you do have the program, it is simply filling a template. If it is a pay to script hash, you will likely not have the script. For a project like Bitcoin, you generally want to keep you script preimages hidden because you want all BTC to be fungible. I do not think that Handshake is money, so I think that in some cases it is ok to reveal your script preimages. Based on a counterparty learning the script and then hashing it and checking it against the hash encoded in the address, (which should be accessible as a hash and not just as an address because that is an additional encoding/decoding step which is annoying) they can determine their level of trust when transacting with you. This is how the non interactive nameswap scheme works - the counterparty must know that the name holder cannot back out of the deal by spending to a |
@@ -192,6 +194,38 @@ class Address extends bio.Struct { | |||
return bech32.encode(hrp, version, hash); | |||
} | |||
|
|||
/** | |||
* Create Script from Address. | |||
* @param {Witness} witness |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This argument is optional {Witness?}
An address is a commitment to some program that will run in the Script interpreter. Every address has a mapping to a program. Depending on the type of address, it may or may not need an opening to the commitment. For example, p2pkh does not need an opening while p2sh does (the program preimage).
The abstract function that maps an address to a program accepts a version, data (hash in this codebase) and an opening to the commitment (top of Witness stack in this codebase). I find reasoning about addresses in this way to be useful. The
hsd
codebase calls thingsScript
when in reality I think they should be calledProgram
. There is a lot of confusion between what is a script/program/redeem script/witness etc.This PR addresses this mapping by:
toScript
method to theAddress
class that encompasses the functionality of converting an address to a script that can be ran in the Script interpreter. See codepath mentioned above here:hsd/lib/script/script.js
Line 2262 in 7c17227
This method must accept the witness as an argument in the case of pay to witness script hash, as it is impossible to know the program that will be ran without the preimage. In the case of pay to pubkey hash, you do not need the witness.
For each of
Coin
,Input
andOutput
thegetJSON
method additionally rendersprogram
andasm
. The program is theaddress.toScript
where the program is the raw hex bytecode of the program and the asm is the assembly printed for human consumption.This will allow block explorers to better visualize the locking scripts of addresses. It will also allow block explorers to display the redeem script (program preimage) when p2sh outputs are spent. @kilpatty