Skip to content
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
140 changes: 140 additions & 0 deletions src/Solnet.Programs/AddressLookupTableProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Collections.Generic;
using Solnet.Rpc.Models;
using Solnet.Wallet;

namespace Solnet.Programs
{
public static class AddressLookupTableProgram

Check warning on line 7 in src/Solnet.Programs/AddressLookupTableProgram.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'AddressLookupTableProgram'

Check warning on line 7 in src/Solnet.Programs/AddressLookupTableProgram.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'AddressLookupTableProgram'
{
/// <summary>
/// The public key of the ATL Program.
/// </summary>
public static readonly PublicKey ProgramIdKey = new("AddressLookupTab1e1111111111111111111111111");

/// <summary>
/// The program's name.
/// </summary>
private const string ProgramName = "Address Lookup Table Program";

/// <summary>
/// Create New Address Lookup Table Instruction
/// </summary>
/// <param name="Authority"></param>
/// <param name="Payer"></param>
/// <param name="RecentSlot"></param>
/// <returns></returns>
public static TransactionInstruction CreateAddressLookupTable(
PublicKey Authority, PublicKey Payer, PublicKey ALT,byte bump, ulong RecentSlot)

Check warning on line 27 in src/Solnet.Programs/AddressLookupTableProgram.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ALT' has no matching param tag in the XML comment for 'AddressLookupTableProgram.CreateAddressLookupTable(PublicKey, PublicKey, PublicKey, byte, ulong)' (but other parameters do)

Check warning on line 27 in src/Solnet.Programs/AddressLookupTableProgram.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'bump' has no matching param tag in the XML comment for 'AddressLookupTableProgram.CreateAddressLookupTable(PublicKey, PublicKey, PublicKey, byte, ulong)' (but other parameters do)

Check warning on line 27 in src/Solnet.Programs/AddressLookupTableProgram.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ALT' has no matching param tag in the XML comment for 'AddressLookupTableProgram.CreateAddressLookupTable(PublicKey, PublicKey, PublicKey, byte, ulong)' (but other parameters do)
{
//byte[] recentSlotBytes = BitConverter.GetBytes(RecentSlot);
//byte[] seed = Authority.KeyBytes.Concat(recentSlotBytes).ToArray();
//PublicKey.TryFindProgramAddress(new List<byte[]> { seed }, ProgramIdKey, out PublicKey ALTaddress, out byte bump);

List<AccountMeta> keys = new()
{
AccountMeta.Writable(ALT, false),
AccountMeta.ReadOnly(Authority, false),
AccountMeta.Writable(Payer, true),
AccountMeta.ReadOnly(SystemProgram.ProgramIdKey, false)
};
return new TransactionInstruction
{
ProgramId = ProgramIdKey.KeyBytes,
Keys = keys,
Data = AddressLookupTableProgramData.EncodeCreateAddressLookupTableData(RecentSlot, bump)
};
}

/// <summary>
/// Freeze Lookup Table Instruction
/// </summary>
/// <param name="LookupTable"></param>
/// <param name="Authority"></param>
/// <returns></returns>
public static TransactionInstruction FreezeLookupTable(PublicKey LookupTable, PublicKey Authority)
{
List<AccountMeta> keys = new()
{
AccountMeta.Writable(LookupTable, false),
AccountMeta.ReadOnly(Authority, true)
};
return new TransactionInstruction
{
ProgramId = ProgramIdKey.KeyBytes,
Keys = keys,
Data = AddressLookupTableProgramData.EncodeFreezeLookupTableData()
};
}

/// <summary>
/// Extend Lookup Table Instruction
/// </summary>
/// <param name="LookupTable"></param>
/// <param name="Authority"></param>
/// <param name="Payer"></param>
/// <param name="keys"></param>
/// <returns></returns>
public static TransactionInstruction ExtendLookupTable(PublicKey LookupTable, PublicKey Authority, PublicKey Payer, List<PublicKey> keys)
{
List<AccountMeta> meta = new()
{
AccountMeta.Writable(LookupTable, false),
AccountMeta.ReadOnly(Authority, true),
AccountMeta.Writable(Payer, true),
AccountMeta.ReadOnly(SystemProgram.ProgramIdKey, false)
};
return new TransactionInstruction
{
ProgramId = ProgramIdKey.KeyBytes,
Keys = meta,
Data = AddressLookupTableProgramData.EncodeExtendLookupTableData((ulong)keys.Count, keys)
};
}

/// <summary>
/// Deactivate Lookup Table Instruction
/// </summary>
/// <param name="LookupTable"></param>
/// <param name="Authority"></param>
/// <returns></returns>
public static TransactionInstruction DeactivateLookupTable(PublicKey LookupTable, PublicKey Authority)
{
List<AccountMeta> keys = new()
{
AccountMeta.Writable(LookupTable, false),
AccountMeta.ReadOnly(Authority, true)
};
return new TransactionInstruction
{
ProgramId = ProgramIdKey.KeyBytes,
Keys = keys,
Data = AddressLookupTableProgramData.EncodeDeactivateLookupTableData()
};
}

/// <summary>
/// Close Lookup Table Instruction
/// </summary>
/// <param name="LookupTable"></param>
/// <param name="Authority"></param>
/// <param name="Recipient"></param>
/// <returns></returns>
public static TransactionInstruction CloseLookupTable(PublicKey LookupTable, PublicKey Authority, PublicKey Recipient)
{
List<AccountMeta> keys = new()
{
AccountMeta.Writable(LookupTable, false),
AccountMeta.ReadOnly(Authority, true),
AccountMeta.Writable(Recipient, false)
};
return new TransactionInstruction
{
ProgramId = ProgramIdKey.KeyBytes,
Keys = keys,
Data = AddressLookupTableProgramData.EncodeCloseLookupTableData()
};
}


}
}
79 changes: 79 additions & 0 deletions src/Solnet.Programs/AddressLookupTableProgramData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections.Generic;
using Solnet.Programs.Utilities;
using Solnet.Wallet;

