BTCPayServer.NTag424 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package BTCPayServer.NTag424 --version 1.0.1                
NuGet\Install-Package BTCPayServer.NTag424 -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BTCPayServer.NTag424" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BTCPayServer.NTag424 --version 1.0.1                
#r "nuget: BTCPayServer.NTag424, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BTCPayServer.NTag424 as a Cake Addin
#addin nuget:?package=BTCPayServer.NTag424&version=1.0.1

// Install BTCPayServer.NTag424 as a Cake Tool
#tool nuget:?package=BTCPayServer.NTag424&version=1.0.1                

BTCPayServer.BoltCardTools

Introduction

This repository hosts tools that help with the creation of Bolt Cards.

Content:

Examples

How to read the UID of an NTag 424 smart card

Plug in a smart card reader, such as the identiv utrust 3700 f, and place an NTag 424 smart card on it.

Reference the nuget package BTCPayServer.NTag424.PCSC in your project.

dotnet add package BTCPayServer.NTag424.PCSC

Then to use it:

using BTCPayServer.NTag424.PCSC;
using System;

using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();
var key = new AESKey(new byte[16]);
await ntag.AuthenticateEV2First(0, key);

var id = await ntag.GetCardUID();
var idStr = Convert.ToHexString(id, 0, id.Length).ToLowerInvariant();
Console.WriteLine($"Card UID: {idStr}");

How to read the NDEF message of an NTag 424 smart card

using BTCPayServer.NTag424.PCSC;
using System;
using NdefLibrary.Ndef;

using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();
var message = await ntag.ReadNDef();
var uri = new NdefUriRecord(message[0]).Uri;
Console.WriteLine($"Card URI: {uri}");

How to verify the signature of an NTag 424 smart card

BoltCards involve the cooperation of three types of agents:

  • Card Issuer: This agent configures the cards for lightning payments. This includes setting up the card to use a specific LNUrl Withdraw Service and generating the access keys.
  • Payment processor: This agent reads the card and forwards the payment request to the LNUrl Withdraw Service.
  • LNUrl Withdraw Service: This service authenticates the card and completes the payment.

BoltCards setup involves three different type of access keys:

  • The IssuerKey: Owned by the Card Issuer, this key is used to configure the card.
  • The EncryptionKey: This key can either be unique to each card or shared among multiple cards. It must be known by the LNUrl Withdraw Service.
  • The AuthenticationKey: This key should be unique and is used to authenticate the card. It must also be known by the LNUrl Withdraw Service.

If you are the LNURL Withdraw Service, here how to authenticate the card:

using BTCPayServer.NTag424;
using BTCPayServer.NTag424.PCSC;
using System;
using System.Collections;
using NdefLibrary.Ndef;

// Set keys have you have setup the card
var encryptionKey = AESKey.Default;

using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();
var message = await ntag.ReadNDef();
var uri = new NdefUriRecord(message[0]).Uri;
var p = Regex.Match(uri, "p=(.*?)&").Groups[1].Value;
var c = Regex.Match(uri, "c=(.*)").Groups[1].Value;

var piccData = PICCData.Create(encryptionKey.Decrypt(p));

// Note that the `piccData.Uid` contains the UID of the card which can be used to fetch
// the proper real `authenticationKey` of the card.
var authenticationKey = AESKey.Default;

var expectedMac = authenticationKey.GetSunMac(piccData);
var expectedMacStr = Convert.ToHexString(expectedMac, 0, expectedMac.Length);

var actualMacStr = c;
if (expectedMacStr != c)
{
    throw new Exception("Invalid card");
}

// The LNUrlw service should also check `piccData.Counter` is always increasing between payments to avoid replay attacks.

How to setup a bolt card

using BTCPayServer.NTag424;
using BTCPayServer.NTag424.PCSC;
using System;
using System.Collections;

using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();

// Example with hard coded keys
var keys = new BoltcardKeys(
    IssuerKey: new AESKey("00000000000000000000000000000001".HexToBytes()),
    EncryptionKey: new AESKey("00000000000000000000000000000002".HexToBytes()),
    AuthenticationKey: new AESKey("00000000000000000000000000000002".HexToBytes()));
var lnurlwService = "lnurlw://test.com";

// Note `BoltcardKeys.Default` assumes the card hasn't been setup yet.
// If it was not the case, you would need to provide the access keys you provided during the last setup.
await ntag.SetupBoltcard(lnurlwService, BoltcardKeys.Default, keys);

// You can reset the card to its factory state with `await ntag.ResetCard(keys);`

License

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BTCPayServer.NTag424:

Package Downloads
BTCPayServer.NTag424.PCSC

A library to communicate with NTag 424 chips and assist BoltCard creation

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on BTCPayServer.NTag424:

Repository Stars
btcpayserver/btcpayserver
Accept Bitcoin payments. Free, open-source & self-hosted, Bitcoin payment processor.
Version Downloads Last updated
1.0.24 107 9/4/2024
1.0.23 6,397 4/24/2024
1.0.22 3,791 2/8/2024
1.0.21 156 1/25/2024
1.0.20 1,566 12/21/2023
1.0.19 887 12/21/2023
1.0.18 690 12/8/2023
1.0.17 258 11/3/2023
1.0.16 211 10/25/2023
1.0.15 156 10/24/2023
1.0.14 132 10/24/2023
1.0.13 159 10/24/2023
1.0.12 149 10/24/2023
1.0.11 227 10/23/2023
1.0.10 179 10/22/2023
1.0.9 168 10/22/2023
1.0.8 148 10/21/2023
1.0.7 144 10/20/2023
1.0.6 149 10/20/2023
1.0.5 155 10/20/2023
1.0.4 152 10/20/2023
1.0.3 156 10/20/2023
1.0.2 129 10/20/2023
1.0.1 147 10/6/2023
1.0.0 134 10/4/2023