Skip to content

Commit

Permalink
V3.2.0
Browse files Browse the repository at this point in the history
Introducing a Transaction Builder for Cardano: Simplify Transaction Creation and Signing
  • Loading branch information
mrtnetwork committed Apr 29, 2024
1 parent 93fa50f commit 201f5ac
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.2.0

- Introducing a Transaction Builder for Cardano: Simplify Transaction Creation and Signing.

## 3.1.0
- Update dependencies.
- Constructors for Tron Native Contracts for Deserialization from Protobuf
Expand Down
2 changes: 1 addition & 1 deletion example/lib/example/cardano/pool_registration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void main() async {
projectId: "preprodMVwzqm4PuBDBSfEULoMzoj5QZcy5o3z5"));
final parameters =
await provider.request(BlockfrostRequestLatestEpochProtocolParameters());
final poolDeposit = BigInt.parse(parameters.poolDeposit);
final poolDeposit = parameters.poolDeposit;
final input = TransactionInput(
transactionId: TransactionHash.fromHex(
"afa75f7ab3e172f8772178766be3830bbf659eee5c4f00fd3966e2c40b3d2d91"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class QuickWalletForTest {

Future<String> faucent([SolAddress? addr]) async {
final f = await rpc.request(SolanaRPCRequestAirdrop(
account: addr ?? address, lamports: 2000000000));
account: addr ?? address, lamports: 1000000000));
return f;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:example/example/solana/quick_wallet_for_testing/quick_wallet.dart';
import 'package:on_chain/solana/solana.dart';

void main() async {
final wallet = QuickWalletForTest(index: 1100);
final mintAccount = QuickWalletForTest(index: 1111);

final associatedTokenAccount =
AssociatedTokenAccountProgramUtils.associatedTokenAccount(
mint: mintAccount.address, owner: wallet.address);

final createAssociatedTokenAccount =
AssociatedTokenAccountProgram.associatedTokenAccount(
payer: wallet.address,
associatedToken: associatedTokenAccount.address,
owner: wallet.address,
mint: mintAccount.address);

final tr = SolanaTransaction(
payerKey: wallet.address,
instructions: [createAssociatedTokenAccount],
recentBlockhash: await wallet.recentBlockhash());
tr.sign([wallet.privateKey]);
await wallet.submitTr(tr.serializeString());

/// https://explorer.solana.com/tx/BTdjhKHYKcKFSNdxQXG617JUXbMGnZs54aWZyKGsiPTh6Z6f8Qe1WMZQGD7BbBjfrFrjEmdU8HyQQ9NRQmZHWUP?cluster=testnet
}
33 changes: 33 additions & 0 deletions example/lib/example/solana/token_program/create_mint_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:example/example/solana/quick_wallet_for_testing/quick_wallet.dart';
import 'package:on_chain/solana/solana.dart';

void main() async {
final wallet = QuickWalletForTest(index: 1100);
final freezeAuthority = QuickWalletForTest(index: 1112);
final mintAccount = QuickWalletForTest(index: 1111);
final mintAccSpace = SolanaMintAccount.size;
final rent = await QuickWalletForTest.rpc
.request(SolanaRPCGetMinimumBalanceForRentExemption(size: mintAccSpace));
final createAccount = SystemProgram.createAccount(
from: wallet.address,
newAccountPubKey: mintAccount.address,
layout: SystemCreateLayout(
lamports: rent,
space: BigInt.from(mintAccSpace),
programId: SPLTokenProgramConst.tokenProgramId));

final mint = SPLTokenProgram.initializeMint2(
layout: SPLTokenInitializeMint2Layout(
mintAuthority: wallet.address,
decimals: 3,
freezeAuthority: freezeAuthority.address),
mint: mintAccount.address);
final tr = SolanaTransaction(
payerKey: wallet.address,
instructions: [createAccount, mint],
recentBlockhash: await wallet.recentBlockhash());
tr.sign([wallet.privateKey, mintAccount.privateKey]);
await wallet.submitTr(tr.serializeString());

/// https://explorer.solana.com/tx/mdjLvhvVUqfBKnmmCRAsmAjXGZsxEWMw7vvZMfT512rjeydmfgDfdX8YZmTjbvxNVqGbSBScedxAGXzfF3GLceR?cluster=testnet
}
24 changes: 24 additions & 0 deletions example/lib/example/solana/token_program/mint_to_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:example/example/solana/quick_wallet_for_testing/quick_wallet.dart';
import 'package:on_chain/solana/solana.dart';

void main() async {
final wallet = QuickWalletForTest(index: 1100);
final mintAccount = QuickWalletForTest(index: 1111);
final owner = SolAddress("6fLggs5D6iwCMwB5a2Scd3gqPYcvtpvWpFb4LXFQ89Bz");
final destinationTokenAccount =
AssociatedTokenAccountProgramUtils.associatedTokenAccount(
mint: mintAccount.address, owner: owner);
final createAssociatedTokenAccount = SPLTokenProgram.mintTo(
mint: mintAccount.address,
destination: destinationTokenAccount.address,
authority: wallet.address,
layout: SPLTokenMintToLayout(amount: BigInt.from(123456455345234234)));
final tr = SolanaTransaction(
payerKey: wallet.address,
instructions: [createAssociatedTokenAccount],
recentBlockhash: await wallet.recentBlockhash());
tr.sign([wallet.privateKey]);
await wallet.submitTr(tr.serializeString());

/// https://explorer.solana.com/tx/3BWNp5nkcwQz786WVfZmnTdhiE7c11Sef7YfhmPvmKYWbpp5KEcnYSLwm8xwqK4y2ufgRVHpwiWR39gDR4JYALH8?cluster=testnet
}
5 changes: 3 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ dependencies:
path: ../
web_socket_channel: ^2.4.0
http: ^1.1.0
blockchain_utils:
path: ../../blockchain_utils
blockchain_utils: ^2.1.2
# blockchain_utils:
# path: ../../blockchain_utils
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
Expand Down
1 change: 1 addition & 0 deletions lib/ada/licenses/cardano_serialization_lib
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Includes other software related under the MIT license:

- chain-libs, Copyright 2018-2019 IOHK. For licensing see /LICENSE-IOHK
14 changes: 14 additions & 0 deletions lib/ada/src/address/era/core/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ abstract class ADAAddress with ADASerialization {
/// Abstract property representing the Bech32 address.
abstract final String bech32Address;

bool get isRewardAddress => addressType == ADAAddressType.reward;

/// Default constructor for ADAAddress.
const ADAAddress.init();

Expand Down Expand Up @@ -91,4 +93,16 @@ abstract class ADAAddress with ADASerialization {
String toJson() {
return address;
}

@override
operator ==(other) {
return identical(this, other) ||
(other is ADAAddress &&
other.runtimeType == runtimeType &&
address == other.address);
}

@override
int get hashCode =>
address.hashCode ^ addressType.hashCode ^ network.hashCode;
}
3 changes: 3 additions & 0 deletions lib/ada/src/builder/builder.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export 'builder/tranasction_builder_utils.dart';
export 'builder/transaction_builder.dart';
export 'builder/mint_builder.dart';
export 'builder/certificate_builder.dart';
export 'builder/deposit.dart';
7 changes: 7 additions & 0 deletions lib/ada/src/builder/builder/certificate_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:on_chain/ada/ada.dart';

class ADACertificateBuilder {
final Certificate certificate;
final ADAAddress? signer;
const ADACertificateBuilder({required this.certificate, this.signer});
}
4 changes: 4 additions & 0 deletions lib/ada/src/builder/builder/deposit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ADADepositBuilder {
final BigInt deposit;
const ADADepositBuilder({required this.deposit});
}
32 changes: 32 additions & 0 deletions lib/ada/src/builder/builder/mint_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:blockchain_utils/binary/utils.dart';
import 'package:on_chain/ada/src/address/era/core/address.dart';
import 'package:on_chain/ada/src/models/ada_models.dart';

class ADAMinsBuilder {
final Assets mintingAssets;
final ADAAddress owner;
final List<int> pubKeyBytes;
ADAMinsBuilder(
{required List<int> pubKeyBytes,
required this.mintingAssets,
required this.owner})
: pubKeyBytes = BytesUtils.toBytes(pubKeyBytes);
PolicyID toPolicyId() {
return PolicyID(toScript().toHash().data);
}

MultiAsset toMultiAssets() => MultiAsset({toPolicyId(): mintingAssets});

NativeScriptScriptPubkey toScript() {
final publickKey = Ed25519KeyHash.fromPubkey(pubKeyBytes);
return NativeScriptScriptPubkey(publickKey);
}

MintAssets toMintAsset() {
return MintAssets(mintingAssets.assets);
}

MintInfo toMintInfo() {
return MintInfo(policyID: toPolicyId(), assets: toMintAsset());
}
}
18 changes: 8 additions & 10 deletions lib/ada/src/builder/builder/tranasction_builder_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ import 'package:on_chain/ada/src/models/fixed_bytes/models/models.dart';
import '../../models/transaction/witnesses/witnesses.dart';

class ADATransactionBuilderUtils {
static final List<int> _fake32Bytes = List<int>.unmodifiable(
List<int>.filled(AdaTransactionConstant.blake2b256DigestSize, 0));
static final List<int> _fakeSignature = List<int>.unmodifiable(
List<int>.filled(AdaTransactionConstant.signatureLength, 0));
static BootstrapWitness fakeBootStrapWitness(String byronAddr) {
final address = ADAByronAddress(byronAddr);
return BootstrapWitness(
vkey: Vkey(
List<int>.filled(AdaTransactionConstant.blake2b256DigestSize, 0)),
signature: Ed25519Signature(
List<int>.filled(AdaTransactionConstant.signatureLength, 0)),
chainCode:
List<int>.filled(AdaTransactionConstant.blake2b256DigestSize, 0),
vkey: Vkey(_fake32Bytes),
signature: Ed25519Signature(_fakeSignature),
chainCode: _fake32Bytes,
attributes: address.attributeSerialize());
}

static Vkeywitness fakeVkeyWitnessWitness() {
return Vkeywitness(
vKey: Vkey(
List<int>.filled(AdaTransactionConstant.blake2b256DigestSize, 0)),
signature: Ed25519Signature(
List<int>.filled(AdaTransactionConstant.signatureLength, 0)));
vKey: Vkey(_fake32Bytes), signature: Ed25519Signature(_fakeSignature));
}
}
Loading

0 comments on commit 201f5ac

Please sign in to comment.