Few renaming and package settings

This commit is contained in:
nicolas.dorier 2023-10-04 18:41:10 +09:00
parent cf5ba1e10e
commit 5b67d1aa15
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
8 changed files with 104 additions and 14 deletions

View File

@ -5,7 +5,7 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BTCPayServer.NTag424", "src\BTCPayServer.NTag424\BTCPayServer.NTag424.csproj", "{1AF51732-C36B-48DE-BEE5-4C5AA6EB0DAB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BoltCardTools.Tests", "tests\BoltCardTools.Tests.csproj", "{4ABD12CC-3B7C-4C8C-8576-19F32C402584}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BTCPayServer.NTag424.Tests", "tests\BTCPayServer.NTag424.Tests.csproj", "{4ABD12CC-3B7C-4C8C-8576-19F32C402584}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{AE043F8E-1026-4502-8E7A-6F9C6C75EAF6}"
ProjectSection(SolutionItems) = preProject
@ -38,4 +38,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ACB2349C-2A7E-4F07-9ABC-4B1C333ABBFC}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
</PropertyGroup>
<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<PropertyGroup>
<Copyright>Copyright © BTCPay Server 2017</Copyright>
<Description>A library to communicate with NTag 424 chips and assist BoltCard creation</Description>
<PackageIcon>../../BTCPayServer.png</PackageIcon>
<PackageTags>ntag424,rfid</PackageTags>
<PackageProjectUrl>https://github.com/btcpayserver/BTCPayServer.BoltCardTools/</PackageProjectUrl>
<RepositoryUrl>https://github.com/btcpayserver/BTCPayServer.BoltCardTools/</RepositoryUrl>
<PackageReadmeFile>https://github.com/btcpayserver/BTCPayServer.BoltCardTools/Readme.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BTCPayServer.NTag424\BTCPayServer.NTag424.csproj" />
<PackageReference Include="PCSC" Version="6.1.3" />
<PackageReference Include="PCSC.Iso7816" Version="6.1.3" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,38 @@
using System.Buffers;
using PCSC;
using PCSC.Extensions;
namespace BTCPayServer.NTag424;
public class PCSCAPDUTransport : IAPDUTransport
{
public readonly ISCardReader CardReader;
public PCSCAPDUTransport(ISCardReader cardReader)
{
ArgumentNullException.ThrowIfNull(cardReader);
CardReader = cardReader;
}
public Task<NtagResponse> SendAPDU(NTagCommand apdu)
{
return Task.Factory.StartNew(() =>
{
var bytes = apdu.ToBytes();
var resp = ArrayPool<byte>.Shared.Rent(512);
try
{
int received = resp.Length;
var sc = CardReader.Transmit(bytes, resp, ref received);
if (sc != SCardError.Success)
sc.Throw();
var sw1sw2 = (ushort)(resp[received - 2] << 8 | resp[received - 1]);
var data = resp[..(received - 2)];
return new NtagResponse(data, sw1sw2);
}
finally
{
ArrayPool<byte>.Shared.Return(resp);
}
}, TaskCreationOptions.LongRunning);
}
}

View File

@ -1,13 +1,11 @@
using BTCPayServer.NTag424;
using PCSC;
namespace BTCPayServer.NTag424.Tests;
public record CardReaderContext(ISCardReader CardReader, IContextFactory ContextFactory, ISCardContext Context) : IDisposable
namespace BTCPayServer.NTag424.PCSC;
public record PCSCContext(ISCardReader CardReader, ISCardContext Context) : IDisposable
{
public static CardReaderContext Create()
public static PCSCContext Create()
{
var contextFactory = PCSC.ContextFactory.Instance;
var contextFactory = ContextFactory.Instance;
var context = contextFactory.Establish(SCardScope.System);
var readerNames = context.GetReaders();
var readerName = readerNames.FirstOrDefault();
@ -17,14 +15,13 @@ public record CardReaderContext(ISCardReader CardReader, IContextFactory Context
}
var reader = new SCardReader(context);
reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);
return new CardReaderContext(reader, contextFactory, context);
return new PCSCContext(reader, context);
}
public void Dispose()
{
CardReader.Dispose();
Context.Dispose();
}
public Ntag424 CreateNTag424()
{
var transport = new PCSCAPDUTransport(this.CardReader);

View File

@ -3,7 +3,21 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
</PropertyGroup>
<PropertyGroup>
<Copyright>Copyright © BTCPay Server 2017</Copyright>
<Description>A library to communicate with NTag 424 chips and assist BoltCard creation</Description>
<PackageIcon>../../BTCPayServer.png</PackageIcon>
<PackageTags>ntag424,rfid</PackageTags>
<PackageProjectUrl>https://github.com/btcpayserver/BTCPayServer.BoltCardTools/</PackageProjectUrl>
<RepositoryUrl>https://github.com/btcpayserver/BTCPayServer.BoltCardTools/</RepositoryUrl>
<PackageReadmeFile>https://github.com/btcpayserver/BTCPayServer.BoltCardTools/Readme.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<PropertyGroup>
@ -12,10 +26,11 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NdefLibrary" Version="4.1.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>

View File

@ -1,3 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("BoltCardTools.Tests")]
[assembly: InternalsVisibleTo("BTCPayServer.NTag424.Tests")]

View File

@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using BTCPayServer.NTag424.PCSC;
using NdefLibrary.Ndef;
namespace BTCPayServer.NTag424.Tests;
@ -136,7 +137,7 @@ public class UnitTest1
[Fact]
public async Task CanAuthenticate()
{
using var ctx = CardReaderContext.Create();
using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();
var key = new AESKey(new byte[16]);
await ntag.AuthenticateEV2First(0, key);
@ -149,7 +150,7 @@ public class UnitTest1
[Fact]
public async Task CanChangeKey()
{
using var ctx = CardReaderContext.Create();
using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();
var key1 = new AESKey(new byte[16]);
var key2b = new byte[16];
@ -167,7 +168,7 @@ public class UnitTest1
[Fact]
public async Task CanDoBoltcard()
{
using var ctx = CardReaderContext.Create();
using var ctx = PCSCContext.Create();
var ntag = ctx.CreateNTag424();
var key = new AESKey(new byte[16]);
await ntag.AuthenticateEV2First(0, key);