Giter VIP home page Giter VIP logo

cardanosharp-wallet's Introduction

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

cardanosharp-wallet's People

Contributors

daniel-schaeferj avatar eddex avatar nothingalike avatar safestak-keith avatar tweakch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.