Skip to content

Commit fb2f5fa

Browse files
authored
Adds ability to specify the curve while constructing Private Key (#4324)
* Adds ability to specify the curve while constructing Private Key * Adds signing functions without a curve * Migrates to new API * Use TWCoinTypeCurve * Adds Curve
1 parent 2c1e0fe commit fb2f5fa

File tree

99 files changed

+426
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+426
-284
lines changed

src/Aeternity/Signer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using namespace TW;
1515
namespace TW::Aeternity {
1616

1717
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
18-
auto privateKey = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
18+
auto privateKey = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), TWCurveED25519);
1919
std::string sender_id = input.from_address();
2020
std::string recipient_id = input.to_address();
2121
std::string payload = input.payload();
@@ -34,7 +34,7 @@ Proto::SigningOutput Signer::sign(const TW::PrivateKey& privateKey, Transaction&
3434
auto msg = buildMessageToSign(txRlp);
3535

3636
/// sign ed25519
37-
auto sigRaw = privateKey.sign(msg, TWCurveED25519);
37+
auto sigRaw = privateKey.sign(msg);
3838
auto signature = Identifiers::prefixSignature + Base58::encodeCheck(sigRaw);
3939

4040
/// encode the message using rlp

src/Aion/Signer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using namespace TW;
99
namespace TW::Aion {
1010

1111
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
12-
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
12+
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), TWCurveED25519);
1313
auto transaction = Signer::buildTransaction(input);
1414
Signer::sign(key, transaction);
1515

@@ -23,7 +23,7 @@ Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
2323
void Signer::sign(const PrivateKey& privateKey, Transaction& transaction) noexcept {
2424
auto encoded = transaction.encode();
2525
auto hashData = Hash::blake2b(encoded, 32);
26-
auto hashSignature = privateKey.sign(hashData, TWCurveED25519);
26+
auto hashSignature = privateKey.sign(hashData);
2727
auto publicKeyData = privateKey.getPublicKey(TWPublicKeyTypeED25519).bytes;
2828

2929
// Aion signature = pubKeyBytes + signatureBytes

src/Algorand/Signer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const std::string ASSET_TRANSACTION = "axfer";
1818

1919
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
2020
auto protoOutput = Proto::SigningOutput();
21-
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
21+
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), TWCurveED25519);
2222
auto pubkey = key.getPublicKey(TWPublicKeyTypeED25519);
2323

2424
auto preImageData = Signer::preImage(pubkey, input);
25-
auto signature = key.sign(preImageData, TWCurveED25519);
25+
auto signature = key.sign(preImageData);
2626
return Signer::encodeTransaction(signature, pubkey, input);
2727
}
2828

@@ -37,7 +37,7 @@ Data Signer::sign(const PrivateKey& privateKey, const BaseTransaction& transacti
3737
Data data;
3838
append(data, TRANSACTION_TAG);
3939
append(data, transaction.serialize());
40-
auto signature = privateKey.sign(data, TWCurveED25519);
40+
auto signature = privateKey.sign(data);
4141
return {signature.begin(), signature.end()};
4242
}
4343

src/Bitcoin/SigningInput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SigningInput::SigningInput(const Proto::SigningInput& input) {
1717
toAddress = input.to_address();
1818
changeAddress = input.change_address();
1919
for (auto&& key : input.private_key()) {
20-
privateKeys.emplace_back(key);
20+
privateKeys.emplace_back(key, TWCurveSECP256k1);
2121
}
2222
for (auto&& script : input.scripts()) {
2323
scripts[script.first] = Script(script.second.begin(), script.second.end());

src/Cardano/Signer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Common::Proto::SigningError Signer::assembleSignatures(std::vector<std::pair<Dat
129129
}
130130

131131
// Add this private key and associated address
132-
const auto privateKey = PrivateKey(privateKeyData);
132+
const auto privateKey = PrivateKey(privateKeyData, TWCurveED25519ExtendedCardano);
133133
const auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeED25519Cardano);
134134
const auto address = AddressV3(publicKey);
135135
privateKeys[address.string()] = privateKeyData;
@@ -190,9 +190,9 @@ Common::Proto::SigningError Signer::assembleSignatures(std::vector<std::pair<Dat
190190
return Common::Proto::Error_missing_private_key;
191191
}
192192
}
193-
const auto privateKey = PrivateKey(privateKeyData);
193+
const auto privateKey = PrivateKey(privateKeyData, TWCurveED25519ExtendedCardano);
194194
const auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeED25519Cardano);
195-
const auto signature = privateKey.sign(txId, TWCurveED25519ExtendedCardano);
195+
const auto signature = privateKey.sign(txId);
196196
signatures.emplace_back(publicKey.bytes, signature);
197197
}
198198

