Read payloads from documents
Use Verifiabl.Verifier.Extraction to read the payload from a payslip PDF, an image file or pixels. For a PDF or an image file, it reads and verifies in one call. The package reads each file in a WebAssembly sandbox.
The package sends the payload text to VerifyAsync without changes. A verification shows that the payload matches the record that its issuer registered. It does not examine the visible contents of the document.
To read the payload with your own tools instead, refer to Read the payload.
Install the package
Section titled “Install the package”dotnet add package Verifiabl.Verifier.Extraction --version 0.1.0-rc.3The package contains the extraction module and the native libraries of Wasmtime, which runs the module. Refer to Platforms. extraction-release.json in the package gives the extraction release of the module and its SHA-256.
Verify a PDF
Section titled “Verify a PDF”using Verifiabl.Verifier;using Verifiabl.Verifier.Extraction.Pdf;
using FileStream pdf = File.OpenRead("payslip.pdf");PdfVerificationResult pdfResult = await verifier.VerifyPdfAsync(pdf, cancellationToken: cancellationToken);VerificationResult result = pdfResult.Verification;The package reads the metadata payload first. If the PDF has no metadata payload, the package makes an image of each page with PDFium and reads the QR code. It finds a code in an image, on a scanned page or in vector shapes. A metadata value longer than VerifierClient.MaxBarcodeLength is not a payload.
To read the payload without verification, use PayslipPdfReader.ReadAsync. It sets PdfPayloads.Metadata, or PdfPayloads.QrCode if the PDF has no metadata payload.
Verify an image
Section titled “Verify an image”using Verifiabl.Verifier;using Verifiabl.Verifier.Extraction.Images;
using FileStream photo = File.OpenRead("payslip-qr.png");ImageVerificationResult imageResult = await verifier.VerifyImageAsync(photo, cancellationToken: cancellationToken);VerificationResult result = imageResult.Verification;The package reads the QR code from a PNG, JPEG or WebP file, for example a photo or a screenshot of a payslip. To read the QR code without verification, use PayslipImageReader.ReadAsync.
Read a QR code from pixels
Section titled “Read a QR code from pixels”For a file that your organisation did not make, use VerifyImageAsync. With pixels, only the QR code read is in the sandbox. Your imaging library decodes the file in your process.
If you decode images with your own imaging library, give the pixels to QrPayloadReader.
using Verifiabl.Verifier;using Verifiabl.Verifier.Extraction;
QrImage image = QrImage.FromRgba32(pixels, width, height);string? payload = await QrPayloadReader.ReadAsync(image, cancellationToken: cancellationToken);
if (payload is not null){ var request = new VerificationRequest(payload); VerificationResult result = await verifier.VerifyAsync(request, cancellationToken);}QrImage accepts RGBA, BGRA or greyscale pixels, in rows from top to bottom with no padding. A code gives the same result from a file and from its pixels. Do not change the pixel array during the call.
Verifiabl codes
Section titled “Verifiabl codes”The package reads only QR codes that have the shape of a Verifiabl payload:
- A bare
1|…or2|…payload. - A scan link on a
verifiabl.iohost. - A scan link on the origin of an issuer that contains a complete payload:
https://<origin>/v/<reference>#<version>.<ciphertext>.
The package ignores other codes. It does not open scan links. A scan link and a bare payload with the same contents are one code. Two different Verifiabl codes give PayloadExtractionException with Failure set to Ambiguous.
VerifyPdfAsync and VerifyImageAsync are extension methods on IVerifierClient. They work with your fakes and decorators.
Retries
Section titled “Retries”To send a failed call again without a second charge, give the idempotency key of the failure in 1 hour or less. A call without a key is a new verification.
The first call reads the stream to its end. Open the file again, or give the same byte[] to the two calls.
VerificationResult result;try{ using FileStream pdf = File.OpenRead("payslip.pdf"); PdfVerificationResult pdfResult = await verifier.VerifyPdfAsync(pdf, cancellationToken: cancellationToken); result = pdfResult.Verification;}catch (VerifierException failure) when (failure.IsRetryable){ // Later, or from a queue. The same key makes this a retry, not a new verification. using FileStream retryPdf = File.OpenRead("payslip.pdf"); PdfVerificationResult pdfResult = await verifier.VerifyPdfAsync( retryPdf, idempotencyKey: failure.IdempotencyKey, cancellationToken: cancellationToken); result = pdfResult.Verification;}To send a call again after a restart, make a key with Guid.NewGuid() and save it before the first attempt. Give it to each call. VerifyImageAsync accepts the same idempotencyKey.
Errors
Section titled “Errors”Catch PayloadExtractionException before the general VerifierException.
try{ using FileStream pdf = File.OpenRead("payslip.pdf"); PdfVerificationResult result = await verifier.VerifyPdfAsync(pdf, cancellationToken: cancellationToken);}catch (PayloadExtractionException unread){ // No payload was read, so nothing was sent to Verifiabl. PayloadExtractionFailure reason = unread.Failure;}catch (VerifierRejectedException){ // The payload could not be verified.}catch (VerifierException failure) when (failure.IsRetryable){ // Temporary. Send the same idempotency key again later.}| Exception | Cause |
|---|---|
PayloadExtractionException |
The package did not read one payload. It sent nothing to Verifiabl. Failure gives the reason. |
VerifierException and its subtypes |
The payload could not be verified, or the verification did not complete. Refer to Handle errors. |
InvalidOperationException |
The sandbox did not start. For example, the native library of Wasmtime did not load on this platform. A file does not cause this error. |
OperationCanceledException |
You cancelled the token. |
PayloadExtractionException is not a result about the payslip.
Failure |
Cause |
|---|---|
NotFound |
The file has no Verifiabl payload that the package can read. |
Ambiguous |
The file has two different Verifiabl payloads. |
LimitExceeded |
The file is more than a limit. |
Unreadable |
The package cannot read the file. |
TimedOut |
The read did not complete in its time limit. IsRetryable is true. Try again later. |
ArgumentException shows an incorrect argument, for example a timeout that is not positive or a pixel array of the wrong length.
Limits and time
Section titled “Limits and time”| Limit | Value |
|---|---|
| File size | SandboxModule.MaxFileBytes, 20 MiB, for a PDF or an image file. The package reads a stream only up to this limit. |
| Image formats | PNG, JPEG and WebP. Other formats, for example GIF and BMP, give Unreadable. |
| Pixels | QrImage.MaxPixels, 50 megapixels. For an image file, the package checks the header before it decodes the pixels. |
| PDF pages | PayslipPdfReader.MaxPages, 50. The package does not read a longer PDF that has no metadata payload. |
| PDF page images | 200 dpi. Each page has a maximum of QrImage.MaxPixels. All pages together have a maximum of 200 megapixels. |
| PDF metadata | 64 metadata streams, 4 MiB of decoded metadata and approximately 1 MiB of payload text. More gives LimitExceeded. |
| QR codes | Approximately 1 MiB of QR code text. More gives LimitExceeded. |
| Memory | 512 MiB for each read, in the sandbox. More gives LimitExceeded. |
| Time | PdfReadOptions.Timeout, ImageReadOptions.Timeout and QrReadOptions.Timeout. The default for each is 30 seconds. |
For files that your organisation did not make, use ReadAsync or a verify method. The synchronous Read methods have no time limit and no limit on the number of reads at the same time.
A call to VerifyPdfAsync or VerifyImageAsync can continue for the read time limit plus the Timeout of the client. The read time limit includes the time to read the stream. If the stream does not obey the cancellation token, the call can continue for longer. The call then waits until the current read from the stream ends. The package does not dispose the stream, and does not read it after the call ends.
ReadAsync and the verify methods decode a maximum of one file for each processor at the same time. For each processor, make 512 MiB, plus the file size, available. Each call also keeps its file in memory while it waits to decode. A call reads all of a stream before it waits. The number of calls that wait has no limit. To keep memory use in a fixed limit, limit the number of uploads at the same time in your web server. If you use the synchronous Read methods, limit the number of reads yourself.
Sandbox
Section titled “Sandbox”The decoders run in a WebAssembly module with its own memory, in your process. The module cannot open files, use the network or read the memory of your process. Each read gets a new instance of the module. The synchronous Read methods block the calling thread until the read ends. If the module stops or returns an incorrect result, the package throws PayloadExtractionException with Failure set to Unreadable.
For pixels that your own library decoded, refer to Read a QR code from pixels.
Startup
Section titled “Startup”Load the module at startup. If you do not, the first read in the process compiles it, and that read is approximately 2 seconds slower. The read time limit starts after the module is ready.
using Verifiabl.Verifier.Extraction;
await SandboxModule.LoadAsync();Platforms
Section titled “Platforms”The package contains the native libraries of Wasmtime for Windows, macOS and Linux with glibc, on x64 and Arm64. Alpine Linux (musl) and 32-bit processes are not supported. On these platforms, a read throws InvalidOperationException.
On .NET Framework, reference the package with PackageReference. Run the application as a 64-bit process. When you compile the application, the package copies the Wasmtime library to wasmtime\x64\ and wasmtime\arm64\ in your output folder. Deploy these folders with the application. The package loads Wasmtime only from these folders.
Wasmtime adds no signal handlers or exception handlers to your process.
Licences
Section titled “Licences”The Verifiabl code in the package has the MIT licence. THIRD-PARTY-NOTICES.txt in the package contains the licences of PDFium, ZXing-C++ and the other components of the module. The Wasmtime licence is licenses/wasmtime/LICENSE in the package.