Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode and Encode Rust String fixes #28

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Solnet.Metaplex.Examples/MetadataCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Run()
if ( balanceRes.WasSuccessful )
Console.WriteLine("Account balance: {0}", balanceRes.Result.Value);

var mint = wallet.GetAccount(20);
var mint = wallet.GetAccount(55);

//PDA METADATA

Expand Down Expand Up @@ -132,15 +132,15 @@ out nonce
true
)
)
.AddInstruction(
MetadataProgram.UpdateMetadataAccount(
new PublicKey(metadataAddress),
wallet.Account.PublicKey,
null,
data2,
null
)
)
// .AddInstruction(
// MetadataProgram.UpdateMetadataAccount(
// new PublicKey(metadataAddress),
// wallet.Account.PublicKey,
// null,
// data2,
// null
// )
// )
.Build(new List<Account> { wallet.Account , mint});

var simTX = client.SimulateTransactionAsync(TX).Result;
Expand Down
20 changes: 11 additions & 9 deletions Solnet.Metaplex.Test/MetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void MintToken()

}

//[TestMethod]
[TestMethod]
public void TestCreateMetadataAccount()
{
var rpcClient = ClientFactory.GetClient(Cluster.DevNet); //, logger);
Expand Down Expand Up @@ -197,12 +197,12 @@ out nonce
true //ISMUTABLE
)
)
// .AddInstruction(
// MetadataProgram.SignMetada(
// new PublicKey(metadataAddress),
// c2.key
// )
// )
.AddInstruction(
MetadataProgram.SignMetada(
new PublicKey(metadataAddress),
c2.key
)
)
.AddInstruction(
MetadataProgram.PuffMetada(
new PublicKey(metadataAddress)
Expand All @@ -221,12 +221,14 @@ out nonce
)
.Build(new List<Account> { fromAccount, wallet.GetAccount(101) });

//var txSim2 = rpcClient.SimulateTransaction(TX2);
var txSim2 = rpcClient.SimulateTransaction(TX2);

//InstructionDecoder.Register(MetadataProgram.ProgramIdKey, MetadataProgram.Decode);
//List<DecodedInstruction> decodedInstructions = InstructionDecoder.DecodeInstructions( TX2 );

//Console.WriteLine($"Transaction sim: \n { txSim2.RawRpcResponse }");
Console.WriteLine($"Transaction sim: \n { txSim2.RawRpcResponse }");

Assert.IsFalse( txSim2.RawRpcResponse.Contains("Error"));

}

Expand Down
11 changes: 8 additions & 3 deletions Solnet.Metaplex/MetadataAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ public static Data ParseData( List<string> data)
byte[] bytes = Convert.FromBase64String(data[0]);
ReadOnlySpan<byte> binData = new(bytes);

(string name,_) = binData.DecodeRustString( MetadataAccountLayout.nameOffset);
(string symbol,_) = binData.DecodeRustString( MetadataAccountLayout.symbolOffset);
(string uri,_) = binData.DecodeRustString( MetadataAccountLayout.uriOffset);
string name;
string symbol;
string uri;

binData.GetString( MetadataAccountLayout.nameOffset, out name);
binData.GetString( MetadataAccountLayout.symbolOffset, out symbol);
binData.GetString( MetadataAccountLayout.uriOffset, out uri);

uint sellerFee = binData.GetU16( MetadataAccountLayout.feeBasisOffset );

