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

AND-8877 Add KRC20 base support #850

Merged
merged 2 commits into from
Dec 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import com.tangem.blockchain.common.datastorage.BlockchainDataStorage
internal object DummyBlockchainDataStorage : BlockchainDataStorage {
override suspend fun getOrNull(key: String): String? = null
override suspend fun store(key: String, value: String) = Unit
override suspend fun remove(key: String) = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal class HederaWalletManager(
if (error is BlockchainSdkError) error("Error isn't BlockchainSdkError")
}

override suspend fun hasRequirements(currencyType: CryptoCurrencyType): Boolean {
private suspend fun assetRequiresAssociation(currencyType: CryptoCurrencyType): Boolean {
return when (currencyType) {
is CryptoCurrencyType.Coin -> false
is CryptoCurrencyType.Token -> {
Expand All @@ -99,7 +99,7 @@ internal class HederaWalletManager(
}

override suspend fun requirementsCondition(currencyType: CryptoCurrencyType): AssetRequirementsCondition? {
if (!hasRequirements(currencyType)) return null
if (!assetRequiresAssociation(currencyType)) return null

return when (currencyType) {
is CryptoCurrencyType.Coin -> null
Expand All @@ -110,7 +110,10 @@ internal class HederaWalletManager(
} else {
val feeValue = exchangeRate * HBAR_TOKEN_ASSOCIATE_USD_COST
val feeAmount = Amount(blockchain = wallet.blockchain, value = feeValue)
AssetRequirementsCondition.PaidTransactionWithFee(feeAmount)
AssetRequirementsCondition.PaidTransactionWithFee(
blockchain = blockchain,
feeAmount = feeAmount,
)
}
}
}
Expand All @@ -120,7 +123,7 @@ internal class HederaWalletManager(
currencyType: CryptoCurrencyType,
signer: TransactionSigner,
): SimpleResult {
if (!hasRequirements(currencyType)) return SimpleResult.Success
if (!assetRequiresAssociation(currencyType)) return SimpleResult.Success

return when (currencyType) {
is CryptoCurrencyType.Coin -> SimpleResult.Success
Expand Down Expand Up @@ -155,6 +158,10 @@ internal class HederaWalletManager(
}
}

override suspend fun discardRequirements(currencyType: CryptoCurrencyType): SimpleResult {
return SimpleResult.Success
}

override suspend fun send(
transactionData: TransactionData,
signer: TransactionSigner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// based on BitcoinCashTransaction
public class KaspaTransaction extends Transaction {
private final byte[] TRANSACTION_SIGNING_DOMAIN = "TransactionSigningHash".getBytes(StandardCharsets.UTF_8);
private final byte[] TRANSACTION_ID = "TransactionID".getBytes(StandardCharsets.UTF_8);
private final byte[] TRANSACTION_SIGNING_ECDSA_DOMAIN_HASH =
Sha256Hash.of("TransactionSigningHashECDSA".getBytes(StandardCharsets.UTF_8)).getBytes();
private final int BLAKE2B_DIGEST_LENGTH = 32;
Expand Down Expand Up @@ -130,6 +131,41 @@ public synchronized byte[] hashForSignatureWitness(
return Sha256Hash.of(finalBos.toByteArray()).getBytes();
}

public synchronized byte[] transactionHash() {
ByteArrayOutputStream bos = new ByteArrayOutputStream(256);
try {
List<TransactionInput> inputs = getInputs();
List<TransactionOutput> outputs = getOutputs();

uint16ToByteStreamLE(0, bos);

uint64ToByteStreamLE(BigInteger.valueOf(inputs.size()), bos);
for (TransactionInput input: inputs) {
bos.write(input.getOutpoint().getHash().getBytes());
uint32ToByteStreamLE(input.getOutpoint().getIndex(), bos);
uint64ToByteStreamLE(BigInteger.valueOf(0), bos);
uint64ToByteStreamLE(BigInteger.valueOf(0), bos);
}
uint64ToByteStreamLE(BigInteger.valueOf(outputs.size()), bos);
for (TransactionOutput output : outputs) {
byte[] scriptBytes = output.getScriptBytes();
uint64ToByteStreamLE(BigInteger.valueOf(output.getValue().value), bos);
uint16ToByteStreamLE(0, bos); // version
uint64ToByteStreamLE(BigInteger.valueOf(scriptBytes.length), bos);
bos.write(scriptBytes);
}
uint64ToByteStreamLE(BigInteger.valueOf(getLockTime()), bos); // lock time
bos.write(new byte[20]); // subnetwork id
uint64ToByteStreamLE(BigInteger.valueOf(0), bos); // gas
uint64ToByteStreamLE(BigInteger.valueOf(0), bos); // payload size
} catch (IOException e) {
throw new RuntimeException(e);
}

Blake2b.Mac digest = Blake2b.Mac.newInstance(TRANSACTION_ID, BLAKE2B_DIGEST_LENGTH);
return digest.digest(bos.toByteArray());
}

private byte[] blake2bDigestOf(byte[] input) {
Blake2b.Mac digest = Blake2b.Mac.newInstance(TRANSACTION_SIGNING_DOMAIN, BLAKE2B_DIGEST_LENGTH);
return digest.digest(input);
Expand Down
Loading
Loading