OpenSea.ImageEncoder
1.0.8
dotnet add package OpenSea.ImageEncoder --version 1.0.8
NuGet\Install-Package OpenSea.ImageEncoder -Version 1.0.8
<PackageReference Include="OpenSea.ImageEncoder" Version="1.0.8" />
paket add OpenSea.ImageEncoder --version 1.0.8
#r "nuget: OpenSea.ImageEncoder, 1.0.8"
// Install OpenSea.ImageEncoder as a Cake Addin #addin nuget:?package=OpenSea.ImageEncoder&version=1.0.8 // Install OpenSea.ImageEncoder as a Cake Tool #tool nuget:?package=OpenSea.ImageEncoder&version=1.0.8
OpenSea Avatar Data Encoder/Decoder
This package has nothing todo Gary Vaynerchuk.
Overview
The OpenSea Avatar Data Encoder/Decoder is a service that allows you to encode and decode data within OpenSea avatar images. This service uses the least significant bit (LSB) manipulation technique to embed and retrieve data from the image's pixel data.
Installation
Add the OpenSea Avatar Data Encoder/Decoder service to your project by registering the dependency in your service container.
services.AddOpenSeaImageClient(new()
{
ApiKey = config.GetValue<string>("apiKey")!
});
Usage
Encoding and Decoding Data
You can use the ImageEncoder
class to encode and decode data within OpenSea avatar images. Below are examples of how to use this service in your application and tests.
Example: Encoding Data to an OpenSea Avatar
To encode data into an OpenSea avatar image, use the EncodeOpenSeaAvatar
method of the ImageEncoder
class. This method takes the wallet address, the data to encode, and a cancellation token as parameters.
public async Task<Stream> EncodeOpenSeaAvatar(string walletAddress, string codeToEncode, CancellationToken cancellationToken)
{
var imageStream = await openSeaUserFetcher
.GetUserAvatarByWalletAddress(walletAddress, cancellationToken)
.ConfigureAwait(false);
var newStream = await EncoderDecoderService
.EncodeDataToImageStream(
imageStream: imageStream,
dataToEncode: codeToEncode,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
return newStream;
}
Example: Decoding Data from an OpenSea Avatar
To decode data from an OpenSea avatar image, use the DecodeOpenSeaAvatar
method of the ImageEncoder
class. This method takes the wallet address and a cancellation token as parameters and returns the decoded data.
public async Task<string> DecodeOpenSeaAvatar(string walletAddress, CancellationToken cancellationToken)
{
var imageStream = await openSeaUserFetcher
.GetUserAvatarByWalletAddress(walletAddress, cancellationToken)
.ConfigureAwait(false);
var code = await EncoderDecoderService
.DecodeDataFromImageStream(
imageStream: imageStream,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
return code;
}
Unit Tests
Below are examples of how to write unit tests for encoding and decoding data to and from OpenSea avatar images.
Test: Encoding Data to an OpenSea Avatar
This test verifies that the EncodeOpenSeaAvatar
method correctly encodes data into the OpenSea avatar image.
public class ImageEncoderTests
{
private readonly ImageEncoder imageEncoder;
public ImageEncoderTests(ImageEncoder imageEncoder)
{
this.imageEncoder = imageEncoder;
}
[Fact]
public async Task Should_Encode_OpenSea_Avatar_Correctly()
{
var walletAddress = "0x1234567890abcdef";
var codeToEncode = "abcd1234";
var encodedStream = await this.imageEncoder.EncodeOpenSeaAvatar(walletAddress, codeToEncode, CancellationToken.None);
using (var fileStream = File.Create("encoded_avatar.png"))
{
await encodedStream.CopyToAsync(fileStream);
}
// Optionally, you can decode it back to verify if it was encoded correctly
var decodedData = await this.imageEncoder.DecodeOpenSeaAvatar(walletAddress, CancellationToken.None);
Assert.Equal(codeToEncode, decodedData);
}
}
Test: Decoding Data from an OpenSea Avatar
This test verifies that the DecodeOpenSeaAvatar
method correctly decodes data from the OpenSea avatar image.
public class ImageDecoderTests
{
private readonly ImageEncoder imageEncoder;
public ImageDecoderTests(ImageEncoder imageEncoder)
{
this.imageEncoder = imageEncoder;
}
[Fact]
public async Task Should_Decode_OpenSea_Avatar_Correctly()
{
var walletAddress = "0x1234567890abcdef";
var expectedOutput = "abcd1234";
var actualOutput = await this.imageEncoder.DecodeOpenSeaAvatar(walletAddress, CancellationToken.None);
Assert.Equal(expectedOutput, actualOutput);
}
}
Example: Encoding and Decoding Data
To encode data into an image, use the EncodeImage
method of the ImageEncoder
class. This method takes the image URL and the data to encode as parameters.
public async Task EncodeImage(string imageUrl, string dataToEncode, CancellationToken cancellationToken)
{
var encodedImageStream = await this.imageEncoder.EncodeImage(imageUrl, dataToEncode, cancellationToken);
// Save the encoded image stream to a file or use it as needed
using (var fileStream = File.Create("encoded_image.png"))
{
await encodedImageStream.CopyToAsync(fileStream);
}
}
To decode data from an image, use the DecodeImage
method of the ImageEncoder
class. This method takes the image URL as a parameter and returns the decoded data.
public async Task<string> DecodeImage(string imageUrl, CancellationToken cancellationToken)
{
var decodedData = await this.imageEncoder.DecodeImage(imageUrl, cancellationToken);
return decodedData;
}
Example Unit Tests
Test: Decoding OpenSea Avatar Correctly
This test verifies that the DecodeImage
method correctly decodes the data from a given image URL.
public class ImageDecoderTests
{
private readonly ImageEncoder imageEncoder;
public ImageDecoderTests(ImageEncoder imageEncoder)
{
this.imageEncoder = imageEncoder;
}
[Fact]
public async Task Should_Decode_OpenSea_Avatar_Correctly()
{
var sample = "https://i.seadn.io/s/raw/files/49638454f0bcfb989748cc48ede2dce2.webp";
var expectedOutput = "abcd1234";
var actualOutput = await this.imageEncoder.DecodeImage(sample, CancellationToken.None);
Assert.Equal(expectedOutput, actualOutput);
}
[Fact]
public async Task Should_Decode_OpenSea_Avatar_Incorrectly()
{
var sample = "https://i.seadn.io/gae/o_Ys9atAhGen_SQ9aWS1tiDflIRNQpHphdhIL_dWJuJAFIyoyVt42WIjxnt1t8LvC45whEeBmvp7sJJkT2NIGIMu4Z4go1_fe9wMzg";
var expectedOutput = "��������";
var actualOutput = await this.imageEncoder.DecodeImage(sample, CancellationToken.None);
Assert.Equal(expectedOutput, actualOutput);
}
}
Conclusion
The OpenSea Avatar Data Encoder/Decoder provides a simple yet powerful way to embed and extract data within OpenSea avatar images using LSB manipulation. With straightforward installation and usage, you can easily integrate this service into your applications and tests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.Http (>= 8.0.0)
- SixLabors.ImageSharp (>= 3.1.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.0.8 | 112 | 5/14/2024 |