namespace Solnet.Programs
{
internal static class AddressLookupTableProgramData
{
internal const int MethodOffset = 0;

/// <summary>
/// Encode transaction instruction data for the <see cref="AddressLookupTableProgramInstruction.Values.CreateLookupTable"/> method.
/// </summary>
/// <param name="Authority">Who own this table </param>

Check warning on line 14 in src/Solnet.Programs/AddressLookupTableProgramData.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has a param tag for 'Authority', but there is no parameter by that name
/// <param name="Payer">Who pay for this table</param>

Check warning on line 15 in src/Solnet.Programs/AddressLookupTableProgramData.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has a param tag for 'Payer', but there is no parameter by that name
/// <param name="RecentSlot">For random seed</param>
/// <returns></returns>
internal static byte[] EncodeCreateAddressLookupTableData( ulong RecentSlot,byte bump)

Check warning on line 18 in src/Solnet.Programs/AddressLookupTableProgramData.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'bump' has no matching param tag in the XML comment for 'AddressLookupTableProgramData.EncodeCreateAddressLookupTableData(ulong, byte)' (but other parameters do)
{
byte[] data = new byte[13];
data.WriteU32((uint)AddressLookupTableProgramInstruction.Values.CreateLookupTable, MethodOffset);
data.WriteU64(RecentSlot, 4);
data.WriteU8(bump, 12);

return data;
}

/// <summary>
/// Encode transaction instruction data for the <see cref="AddressLookupTableProgramInstruction.Values.FreezeLookupTable"/> method.
/// </summary>
/// <returns></returns>
internal static byte[] EncodeFreezeLookupTableData()
{
byte[] data = new byte[4];
data.WriteU32((uint)AddressLookupTableProgramInstruction.Values.FreezeLookupTable, MethodOffset);
return data;
}

/// <summary>
/// Encode transaction instruction data for the <see cref="AddressLookupTableProgramInstruction.Values.ExtendLookupTable"/> method.
/// </summary>
/// <param name="RecentSlot"></param>

Check warning on line 42 in src/Solnet.Programs/AddressLookupTableProgramData.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has a param tag for 'RecentSlot', but there is no parameter by that name
/// <param name="Keys"></param>
/// <returns></returns>
internal static byte[] EncodeExtendLookupTableData(ulong KeyCounts,List<PublicKey> Keys)

Check warning on line 45 in src/Solnet.Programs/AddressLookupTableProgramData.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'KeyCounts' has no matching param tag in the XML comment for 'AddressLookupTableProgramData.EncodeExtendLookupTableData(ulong, List<PublicKey>)' (but other parameters do)
{
byte[] data = new byte[12 + Keys.Count * 32];
data.WriteU32((uint)AddressLookupTableProgramInstruction.Values.ExtendLookupTable, MethodOffset);
data.WriteU64(KeyCounts, 4);
for (int i = 0; i < Keys.Count; i++)
{
data.WritePubKey(Keys[i], 12 + i * 32);
}
return data;
}

/// <summary>
/// Encode transaction instruction data for the <see cref="AddressLookupTableProgramInstruction.Values.DeactivateLookupTable"/> method.
/// </summary>
/// <returns></returns>
internal static byte[] EncodeDeactivateLookupTableData()
{
byte[] data = new byte[4];
data.WriteU32((uint)AddressLookupTableProgramInstruction.Values.DeactivateLookupTable, MethodOffset);
return data;
}

/// <summary>
/// Encode transaction instruction data for the <see cref="AddressLookupTableProgramInstruction.Values.CloseLookupTable"/> method.
/// </summary>
/// <returns></returns>
internal static byte[] EncodeCloseLookupTableData()
{
byte[] data = new byte[4];
data.WriteU32((uint)AddressLookupTableProgramInstruction.Values.CloseLookupTable, MethodOffset);
return data;
}
}
}
31 changes: 31 additions & 0 deletions src/Solnet.Programs/AddressLookupTableProgramInstruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;

