Skip to content

CardanoSharp.Wallet is a Cardano Cryptographic and Serialization library for .NET applications.

License

Notifications You must be signed in to change notification settings

santoraphael/cardanosharp-wallet

 
 

Repository files navigation

CardanoSharp.Wallet

Build status Test status NuGet Version NuGet Downloads

CardanoSharp Wallet is a .NET library for Creating/Managing Wallets and Building/Signing Transactions.

Features

  • Generate Mnemonics
  • Create Private and Public Keys
  • Create Addresses
  • Build Transactions
  • Sign Transactions

Getting Started

CardanoSharp.Wallet is installed from NuGet.

Install-Package CardanoSharp.Wallet

Generate a Mnemonic

using CardanoSharp.Wallet;
using CardanoSharp.Wallet.Enums;
using CardanoSharp.Wallet.Models.Keys;

class Program
{
    static void Main(string[] args)
    {
        // The KeyServices exposes functions for Mnemonics and Deriving Keys
        var keyService = new KeyService();
        // The AddressService allows us to create Addresses from our Public Keys
        var addressService = new AddressService();
        // 24 represents the number of words we want for our Mnemonic
        int size = 24;

        // This will generate a 24 English Mnemonic
        Mnemonic mnemonic = keyService.Generate(size, WordLists.English);
        System.Console.WriteLine(mnemonic.Words);
    }
}

Create Private and Public Keys

Add powerful extensions to create and derive keys.

using CardanoSharp.Wallet.Extensions.Models;
// The masterKey is a PrivateKey made of up of the 
//  - byte[] Key
//  - byte[] Chaincode
PrivateKey masterKey = mnemonic.GetRootKey();

// This path will give us our Payment Key on index 0
string paymentPath = $"m/1852'/1815'/0'/0/0";
// The paymentPrv is another Tuple with the Private Key and Chain Code
PrivateKey paymentPrv = masterKey.Derive(paymentPath);
// Get the Public Key from the Payment Private Key
PublicKey paymentPub = paymentPrv.GetPublicKey(false);

// This path will give us our Stake Key on index 0
string stakePath = $"m/1852'/1815'/0'/2/0";
// The stakePrv is another Tuple with the Private Key and Chain Code
var stakePrv = masterKey.Derive(stakePath);
// Get the Public Key from the Stake Private Key
var stakePub = stakePrv.GetPublicKey(false);

Create Addresses

// Creating Addresses require the Public Payment and Stake Keys
Address baseAddr = addressService.GetAddress(
    paymentPub, 
    stakePub, 
    NetworkType.Testnet, 
    AddressType.Base);

NetworkType

namespace CardanoSharp.Wallet.Enums
{
    public enum NetworkType
    {
        Testnet,
        Mainnet
    }
}

AddressType

namespace CardanoSharp.Wallet.Enums
{
    public enum AddressType
    {
        Base,
        Enterprise,
        Reward
    }
}

Build and Sign Transactions

This is just an example of how to start. You will need to Calculate Fees, compare with Protocol Parameters and re-serialize.

using CardanoSharp.Wallet.Models.Transactions;
// The Transaction Builder allows us to contruct and serialize our Transaction
using CardanoSharp.Wallet.Models.Transactions;
using CardanoSharp.Wallet.Extensions.Models.Transactions;
//For CBOR Utilities
using PeterO.Cbor2;

// Create the Transaction Body
//  The Transaction Body:
//      Supported
//       - Transaction Inputs
//       - Transaction Outputs
//       - Fee
//       - TTL
//       - Certificates
//       - Metadata Hash
//      Coming Soon
//       - Transaction Start Interval
//       - Withdrawls
//       - Mint
var transactionBody = new TransactionBody()
{
    TransactionInputs = new List<TransactionInput>()
    {
        new TransactionInput()
        {
            TransactionIndex = 0,
            TransactionId = new byte[32]
        }
    },
    TransactionOutputs = new List<TransactionOutput>()
    {
        new TransactionOutput()
        {
            Address = baseAddr.GetBytes(),
            Value = new TransactionOutputValue()
            {
                Coin = 1
            }
        }
    },
    Ttl = 10,
    Fee = 0
};

// Add our witness(es)
//  Currently we only support VKey Witnesses
var witnesses = new TransactionWitnessSet()
{
    VKeyWitnesses = new List<VKeyWitness>()
    {
        new VKeyWitness()
        {
            VKey = paymentPub,
            SKey = paymentPrv
        }
    }
};

// Create Transaction
var transaction = new Transaction()
{
    TransactionBody = transactionBody,
    TransactionWitnessSet = witnesses
};

// Serialize Transaction with Body and Witnesses
//  This results in a Signed Transaction
var signedTxSerialized = transaction.Serialize();

Calculate Fees

// From Current Protocol Parameters
// 44     = txFeePerByte
// 155381 = txFeeFixed
var fee = transaction.CalculateFee(44, 155381);

Adding Metadata

var auxData = new AuxiliaryData()
{
    Metadata = new Dictionary<int, object>()
    {
        { 1234, new { name = "simple message" } }
    }
};

var transaction = new Transaction()
{
    TransactionBody = transactionBody,
    TransactionWitnessSet = witnesses,
    AuxiliaryData = auxData
};

More Examples

Please see the Transaction Tests

About

CardanoSharp.Wallet is a Cardano Cryptographic and Serialization library for .NET applications.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%