Skip to content
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

Strkey update #4195

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/crypto/KeyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ KeyUtils::getKeyVersionSize(strKey::StrKeyVersionByte keyVersion)
case strKey::STRKEY_PRE_AUTH_TX:
case strKey::STRKEY_HASH_X:
return 32U;
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
return 96U; // 32 bytes for the key and 64 bytes for the payload
default:
throw std::invalid_argument("invalid key version: " +
Expand Down
86 changes: 73 additions & 13 deletions src/crypto/SecretKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,44 +496,104 @@ logSecretKey(std::ostream& s, SecretKey const& sk)
void
StrKeyUtils::logKey(std::ostream& s, std::string const& key)
{
// if it's a hex string, display it in all forms

// see if it's a public key
try
{
uint256 data = hexToBin256(key);
PublicKey pk;
pk.type(PUBLIC_KEY_TYPE_ED25519);
pk.ed25519() = data;
PublicKey pk = KeyUtils::fromStrKey<PublicKey>(key);
logPublicKey(s, pk);

SecretKey sk(SecretKey::fromSeed(data));
logSecretKey(s, sk);
return;
}
catch (...)
{
}

// see if it's a public key
// see if it's a seed
try
{
PublicKey pk = KeyUtils::fromStrKey<PublicKey>(key);
logPublicKey(s, pk);
SecretKey sk = SecretKey::fromStrKeySeed(key);
logSecretKey(s, sk);
return;
}
catch (...)
{
}

// see if it's a seed
// if it's a hex string, display it in all forms
try
{
SecretKey sk = SecretKey::fromStrKeySeed(key);
uint256 data = hexToBin256(key);
PublicKey pk;
pk.type(PUBLIC_KEY_TYPE_ED25519);
pk.ed25519() = data;
s << "Interpreted as ";
logPublicKey(s, pk);

s << std::endl;
SecretKey sk(SecretKey::fromSeed(data));
s << "Interpreted as ";
logSecretKey(s, sk);

s << std::endl;
s << "Other interpretations:" << std::endl;
s << " STRKEY_PRE_AUTH_TX: "
<< strKey::toStrKey(strKey::STRKEY_PRE_AUTH_TX, data).value
<< std::endl;
s << " STRKEY_HASH_X: "
<< strKey::toStrKey(strKey::STRKEY_HASH_X, data).value << std::endl;
s << " STRKEY_SIGNED_PAYLOAD: "
<< strKey::toStrKey(strKey::STRKEY_SIGNED_PAYLOAD_ED25519, data).value
<< std::endl;
s << " STRKEY_MUXED_ACCOUNT_ED25519: "
<< strKey::toStrKey(strKey::STRKEY_MUXED_ACCOUNT_ED25519, data).value
<< std::endl;
s << " STRKEY_CONTRACT: "
<< strKey::toStrKey(strKey::STRKEY_CONTRACT, data).value << std::endl;
return;
}
catch (...)
{
}

// Try generic strkey decoding for other strkey types

uint8_t outVersion;
std::vector<uint8_t> decoded;
if (strKey::fromStrKey(key, outVersion, decoded))
{
s << "StrKey:" << std::endl;
switch (outVersion)
{
case strKey::STRKEY_PUBKEY_ED25519:
s << " type: STRKEY_PUBKEY_ED25519" << std::endl;
break;
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
s << " type: STRKEY_SIGNED_PAYLOAD_ED25519" << std::endl;
break;
case strKey::STRKEY_SEED_ED25519:
s << " type: STRKEY_SEED_ED25519" << std::endl;
break;
case strKey::STRKEY_PRE_AUTH_TX:
s << " type: STRKEY_PRE_AUTH_TX" << std::endl;
break;
case strKey::STRKEY_HASH_X:
s << " type: STRKEY_HASH_X" << std::endl;
break;
case strKey::STRKEY_MUXED_ACCOUNT_ED25519:
throw std::runtime_error(
"unexpected StrKey type STRKEY_MUXED_ACCOUNT_ED25519");
break;
case strKey::STRKEY_CONTRACT:
s << " type: STRKEY_CONTRACT" << std::endl;
break;
default:
s << " type: unknown" << std::endl;
break;
}
s << " hex: " << binToHex(decoded) << std::endl;
return;
}

s << "Unknown key type" << std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/SecretKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ PublicKey pseudoRandomForTesting();

namespace StrKeyUtils
{
// logs a key (can be a public or private key) in all
// logs a key (can be a strkey or raw hex string) in all
// known formats
void logKey(std::ostream& s, std::string const& key);
}
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/SignerKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ KeyFunctions<SignerKey>::getKeyVersionIsSupported(
return true;
case strKey::STRKEY_HASH_X:
return true;
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
return true;
default:
return false;
Expand All @@ -50,7 +50,7 @@ KeyFunctions<SignerKey>::getKeyVersionIsVariableLength(
return false;
case strKey::STRKEY_HASH_X:
return false;
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
return true;
default:
throw std::invalid_argument("invalid signer key type");
Expand All @@ -68,7 +68,7 @@ KeyFunctions<SignerKey>::toKeyType(strKey::StrKeyVersionByte keyVersion)
return SignerKeyType::SIGNER_KEY_TYPE_PRE_AUTH_TX;
case strKey::STRKEY_HASH_X:
return SignerKeyType::SIGNER_KEY_TYPE_HASH_X;
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
return SignerKeyType::SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD;
default:
throw std::invalid_argument("invalid signer key type");
Expand All @@ -87,7 +87,7 @@ KeyFunctions<SignerKey>::toKeyVersion(SignerKeyType keyType)
case SignerKeyType::SIGNER_KEY_TYPE_HASH_X:
return strKey::STRKEY_HASH_X;
case SignerKeyType::SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD:
return strKey::STRKEY_ED25519_SIGNED_PAYLOAD;
return strKey::STRKEY_SIGNED_PAYLOAD_ED25519;
default:
throw std::invalid_argument("invalid signer key type");
}
Expand Down
8 changes: 5 additions & 3 deletions src/crypto/StrKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ enum StrKeyVersionByte : uint8_t
{
// version bytes - 5 bits only
STRKEY_PUBKEY_ED25519 = 6, // 'G'
STRKEY_ED25519_SIGNED_PAYLOAD = 15, // 'P'
STRKEY_SIGNED_PAYLOAD_ED25519 = 15, // 'P'
STRKEY_SEED_ED25519 = 18, // 'S'
STRKEY_PRE_AUTH_TX = 19, // 'T',
STRKEY_HASH_X = 23 // 'X'
STRKEY_PRE_AUTH_TX = 19, // 'T'
STRKEY_HASH_X = 23, // 'X'
STRKEY_MUXED_ACCOUNT_ED25519 = 12, // 'M'
STRKEY_CONTRACT = 2, // 'C'
};

// Encode a version byte and ByteSlice into StrKey
Expand Down
55 changes: 11 additions & 44 deletions src/testdata/ledger-close-meta-v1-protocol-20-soroban.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@
"hostFunction": {
"type": "HOST_FUNCTION_TYPE_INVOKE_CONTRACT",
"invokeContract": {
"contractAddress": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contractAddress": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"functionName": "put_persistent",
"args": [
{
Expand Down Expand Up @@ -125,10 +122,7 @@
{
"type": "CONTRACT_DATA",
"contractData": {
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"key": {
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
},
Expand Down Expand Up @@ -179,10 +173,7 @@
"contractIDPreimage": {
"type": "CONTRACT_ID_PREIMAGE_FROM_ADDRESS",
"fromAddress": {
"address": {
"type": "SC_ADDRESS_TYPE_ACCOUNT",
"accountId": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU"
},
"address": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU",
"salt": "63479ad69a090b258277ec8fba6f99419a2ffb248981510657c944ccd1148e97"
}
},
Expand All @@ -204,10 +195,7 @@
"contractIDPreimage": {
"type": "CONTRACT_ID_PREIMAGE_FROM_ADDRESS",
"fromAddress": {
"address": {
"type": "SC_ADDRESS_TYPE_ACCOUNT",
"accountId": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU"
},
"address": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU",
"salt": "63479ad69a090b258277ec8fba6f99419a2ffb248981510657c944ccd1148e97"
}
},
Expand Down Expand Up @@ -245,10 +233,7 @@
{
"type": "CONTRACT_DATA",
"contractData": {
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "9ca961f94e83bd738223161350d67b21b0ac92f01478c057d5cbafab87186da5"
},
"contract": "CCOKSYPZJ2B3244CEMLBGUGWPMQ3BLES6AKHRQCX2XF27K4HDBW2LKDF",
"key": {
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
},
Expand Down Expand Up @@ -318,10 +303,7 @@
{
"type": "CONTRACT_DATA",
"contractData": {
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"key": {
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
},
Expand Down Expand Up @@ -386,10 +368,7 @@
{
"type": "CONTRACT_DATA",
"contractData": {
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"key": {
"type": "SCV_SYMBOL",
"sym": "archived"
Expand Down Expand Up @@ -437,10 +416,7 @@
"hostFunction": {
"type": "HOST_FUNCTION_TYPE_INVOKE_CONTRACT",
"invokeContract": {
"contractAddress": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contractAddress": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"functionName": "put_persistent",
"args": [
{
Expand Down Expand Up @@ -477,10 +453,7 @@
{
"type": "CONTRACT_DATA",
"contractData": {
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"key": {
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
},
Expand All @@ -492,10 +465,7 @@
{
"type": "CONTRACT_DATA",
"contractData": {
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"key": {
"type": "SCV_SYMBOL",
"sym": "key"
Expand Down Expand Up @@ -1580,10 +1550,7 @@
"ext": {
"v": 0
},
"contract": {
"type": "SC_ADDRESS_TYPE_CONTRACT",
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
},
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
"key": {
"type": "SCV_SYMBOL",
"sym": "key"
Expand Down
Loading
Loading