Skip to content

Commit

Permalink
Add P2SH support for tagging addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
blondfrogs committed Jun 30, 2022
1 parent 0472cd6 commit 4258e6f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define OFFSET_THREE 3
#define OFFSET_FOUR 4
#define OFFSET_TWENTY_THREE 23
#define OFFSET_TWENTY_FOUR 24


std::map<uint256, std::string> mapReissuedTx;
Expand Down Expand Up @@ -857,8 +858,16 @@ bool AssetNullDataFromScript(const CScript& scriptPubKey, CNullAssetTxData& asse

strAddress = EncodeDestination(destination);

// Only used for P2PKH addresses
int offset = OFFSET_TWENTY_THREE;

// Only used for P2SH addresses
if (scriptPubKey[2] != OP_1NEGATE) {
offset = OFFSET_TWENTY_FOUR;
}

std::vector<unsigned char> vchAssetData;
vchAssetData.insert(vchAssetData.end(), scriptPubKey.begin() + OFFSET_TWENTY_THREE, scriptPubKey.end());
vchAssetData.insert(vchAssetData.end(), scriptPubKey.begin() + offset, scriptPubKey.end());
CDataStream ssData(vchAssetData, SER_NETWORK, PROTOCOL_VERSION);

try {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ UniValue listtagsforaddress(const JSONRPCRequest &request)

UniValue listaddressesfortag(const JSONRPCRequest& request)
{
if (request.fHelp || !AreRestrictedAssetsDeployed() || request.params.size() !=1)
if (request.fHelp || !AreRestrictedAssetsDeployed() || request.params.size() != 1)
throw std::runtime_error(
"listaddressesfortag tag_name\n"
+ RestrictedActivationWarning() +
Expand Down
9 changes: 8 additions & 1 deletion src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,16 @@ bool CScript::IsNullAsset() const

bool CScript::IsNullAssetTxDataScript() const
{
return (this->size() > 23 &&
bool p2pkh = (this->size() > 23 &&
(*this)[0] == OP_RVN_ASSET &&
(*this)[1] == 0x14);

bool p2sh = (this->size() > 23 &&
(*this)[0] == OP_RVN_ASSET &&
(*this)[1] == OP_1NEGATE &&
(*this)[2] == 0x14);

return p2sh || p2pkh;
}

bool CScript::IsNullGlobalRestrictionAssetTxDataScript() const
Expand Down
23 changes: 19 additions & 4 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,17 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, txnouttype& script
typeRet = TX_RESTRICTED_ASSET_DATA;

if (scriptPubKey.size() >= 23 && scriptPubKey[1] != OP_RESERVED) {
std::vector<unsigned char> hashBytes(scriptPubKey.begin() + 2, scriptPubKey.begin() + 22);
vSolutionsRet.push_back(hashBytes);
if (scriptPubKey[1] == OP_1NEGATE) {
// P2SH script
scriptTypeRet = TX_SCRIPTHASH;
std::vector<unsigned char> hashBytes(scriptPubKey.begin() + 3, scriptPubKey.begin() + 23);
vSolutionsRet.push_back(hashBytes);
} else {
// P2PKH script
scriptTypeRet = TX_PUBKEYHASH;
std::vector<unsigned char> hashBytes(scriptPubKey.begin() + 2, scriptPubKey.begin() + 22);
vSolutionsRet.push_back(hashBytes);
}
}
return true;
}
Expand Down Expand Up @@ -255,7 +264,11 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
return true;
} else if (whichType == TX_RESTRICTED_ASSET_DATA) {
if (vSolutions.size()) {
addressRet = CKeyID(uint160(vSolutions[0]));
if (scriptType == TX_SCRIPTHASH) {
addressRet = CScriptID(uint160(vSolutions[0]));
} else {
addressRet = CKeyID(uint160(vSolutions[0]));
}
return true;
}
}
Expand Down Expand Up @@ -355,7 +368,9 @@ namespace

bool operator()(const CScriptID &scriptID) const {
script->clear();
*script << OP_RVN_ASSET << ToByteVector(scriptID);
// OP1_NEGATE was chosen because it is a PUSH only value and it worked.
// You can use any PUSH value though
*script << OP_RVN_ASSET << OP_1NEGATE << ToByteVector(scriptID);
return true;
}
};
Expand Down

0 comments on commit 4258e6f

Please sign in to comment.