Skip to content

Commit 7635564

Browse files
authored
Merge pull request #4195 from stellar/strkey-update
Strkey update Reviewed-by: graydon
2 parents e4992c7 + ff88f97 commit 7635564

10 files changed

+160
-112
lines changed

src/crypto/KeyUtils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ KeyUtils::getKeyVersionSize(strKey::StrKeyVersionByte keyVersion)
2020
case strKey::STRKEY_PRE_AUTH_TX:
2121
case strKey::STRKEY_HASH_X:
2222
return 32U;
23-
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
23+
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
2424
return 96U; // 32 bytes for the key and 64 bytes for the payload
2525
default:
2626
throw std::invalid_argument("invalid key version: " +

src/crypto/SecretKey.cpp

+73-13
Original file line numberDiff line numberDiff line change
@@ -496,44 +496,104 @@ logSecretKey(std::ostream& s, SecretKey const& sk)
496496
void
497497
StrKeyUtils::logKey(std::ostream& s, std::string const& key)
498498
{
499-
// if it's a hex string, display it in all forms
499+
500+
// see if it's a public key
500501
try
501502
{
502-
uint256 data = hexToBin256(key);
503-
PublicKey pk;
504-
pk.type(PUBLIC_KEY_TYPE_ED25519);
505-
pk.ed25519() = data;
503+
PublicKey pk = KeyUtils::fromStrKey<PublicKey>(key);
506504
logPublicKey(s, pk);
507-
508-
SecretKey sk(SecretKey::fromSeed(data));
509-
logSecretKey(s, sk);
510505
return;
511506
}
512507
catch (...)
513508
{
514509
}
515510

516-
// see if it's a public key
511+
// see if it's a seed
517512
try
518513
{
519-
PublicKey pk = KeyUtils::fromStrKey<PublicKey>(key);
520-
logPublicKey(s, pk);
514+
SecretKey sk = SecretKey::fromStrKeySeed(key);
515+
logSecretKey(s, sk);
521516
return;
522517
}
523518
catch (...)
524519
{
525520
}
526521

527-
// see if it's a seed
522+
// if it's a hex string, display it in all forms
528523
try
529524
{
530-
SecretKey sk = SecretKey::fromStrKeySeed(key);
525+
uint256 data = hexToBin256(key);
526+
PublicKey pk;
527+
pk.type(PUBLIC_KEY_TYPE_ED25519);
528+
pk.ed25519() = data;
529+
s << "Interpreted as ";
530+
logPublicKey(s, pk);
531+
532+
s << std::endl;
533+
SecretKey sk(SecretKey::fromSeed(data));
534+
s << "Interpreted as ";
531535
logSecretKey(s, sk);
536+
537+
s << std::endl;
538+
s << "Other interpretations:" << std::endl;
539+
s << " STRKEY_PRE_AUTH_TX: "
540+
<< strKey::toStrKey(strKey::STRKEY_PRE_AUTH_TX, data).value
541+
<< std::endl;
542+
s << " STRKEY_HASH_X: "
543+
<< strKey::toStrKey(strKey::STRKEY_HASH_X, data).value << std::endl;
544+
s << " STRKEY_SIGNED_PAYLOAD: "
545+
<< strKey::toStrKey(strKey::STRKEY_SIGNED_PAYLOAD_ED25519, data).value
546+
<< std::endl;
547+
s << " STRKEY_MUXED_ACCOUNT_ED25519: "
548+
<< strKey::toStrKey(strKey::STRKEY_MUXED_ACCOUNT_ED25519, data).value
549+
<< std::endl;
550+
s << " STRKEY_CONTRACT: "
551+
<< strKey::toStrKey(strKey::STRKEY_CONTRACT, data).value << std::endl;
532552
return;
533553
}
534554
catch (...)
535555
{
536556
}
557+
558+
// Try generic strkey decoding for other strkey types
559+
560+
uint8_t outVersion;
561+
std::vector<uint8_t> decoded;
562+
if (strKey::fromStrKey(key, outVersion, decoded))
563+
{
564+
s << "StrKey:" << std::endl;
565+
switch (outVersion)
566+
{
567+
case strKey::STRKEY_PUBKEY_ED25519:
568+
s << " type: STRKEY_PUBKEY_ED25519" << std::endl;
569+
break;
570+
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
571+
s << " type: STRKEY_SIGNED_PAYLOAD_ED25519" << std::endl;
572+
break;
573+
case strKey::STRKEY_SEED_ED25519:
574+
s << " type: STRKEY_SEED_ED25519" << std::endl;
575+
break;
576+
case strKey::STRKEY_PRE_AUTH_TX:
577+
s << " type: STRKEY_PRE_AUTH_TX" << std::endl;
578+
break;
579+
case strKey::STRKEY_HASH_X:
580+
s << " type: STRKEY_HASH_X" << std::endl;
581+
break;
582+
case strKey::STRKEY_MUXED_ACCOUNT_ED25519:
583+
throw std::runtime_error(
584+
"unexpected StrKey type STRKEY_MUXED_ACCOUNT_ED25519");
585+
break;
586+
case strKey::STRKEY_CONTRACT:
587+
s << " type: STRKEY_CONTRACT" << std::endl;
588+
break;
589+
default:
590+
s << " type: unknown" << std::endl;
591+
break;
592+
}
593+
s << " hex: " << binToHex(decoded) << std::endl;
594+
return;
595+
}
596+
537597
s << "Unknown key type" << std::endl;
538598
}
539599

src/crypto/SecretKey.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ PublicKey pseudoRandomForTesting();
150150

151151
namespace StrKeyUtils
152152
{
153-
// logs a key (can be a public or private key) in all
153+
// logs a key (can be a strkey or raw hex string) in all
154154
// known formats
155155
void logKey(std::ostream& s, std::string const& key);
156156
}

src/crypto/SignerKey.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ KeyFunctions<SignerKey>::getKeyVersionIsSupported(
3131
return true;
3232
case strKey::STRKEY_HASH_X:
3333
return true;
34-
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
34+
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
3535
return true;
3636
default:
3737
return false;
@@ -50,7 +50,7 @@ KeyFunctions<SignerKey>::getKeyVersionIsVariableLength(
5050
return false;
5151
case strKey::STRKEY_HASH_X:
5252
return false;
53-
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
53+
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
5454
return true;
5555
default:
5656
throw std::invalid_argument("invalid signer key type");
@@ -68,7 +68,7 @@ KeyFunctions<SignerKey>::toKeyType(strKey::StrKeyVersionByte keyVersion)
6868
return SignerKeyType::SIGNER_KEY_TYPE_PRE_AUTH_TX;
6969
case strKey::STRKEY_HASH_X:
7070
return SignerKeyType::SIGNER_KEY_TYPE_HASH_X;
71-
case strKey::STRKEY_ED25519_SIGNED_PAYLOAD:
71+
case strKey::STRKEY_SIGNED_PAYLOAD_ED25519:
7272
return SignerKeyType::SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD;
7373
default:
7474
throw std::invalid_argument("invalid signer key type");
@@ -87,7 +87,7 @@ KeyFunctions<SignerKey>::toKeyVersion(SignerKeyType keyType)
8787
case SignerKeyType::SIGNER_KEY_TYPE_HASH_X:
8888
return strKey::STRKEY_HASH_X;
8989
case SignerKeyType::SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD:
90-
return strKey::STRKEY_ED25519_SIGNED_PAYLOAD;
90+
return strKey::STRKEY_SIGNED_PAYLOAD_ED25519;
9191
default:
9292
throw std::invalid_argument("invalid signer key type");
9393
}

src/crypto/StrKey.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ enum StrKeyVersionByte : uint8_t
1818
{
1919
// version bytes - 5 bits only
2020
STRKEY_PUBKEY_ED25519 = 6, // 'G'
21-
STRKEY_ED25519_SIGNED_PAYLOAD = 15, // 'P'
21+
STRKEY_SIGNED_PAYLOAD_ED25519 = 15, // 'P'
2222
STRKEY_SEED_ED25519 = 18, // 'S'
23-
STRKEY_PRE_AUTH_TX = 19, // 'T',
24-
STRKEY_HASH_X = 23 // 'X'
23+
STRKEY_PRE_AUTH_TX = 19, // 'T'
24+
STRKEY_HASH_X = 23, // 'X'
25+
STRKEY_MUXED_ACCOUNT_ED25519 = 12, // 'M'
26+
STRKEY_CONTRACT = 2, // 'C'
2527
};
2628

2729
// Encode a version byte and ByteSlice into StrKey

src/testdata/ledger-close-meta-v1-protocol-20-soroban.json

+11-44
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@
8585
"hostFunction": {
8686
"type": "HOST_FUNCTION_TYPE_INVOKE_CONTRACT",
8787
"invokeContract": {
88-
"contractAddress": {
89-
"type": "SC_ADDRESS_TYPE_CONTRACT",
90-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
91-
},
88+
"contractAddress": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
9289
"functionName": "put_persistent",
9390
"args": [
9491
{
@@ -125,10 +122,7 @@
125122
{
126123
"type": "CONTRACT_DATA",
127124
"contractData": {
128-
"contract": {
129-
"type": "SC_ADDRESS_TYPE_CONTRACT",
130-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
131-
},
125+
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
132126
"key": {
133127
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
134128
},
@@ -179,10 +173,7 @@
179173
"contractIDPreimage": {
180174
"type": "CONTRACT_ID_PREIMAGE_FROM_ADDRESS",
181175
"fromAddress": {
182-
"address": {
183-
"type": "SC_ADDRESS_TYPE_ACCOUNT",
184-
"accountId": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU"
185-
},
176+
"address": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU",
186177
"salt": "63479ad69a090b258277ec8fba6f99419a2ffb248981510657c944ccd1148e97"
187178
}
188179
},
@@ -204,10 +195,7 @@
204195
"contractIDPreimage": {
205196
"type": "CONTRACT_ID_PREIMAGE_FROM_ADDRESS",
206197
"fromAddress": {
207-
"address": {
208-
"type": "SC_ADDRESS_TYPE_ACCOUNT",
209-
"accountId": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU"
210-
},
198+
"address": "GA2NXNEE2MHWGQP5XXACPYG2BDZFPKGYPFNST5V3ZZN75NSLQAEXX7CU",
211199
"salt": "63479ad69a090b258277ec8fba6f99419a2ffb248981510657c944ccd1148e97"
212200
}
213201
},
@@ -245,10 +233,7 @@
245233
{
246234
"type": "CONTRACT_DATA",
247235
"contractData": {
248-
"contract": {
249-
"type": "SC_ADDRESS_TYPE_CONTRACT",
250-
"contractId": "9ca961f94e83bd738223161350d67b21b0ac92f01478c057d5cbafab87186da5"
251-
},
236+
"contract": "CCOKSYPZJ2B3244CEMLBGUGWPMQ3BLES6AKHRQCX2XF27K4HDBW2LKDF",
252237
"key": {
253238
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
254239
},
@@ -318,10 +303,7 @@
318303
{
319304
"type": "CONTRACT_DATA",
320305
"contractData": {
321-
"contract": {
322-
"type": "SC_ADDRESS_TYPE_CONTRACT",
323-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
324-
},
306+
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
325307
"key": {
326308
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
327309
},
@@ -386,10 +368,7 @@
386368
{
387369
"type": "CONTRACT_DATA",
388370
"contractData": {
389-
"contract": {
390-
"type": "SC_ADDRESS_TYPE_CONTRACT",
391-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
392-
},
371+
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
393372
"key": {
394373
"type": "SCV_SYMBOL",
395374
"sym": "archived"
@@ -437,10 +416,7 @@
437416
"hostFunction": {
438417
"type": "HOST_FUNCTION_TYPE_INVOKE_CONTRACT",
439418
"invokeContract": {
440-
"contractAddress": {
441-
"type": "SC_ADDRESS_TYPE_CONTRACT",
442-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
443-
},
419+
"contractAddress": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
444420
"functionName": "put_persistent",
445421
"args": [
446422
{
@@ -477,10 +453,7 @@
477453
{
478454
"type": "CONTRACT_DATA",
479455
"contractData": {
480-
"contract": {
481-
"type": "SC_ADDRESS_TYPE_CONTRACT",
482-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
483-
},
456+
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
484457
"key": {
485458
"type": "SCV_LEDGER_KEY_CONTRACT_INSTANCE"
486459
},
@@ -492,10 +465,7 @@
492465
{
493466
"type": "CONTRACT_DATA",
494467
"contractData": {
495-
"contract": {
496-
"type": "SC_ADDRESS_TYPE_CONTRACT",
497-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
498-
},
468+
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
499469
"key": {
500470
"type": "SCV_SYMBOL",
501471
"sym": "key"
@@ -1580,10 +1550,7 @@
15801550
"ext": {
15811551
"v": 0
15821552
},
1583-
"contract": {
1584-
"type": "SC_ADDRESS_TYPE_CONTRACT",
1585-
"contractId": "01b8290fd49b5bd1330f0ea718bdea6729320620f65ce1a763657c57638c580b"
1586-
},
1553+
"contract": "CAA3QKIP2SNVXUJTB4HKOGF55JTSSMQGED3FZYNHMNSXYV3DRRMAWA3Y",
15871554
"key": {
15881555
"type": "SCV_SYMBOL",
15891556
"sym": "key"

0 commit comments

Comments
 (0)