src/Decred/Signer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Result<std::vector<Data>, Common::Proto::SigningError> Signer::signStep(Bitcoin:
181181
return Result<std::vector<Data>, Common::Proto::SigningError>::failure(Common::Proto::Error_missing_private_key);
182182
}
183183
} else {
184-
pubkey = PrivateKey(key).getPublicKey(TWPublicKeyTypeSECP256k1).bytes;
184+
pubkey = PrivateKey(key, TWCurveSECP256k1).getPublicKey(TWPublicKeyTypeSECP256k1).bytes;
185185
}
186186

187187
auto signature = createSignature(transactionToSign, script, key, data, index);
@@ -263,7 +263,7 @@ Data Signer::createSignature(const Transaction& transaction, const Bitcoin::Scri
263263
return externalSignature;
264264
}
265265

266-
auto pk = PrivateKey(key);
266+
auto pk = PrivateKey(key, TWCurveSECP256k1);
267267
auto signature = pk.signAsDER(Data(begin(sighash), end(sighash)));
268268
if (script.empty()) {
269269
return {};
@@ -275,7 +275,7 @@ Data Signer::createSignature(const Transaction& transaction, const Bitcoin::Scri
275275

276276
Data Signer::keyForPublicKeyHash(const Data& hash) const {
277277
for (auto& key : input.private_key()) {
278-
auto publicKey = PrivateKey(key).getPublicKey(TWPublicKeyTypeSECP256k1);
278+
auto publicKey = PrivateKey(key, TWCurveSECP256k1).getPublicKey(TWPublicKeyTypeSECP256k1);
279279
auto keyHash = TW::Hash::ripemd(TW::Hash::blake256(publicKey.bytes));
280280
if (std::equal(std::begin(keyHash), std::end(keyHash), std::begin(hash), std::end(hash))) {
281281
return Data(key.begin(), key.end());

src/EOS/Signer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
1717
auto signer = Signer(chainId);
1818
auto tx = signer.buildTx(input);
1919

20+
// values for Legacy and ModernK1
21+
TWCurve curve = TWCurveSECP256k1;
2022
// get key type
2123
EOS::Type type = Type::Legacy;
2224
switch (input.private_key_type()) {
@@ -29,14 +31,15 @@ Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
2931
break;
3032

3133
case Proto::KeyType::MODERNR1:
34+
curve = TWCurveNIST256p1;
3235
type = Type::ModernR1;
3336
break;
3437
default:
3538
break;
3639
}
3740

3841
// sign the transaction with a Signer
39-
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
42+
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), curve);
4043
signer.sign(key, type, tx);
4144

4245
// Pack the transaction and add the json encoding to Signing outputput
@@ -55,17 +58,14 @@ void Signer::sign(const PrivateKey& privateKey, Type type, Transaction& transact
5558
throw std::invalid_argument("Invalid transaction!");
5659
}
5760

58-
// values for Legacy and ModernK1
59-
TWCurve curve = TWCurveSECP256k1;
6061
auto canonicalChecker = isCanonical;
6162

6263
// Values for ModernR1
6364
if (type == Type::ModernR1) {
64-
curve = TWCurveNIST256p1;
6565
canonicalChecker = nullptr;
6666
}
6767

68-
const Data result = privateKey.sign(hash(transaction), curve, canonicalChecker);
68+
const Data result = privateKey.sign(hash(transaction), canonicalChecker);
6969

7070
transaction.signatures.emplace_back(Signature(result, type));
7171
}

src/Ethereum/Barz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ Data getAuthorizationHash(const Data& chainId, const std::string& contractAddres
243243

244244
std::vector<Data> getRSVY(const Data& hash, const std::string& privateKey) {
245245
auto privateKeyData = parse_hex(privateKey);
246-
auto privateKeyObj = PrivateKey(privateKeyData);
247-
auto signature = privateKeyObj.sign(hash, TWCurveSECP256k1);
246+
auto privateKeyObj = PrivateKey(privateKeyData, TWCurveSECP256k1);
247+
auto signature = privateKeyObj.sign(hash);
248248
if (signature.empty()) {
249249
return {};
250250
}

src/Everscale/Signer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace TW::Everscale {
1313

1414
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
15-
auto key = PrivateKey(input.private_key());
15+
auto key = PrivateKey(input.private_key(), TWCurveED25519);
1616
auto publicKey = key.getPublicKey(TWPublicKeyTypeED25519);
1717

1818
auto protoOutput = Proto::SigningOutput();

src/FIO/TransactionBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ string TransactionBuilder::actionName(const Proto::SigningInput& input) {
7070
}
7171

7272
string TransactionBuilder::sign(Proto::SigningInput in) {
73-
PrivateKey privateKey(in.private_key());
73+
PrivateKey privateKey(in.private_key(), TWCurveSECP256k1);
7474
PublicKey publicKey = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1);
7575
Address owner(publicKey);
7676

0 commit comments

Comments
 (0)