Skip to content

Commit 9cd1137

Browse files
⚡ Align partial sign with Web3.js (#11)
Align partial sign with Web3.js version, including fix from the original Solnet codebase
1 parent 76a82d9 commit 9cd1137

File tree

6 files changed

+101
-89
lines changed

6 files changed

+101
-89
lines changed

SharedBuildProperties.props

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Product>Solana.Unity</Product>
5-
<Version>2.6.0.8</Version>
6-
<Copyright>Copyright 2022 &#169; Solana.Unity</Copyright>
7-
<Authors>garbles-dev</Authors>
8-
<PublisherName>garbles-dev</PublisherName>
9-
<RepositoryUrl>https://github.com/garbles-dev/Solana.Unity</RepositoryUrl>
5+
<Version>2.6.0.9</Version>
6+
<Copyright>Copyright 2022 &#169; Garbles Labs</Copyright>
7+
<Authors>Garbles Labs</Authors>
8+
<PublisherName>Garbles Labs</PublisherName>
9+
<RepositoryUrl>https://github.com/garbles-labs/Solana.Unity-Core</RepositoryUrl>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<LangVersion>latest</LangVersion>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1313
<PackageIcon>icon.png</PackageIcon>
14-
<PackageDescription>Solana.Unity is Solana's .NET standard 2.0 integration library.</PackageDescription>
14+
<PackageDescription>Solana.Unity-Core is Solana's .NET standard 2.0 integration library.</PackageDescription>
1515
<PackageTags>solana;solnet;sol;net2.0;unity;spl</PackageTags>
16-
<PackageReleaseNotes>https://github.com/garbles-dev/Solana.Unity/releases</PackageReleaseNotes>
16+
<PackageReleaseNotes>https://github.com/garbles-labs/Solana.Unity-Core/releases</PackageReleaseNotes>
1717
<RepositoryType>git</RepositoryType>
1818
</PropertyGroup>
1919

src/Solana.Unity.Rpc/Builders/MessageBuilder.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ internal byte[] Build()
109109

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

115115
CompiledInstruction compiledInstruction = new CompiledInstruction
116116
{
117-
ProgramIdIndex = (byte)FindAccountIndex(keysList, instruction.ProgramId),
117+
ProgramIdIndex = FindAccountIndex(keysList, instruction.ProgramId),
118118
KeyIndicesCount = ShortVectorEncoding.EncodeLength(keyCount),
119119
KeyIndices = keyIndices,
120120
DataLength = ShortVectorEncoding.EncodeLength(instruction.Data.Length),
@@ -180,8 +180,9 @@ internal byte[] Build()
180180
private List<AccountMeta> GetAccountKeys()
181181
{
182182
List<AccountMeta> newList = new();
183-
IList<AccountMeta> keysList = _accountKeysList.AccountList;
184-
int feePayerIndex = FindAccountIndex(keysList, FeePayer.KeyBytes);
183+
var keysList = _accountKeysList.AccountList;
184+
int feePayerIndex = Array.FindIndex<AccountMeta>( _accountKeysList.AccountList.ToArray(),
185+
x => x.PublicKey == FeePayer.Key);
185186

186187
if (feePayerIndex == -1)
187188
{
@@ -204,15 +205,26 @@ private List<AccountMeta> GetAccountKeys()
204205
/// <param name="accountMetas">The <see cref="AccountMeta"/>.</param>
205206
/// <param name="publicKey">The public key.</param>
206207
/// <returns>The index of the</returns>
207-
private static int FindAccountIndex(IList<AccountMeta> accountMetas, byte[] publicKey)
208+
private static byte FindAccountIndex(IList<AccountMeta> accountMetas, byte[] publicKey)
208209
{
209210
string encodedKey = Encoders.Base58.EncodeData(publicKey);
210-
for (int index = 0; index < accountMetas.Count; index++)
211+
return FindAccountIndex(accountMetas, encodedKey);
212+
}
213+
214+
/// <summary>
215+
/// Finds the index of the given public key in the accounts list.
216+
/// </summary>
217+
/// <param name="accountMetas">The <see cref="AccountMeta"/>.</param>
218+
/// <param name="publicKey">The public key.</param>
219+
/// <returns>The index of the</returns>
220+
private static byte FindAccountIndex(IList<AccountMeta> accountMetas, string publicKey)
221+
{
222+
for (byte index = 0; index < accountMetas.Count; index++)
211223
{
212-
if (accountMetas[index].PublicKey == encodedKey) return index;
224+
if (accountMetas[index].PublicKey == publicKey) return index;
213225
}
214226

215-
return -1;
227+
throw new Exception($"Something went wrong encoding this transaction. Account `{publicKey}` was not found among list of accounts. Should be impossible.");
216228
}
217229
}
218230
}

src/Solana.Unity.Rpc/Models/AccountKeysList.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,27 @@ internal class AccountKeysList
1717
/// <summary>
1818
/// Get the accounts as a list.
1919
/// </summary>
20-
internal IList<AccountMeta> AccountList
20+
internal List<AccountMeta> AccountList
2121
{
2222
get
2323
{
24-
List<AccountMeta> res = new(_accounts.Count);
25-
for (int i = 0; i < _accounts.Count; i++)
26-
{
27-
if (_accounts[i].IsSigner)
28-
{
29-
res.Add(_accounts[i]);
30-
}
31-
}
24+
List<AccountMeta> res = _accounts.Select(acc => acc).ToList();
3225

33-
for (int i = 0; i < _accounts.Count; i++)
26+
res.Sort((x, y) =>
3427
{
35-
if (!_accounts[i].IsSigner && _accounts[i].IsWritable)
28+
if (x.IsSigner != y.IsSigner)
3629
{
37-
res.Add(_accounts[i]);
30+
// Signers always come before non-signers
31+
return x.IsSigner ? -1 : 1;
3832
}
39-
}
40-
41-
for (int i = 0; i < _accounts.Count; i++)
42-
{
43-
if (!_accounts[i].IsSigner && !_accounts[i].IsWritable)
33+
if (x.IsWritable != y.IsWritable)
4434
{
45-
res.Add(_accounts[i]);
35+
// Writable accounts always come before read-only accounts
36+
return x.IsWritable ? -1 : 1;
4637
}
47-
}
38+
// Otherwise, sort by pubkey, stringwise.
39+
return x.PublicKey.CompareTo(y.PublicKey);
40+
});
4841

4942
return res;
5043
}

test/Solana.Unity.Extensions.Test/TokenWalletTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class TokenWalletTest
2121
{
2222

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

2727
private const string Blockhash = "5cZja93sopRB9Bkhckj5WzCxCaVyriv2Uh5fFDPDFFfj";
2828

@@ -146,7 +146,7 @@ public void TestProvisionAtaInjectBuilder()
146146
var testAccounts = wallet.TokenAccounts().WithMint("98mCaWvZYTmTHmimisaAQW4WGLphN1cWhcC7KtnZF819");
147147
Assert.AreEqual(1, testAccounts.Count());
148148
Assert.AreEqual(0, testAccounts.WhichAreAssociatedTokenAccounts().Count());
149-
149+
150150
// provision the ata
151151
var builder = new TransactionBuilder();
152152
builder

test/Solana.Unity.Rpc.Test/TransactionBuilderTest.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ public class TransactionBuilderTest
3434
"bnmzhen0yUOsH2zbbgICAgABDAIAAACAlpgAAAAAAAMBABVIZWxsbyBmcm9tIFNvbC5OZXQgOik=";
3535

3636
private const string ExpectedTransactionHashCreateInitializeAndMintTo =
37-
"A5X22for3AxcX09IKX5Cbrpvv4k/1TcdTY2wf6vkq7Wcb/3fwMjA0vCshKkBG0EXQM2oKanIaQilKC/L" +
38-
"KLmTYwc2yOVXu0TZCGwraCrxf4Pr8KpvTZZcUz/s4sls3VzGRqQmIhR3nXBR/O3\u002B4ZdICd8hYXb" +
39-
"USqUBE\u002B4qCwpbC7gLlVo1ErARFL9csoTPvxA3/00wTxbs01sXlAH5t\u002ByAiwlan7B24Za3d" +
40-
"CYydaczAOenGVU0nxBrz/gdFZgCJArZAAMABAdHaauXIEuoP7DK7hf3ho8eB05SFYGg2J2UN52qZbcXs" +
41-
"k\u002BnIqdN4P6YFyTS64cak6Wd2hx9Qsbwf4gfPc5VPJvFTT4lvYz77q8imSqvzO/5qiFW9tKqfO4l5F" +
42-
"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J4iilt3x6rq9CmBniISTL07vagBqfVFxksXFEhjMlMPUrxf1ja7gibof1E49v" +
44+
"ZigAAAAAG3fbh12Whk9nL4UbO63msHLSF7V9bN5E6jPWFfv8AqeD/Y3arpTMrvjv2uP0ZD3LV" +
45+
"kDTmRAfOpQ603IYXOGjCBgMCAAI0AAAAAGBNFgAAAAAAUgAAAAAAAAAG3fbh12Whk9nL4UbO6" +
46+
"3msHLSF7V9bN5E6jPWFfv8AqQYCAgVDAAJHaauXIEuoP7DK7hf3ho8eB05SFYGg2J2UN52qZb" +
47+
"cXsgFHaauXIEuoP7DK7hf3ho8eB05SFYGg2J2UN52qZbcXsgMCAAE0AAAAAPAdHwAAAAAApQA" +
48+
"AAAAAAAAG3fbh12Whk9nL4UbO63msHLSF7V9bN5E6jPWFfv8AqQYEAQIABQEBBgMCAQAJB6hh" +
49+
"AAAAAAAABAEBEkhlbGxvIGZyb20gU29sLk5ldA==";
4950

5051
private const string Nonce = "2S1kjspXLPs6jpNVXQfNMqZzzSrKLbGdr9Fxap5h1DLN";
5152

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

183+
var tx2 = Transaction.Deserialize(tx);
184+
var msg = tx2.CompileMessage();
185+
186+
Assert.IsTrue(tx2.Signatures[0].PublicKey.Verify(msg, tx2.Signatures[0].Signature));
187+
182188
Assert.AreEqual(ExpectedTransactionHashCreateInitializeAndMintTo, Convert.ToBase64String(tx));
183189
}
184190

@@ -253,5 +259,6 @@ public void TransactionBuilderAddSignatureTest()
253259

254260
Assert.AreEqual(AddSignatureTransaction, Convert.ToBase64String(tx));
255261
}
262+
256263
}
257264
}

0 commit comments

Comments
 (0)