Skip to content

Commit

Permalink
AND-8877 Add KRC20 base support
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeeei committed Dec 3, 2024
1 parent 59b049b commit 465cabc
Show file tree
Hide file tree
Showing 29 changed files with 970 additions and 118 deletions.
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
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

0 comments on commit 465cabc

Please sign in to comment.