Skip to content

Commit

Permalink
⚡ Align partial sign with Web3.js (#11)
Browse files Browse the repository at this point in the history
Align partial sign with Web3.js version, including fix from the original Solnet codebase
  • Loading branch information
GabrielePicco authored Sep 25, 2022
1 parent 76a82d9 commit 9cd1137
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 89 deletions.
14 changes: 7 additions & 7 deletions SharedBuildProperties.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Solana.Unity</Product>
<Version>2.6.0.8</Version>
<Copyright>Copyright 2022 &#169; Solana.Unity</Copyright>
<Authors>garbles-dev</Authors>
<PublisherName>garbles-dev</PublisherName>
<RepositoryUrl>https://github.com/garbles-dev/Solana.Unity</RepositoryUrl>
<Version>2.6.0.9</Version>
<Copyright>Copyright 2022 &#169; Garbles Labs</Copyright>
<Authors>Garbles Labs</Authors>
<PublisherName>Garbles Labs</PublisherName>
<RepositoryUrl>https://github.com/garbles-labs/Solana.Unity-Core</RepositoryUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>latest</LangVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>icon.png</PackageIcon>
<PackageDescription>Solana.Unity is Solana's .NET standard 2.0 integration library.</PackageDescription>
<PackageDescription>Solana.Unity-Core is Solana's .NET standard 2.0 integration library.</PackageDescription>
<PackageTags>solana;solnet;sol;net2.0;unity;spl</PackageTags>
<PackageReleaseNotes>https://github.com/garbles-dev/Solana.Unity/releases</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/garbles-labs/Solana.Unity-Core/releases</PackageReleaseNotes>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

Expand Down
28 changes: 20 additions & 8 deletions src/Solana.Unity.Rpc/Builders/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ internal byte[] Build()

for (int i = 0; i < keyCount; i++)
{
keyIndices[i] = (byte)FindAccountIndex(keysList, instruction.Keys[i].PublicKeyBytes);
keyIndices[i] = FindAccountIndex(keysList, instruction.Keys[i].PublicKey);
}

CompiledInstruction compiledInstruction = new CompiledInstruction
{
ProgramIdIndex = (byte)FindAccountIndex(keysList, instruction.ProgramId),
ProgramIdIndex = FindAccountIndex(keysList, instruction.ProgramId),
KeyIndicesCount = ShortVectorEncoding.EncodeLength(keyCount),
KeyIndices = keyIndices,
DataLength = ShortVectorEncoding.EncodeLength(instruction.Data.Length),
Expand Down Expand Up @@ -180,8 +180,9 @@ internal byte[] Build()
private List<AccountMeta> GetAccountKeys()
{
List<AccountMeta> newList = new();
IList<AccountMeta> keysList = _accountKeysList.AccountList;
int feePayerIndex = FindAccountIndex(keysList, FeePayer.KeyBytes);
var keysList = _accountKeysList.AccountList;
int feePayerIndex = Array.FindIndex<AccountMeta>( _accountKeysList.AccountList.ToArray(),
x => x.PublicKey == FeePayer.Key);

if (feePayerIndex == -1)
{
Expand All @@ -204,15 +205,26 @@ private List<AccountMeta> GetAccountKeys()
/// <param name="accountMetas">The <see cref="AccountMeta"/>.</param>
/// <param name="publicKey">The public key.</param>
/// <returns>The index of the</returns>
private static int FindAccountIndex(IList<AccountMeta> accountMetas, byte[] publicKey)
private static byte FindAccountIndex(IList<AccountMeta> accountMetas, byte[] publicKey)
{
string encodedKey = Encoders.Base58.EncodeData(publicKey);
for (int index = 0; index < accountMetas.Count; index++)
return FindAccountIndex(accountMetas, encodedKey);
}

/// <summary>
/// Finds the index of the given public key in the accounts list.
/// </summary>
/// <param name="accountMetas">The <see cref="AccountMeta"/>.</param>
/// <param name="publicKey">The public key.</param>
/// <returns>The index of the</returns>
private static byte FindAccountIndex(IList<AccountMeta> accountMetas, string publicKey)
{
for (byte index = 0; index < accountMetas.Count; index++)
{
if (accountMetas[index].PublicKey == encodedKey) return index;
if (accountMetas[index].PublicKey == publicKey) return index;
}

return -1;
throw new Exception($"Something went wrong encoding this transaction. Account `{publicKey}` was not found among list of accounts. Should be impossible.");
}
}
}
31 changes: 12 additions & 19 deletions src/Solana.Unity.Rpc/Models/AccountKeysList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,27 @@ internal class AccountKeysList
/// <summary>
/// Get the accounts as a list.
/// </summary>
internal IList<AccountMeta> AccountList
internal List<AccountMeta> AccountList
{
get
{
List<AccountMeta> res = new(_accounts.Count);
for (int i = 0; i < _accounts.Count; i++)
{
if (_accounts[i].IsSigner)
{
res.Add(_accounts[i]);
}
}
List<AccountMeta> res = _accounts.Select(acc => acc).ToList();

for (int i = 0; i < _accounts.Count; i++)
res.Sort((x, y) =>
{
if (!_accounts[i].IsSigner && _accounts[i].IsWritable)
if (x.IsSigner != y.IsSigner)
{
res.Add(_accounts[i]);
// Signers always come before non-signers
return x.IsSigner ? -1 : 1;
}
}

for (int i = 0; i < _accounts.Count; i++)
{
if (!_accounts[i].IsSigner && !_accounts[i].IsWritable)
if (x.IsWritable != y.IsWritable)
{
res.Add(_accounts[i]);
// Writable accounts always come before read-only accounts
return x.IsWritable ? -1 : 1;
}
}
// Otherwise, sort by pubkey, stringwise.
return x.PublicKey.CompareTo(y.PublicKey);
});

return res;
}
Expand Down
6 changes: 3 additions & 3 deletions test/Solana.Unity.Extensions.Test/TokenWalletTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class TokenWalletTest
{

private const string MnemonicWords =
"route clerk disease box emerge airport loud waste attitude film army tray" +
" forward deal onion eight catalog surface unit card window walnut wealth medal";
"route clerk disease box emerge airport loud waste attitude film army tray" +
" forward deal onion eight catalog surface unit card window walnut wealth medal";

private const string Blockhash = "5cZja93sopRB9Bkhckj5WzCxCaVyriv2Uh5fFDPDFFfj";

Expand Down Expand Up @@ -146,7 +146,7 @@ public void TestProvisionAtaInjectBuilder()
var testAccounts = wallet.TokenAccounts().WithMint("98mCaWvZYTmTHmimisaAQW4WGLphN1cWhcC7KtnZF819");
Assert.AreEqual(1, testAccounts.Count());
Assert.AreEqual(0, testAccounts.WhichAreAssociatedTokenAccounts().Count());

// provision the ata
var builder = new TransactionBuilder();
builder
Expand Down
31 changes: 19 additions & 12 deletions test/Solana.Unity.Rpc.Test/TransactionBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ public class TransactionBuilderTest
"bnmzhen0yUOsH2zbbgICAgABDAIAAACAlpgAAAAAAAMBABVIZWxsbyBmcm9tIFNvbC5OZXQgOik=";

private const string ExpectedTransactionHashCreateInitializeAndMintTo =
"A5X22for3AxcX09IKX5Cbrpvv4k/1TcdTY2wf6vkq7Wcb/3fwMjA0vCshKkBG0EXQM2oKanIaQilKC/L" +
"KLmTYwc2yOVXu0TZCGwraCrxf4Pr8KpvTZZcUz/s4sls3VzGRqQmIhR3nXBR/O3\u002B4ZdICd8hYXb" +
"USqUBE\u002B4qCwpbC7gLlVo1ErARFL9csoTPvxA3/00wTxbs01sXlAH5t\u002ByAiwlan7B24Za3d" +
"CYydaczAOenGVU0nxBrz/gdFZgCJArZAAMABAdHaauXIEuoP7DK7hf3ho8eB05SFYGg2J2UN52qZbcXs" +
"k\u002BnIqdN4P6YFyTS64cak6Wd2hx9Qsbwf4gfPc5VPJvFTT4lvYz77q8imSqvzO/5qiFW9tKqfO4l5F" +
"KhFh6lZQsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAan1RcZLFxRIYzJTD1K8X9Y2u4Im6H9" +
"ROPb2YoAAAAABt324ddloZPZy\u002BFGzut5rBy0he1fWzeROoz1hX7/AKkFSlNQ\u002BF3IgtYUpVZye" +
"Iopbd8eq6vQpgZ4iEky9O72oOD/Y3arpTMrvjv2uP0ZD3LVkDTmRAfOpQ603IYXOGjCBgMCAAE0AAAAAGBN" +
"FgAAAAAAUgAAAAAAAAAG3fbh12Whk9nL4UbO63msHLSF7V9bN5E6jPWFfv8AqQUCAQRDAAJHaauXIEuoP7DK" +
"7hf3ho8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msHLSF7V9bN5E6jPWFfv8AqQYEAQIABQEBBgMCAQAJB6hh" +
"AAAAAAAABAEBEkhlbGxvIGZyb20gU29sLk5ldA==";

private const string Nonce = "2S1kjspXLPs6jpNVXQfNMqZzzSrKLbGdr9Fxap5h1DLN";

Expand Down Expand Up @@ -179,6 +180,11 @@ public void CreateInitializeAndMintToTest()
.AddInstruction(MemoProgram.NewMemo(initialAccount, "Hello from Sol.Net"))
.Build(new List<Account> { ownerAccount, mintAccount, initialAccount });

var tx2 = Transaction.Deserialize(tx);
var msg = tx2.CompileMessage();

Assert.IsTrue(tx2.Signatures[0].PublicKey.Verify(msg, tx2.Signatures[0].Signature));

Assert.AreEqual(ExpectedTransactionHashCreateInitializeAndMintTo, Convert.ToBase64String(tx));
}

Expand Down Expand Up @@ -253,5 +259,6 @@ public void TransactionBuilderAddSignatureTest()

Assert.AreEqual(AddSignatureTransaction, Convert.ToBase64String(tx));
}

}
}
Loading

0 comments on commit 9cd1137

Please sign in to comment.