var numOfCreators = binData.GetU16( MetadataAccountLayout.creatorsOffset );
Expand Down
51 changes: 35 additions & 16 deletions Solnet.Metaplex/MetadataProgramData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,27 @@ internal static byte[] EncodeCreateMetadataAccountData (
)
{

byte[] encodedName = Serialization.EncodeRustString(parameters.name);
byte[] encodedSymbol = Serialization.EncodeRustString(parameters.symbol);
byte[] encodedUri = Serialization.EncodeRustString(parameters.uri);
byte[] encodedName = Encoding.UTF8.GetBytes(parameters.name);
byte[] encodedSymbol = Encoding.UTF8.GetBytes(parameters.symbol);
byte[] encodedUri = Encoding.UTF8.GetBytes(parameters.uri);

var buffer = new MemoryStream();
BinaryWriter writer = new BinaryWriter(buffer);

writer.Write( (byte) MetadataProgramInstructions.Values.CreateMetadataAccount );
writer.Write( encodedName.Length );
writer.Write( encodedName) ;
writer.Write( encodedSymbol.Length );
writer.Write( encodedSymbol );
writer.Write( encodedUri.Length );
writer.Write( encodedUri );
writer.Write( (ushort) parameters.sellerFeeBasisPoints);

if ( parameters.creators == null || parameters.creators?.Count < 1 )
{
writer.Write( (byte) 0); //Option()
} else
}
else
{
writer.Write( (byte) 1);
writer.Write( parameters.creators.Count );
Expand Down Expand Up @@ -170,13 +174,17 @@ internal static byte[] EncodeUpdateMetadataData (
{
writer.Write((byte)1);

byte[] encodedName = Serialization.EncodeRustString(parameters.name);
byte[] encodedSymbol = Serialization.EncodeRustString(parameters.symbol);
byte[] encodedUri = Serialization.EncodeRustString(parameters.uri);
byte[] encodedName = Encoding.UTF8.GetBytes(parameters.name);
byte[] encodedSymbol = Encoding.UTF8.GetBytes(parameters.symbol);
byte[] encodedUri = Encoding.UTF8.GetBytes(parameters.uri);

writer.Write(encodedName.Length);
writer.Write(encodedName);
writer.Write(encodedSymbol.Length);
writer.Write(encodedSymbol);
writer.Write(encodedUri.Length);
writer.Write(encodedUri);

writer.Write( encodedName) ;
writer.Write( encodedSymbol );
writer.Write( encodedUri );
writer.Write( (ushort) parameters.sellerFeeBasisPoints);

if ( parameters.creators == null || parameters.creators?.Count < 1 )
Expand Down Expand Up @@ -271,10 +279,15 @@ byte[] keyIndices
decodedInstruction.Values.Add("updateAuthorityKey", keys[keyIndices[4]]);
decodedInstruction.Values.Add("SysProgramId", keys[keyIndices[5]]);
decodedInstruction.Values.Add("SysVarRentKey", keys[keyIndices[6]]);

string name;
string symbol;
string uri;

int nameLength = data.GetString( 1 , out name);
int symbolLength = data.GetString( 1 + nameLength , out symbol);
int uriLength = data.GetString( 1 + nameLength + symbolLength ,out uri);

(string name , int nameLength) = data.DecodeRustString(1);
(string symbol, int symbolLength) = data.DecodeRustString(1 + nameLength);
(string uri, int uriLength) = data.DecodeRustString(1 + nameLength+symbolLength);
int sellerFeeBasisPoints = data.GetU16(1 + nameLength + symbolLength + uriLength);

decodedInstruction.Values.Add("name", name );
Expand Down Expand Up @@ -335,9 +348,15 @@ byte[] keyIndices
if ( data.GetU8(offset) == 1 )
{
offset++;
(string name , int nameLength) = data.DecodeRustString(offset);
(string symbol, int symbolLength) = data.DecodeRustString(offset+nameLength);
(string uri, int uriLength) = data.DecodeRustString(offset+nameLength+symbolLength);

string name;
string symbol;
string uri;

int nameLength = data.GetString(offset, out name);
int symbolLength = data.GetString(offset + nameLength, out symbol);
int uriLength = data.GetString(offset + nameLength + symbolLength, out uri);

int sellerFeeBasisPoints = data.GetU16(offset + nameLength + symbolLength + uriLength);

decodedInstruction.Values.Add("name", name );
Expand Down