namespace Solnet.Programs
{
internal static class AddressLookupTableProgramInstruction
{
/// <summary>
/// Represents the user-friendly names for the instruction types for the <see cref="AddressLookupTableProgram"/>.
/// </summary>
internal static readonly Dictionary<Values, string> Names = new()
{
{ Values.CreateLookupTable, "Create Lookup Table" },
{ Values.FreezeLookupTable, "Freeze Lookup Table" },
{ Values.ExtendLookupTable, "Extend Lookup Table" },
{ Values.DeactivateLookupTable, "Deactivate Lookup Table" },
{ Values.CloseLookupTable, "Close Lookup Table" }
};

/// <summary>
/// Represents the instruction types for the <see cref="AddressLookupTableProgram"/>.
/// </summary>
internal enum Values : byte
{
CreateLookupTable = 0,
FreezeLookupTable = 1,
ExtendLookupTable = 2,
DeactivateLookupTable =3,
CloseLookupTable =4
}
}
}
8 changes: 4 additions & 4 deletions src/Solnet.Rpc/Solnet.Rpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Solnet.Rpc.Test</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Solnet.Extensions.Test</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Solnet.Extensions.Test</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
Expand Down
31 changes: 26 additions & 5 deletions src/Solnet.Wallet/Account.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Bifrost.Security;
using Solnet.Wallet.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Principal;

namespace Solnet.Wallet
{
Expand All @@ -27,9 +29,8 @@ public class Account
public Account()
{
byte[] seed = GenerateRandomSeed();

(byte[] privateKey, byte[] publicKey) = Utils.EdKeyPairFromSeed(seed);

byte[] privateKey = Ed25519.ExpandedPrivateKeyFromSeed(seed);
byte[] publicKey = Ed25519.PublicKeyFromSeed(seed);
PrivateKey = new PrivateKey(privateKey);
PublicKey = new PublicKey(publicKey);
}
Expand Down Expand Up @@ -64,7 +65,7 @@ public static Account FromSecretKey(string secretKey)
throw new ArgumentException("Not a secret key");
}

Account acc = new Account(skeyBytes, skeyBytes.Slice(32, 64));
Account acc = new Account(skeyBytes, skeyBytes.AsSpan(32, 32).ToArray());

return acc;
}
Expand Down Expand Up @@ -121,5 +122,25 @@ public override bool Equals(object obj)

/// <inheritdoc cref="GetHashCode"/>
public override int GetHashCode() => PublicKey.GetHashCode();

public static List<Account> ImportMany(List<string> Keys)
{
List<Account> accounts = new List<Account>();
foreach (string key in Keys)
{
accounts.Add(FromSecretKey(key));
}
return accounts;
}

public static List<Account> ImportMany(List<byte[]> Keys)
{
List<Account> accounts = new List<Account>();
foreach (byte[] key in Keys)
{
accounts.Add(new Account(key, key.AsSpan(32, 32).ToArray()));
}
return accounts;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Solnet.Wallet/Bip39/HardcodedWordListSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static HardcodedWordlistSource()
/// <returns>A task which returns the word list.</returns>
public Task<WordList> LoadAsync(string name)
{
string list = WordLists.TryGet(name);
WordLists.TryGetValue(name,out string list);
if (list == null)
return null;
return Task.FromResult(new WordList(list.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries),
Expand All @@ -78,4 +78,4 @@ public Task<WordList> LoadAsync(string name)

#endregion
}
}
}
9 changes: 3 additions & 6 deletions src/Solnet.Wallet/Bip39/Mnemonic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Solnet.Wallet.Utilities;
using System;
using System.Collections;
Expand Down Expand Up @@ -63,7 +60,7 @@ private Mnemonic(WordList wordList, byte[] entropy = null)
throw new ArgumentException("The length for entropy should be " + string.Join(",", EntArray) + " bits", nameof(entropy));

int cs = CsArray[i];
byte[] checksum = Utils.Sha256(entropy);
byte[] checksum = SHA256.HashData(entropy);
BitWriter entropyResult = new();

entropyResult.Write(entropy);
Expand Down Expand Up @@ -135,7 +132,7 @@ public bool IsValidChecksum
BitArray bits = WordList.ToBits(Indices);
writer.Write(bits, ent);
byte[] entropy = writer.ToBytes();
byte[] checksum = Utils.Sha256(entropy);
byte[] checksum = SHA256.HashData(entropy);

writer.Write(checksum, cs);
int[] expectedIndices = writer.ToIntegers();
Expand Down Expand Up @@ -286,4 +283,4 @@ public override string ToString()
return _mnemonic;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Solnet.Wallet/Bip39/WordList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private static async Task<WordList> LoadWordList(string name)
if (result != null)
lock (LoadedLists)
{
LoadedLists.AddOrReplace(name, result);
LoadedLists[name] = result;
}

return result;
Expand Down Expand Up @@ -429,4 +429,4 @@ public static BitArray ToBits(int[] values)
return result;
}
}
}
}
Loading
Loading