Skip to content

Commit

Permalink
feat: implement Keypair.fromSecret() method that accepts secretKey, b…
Browse files Browse the repository at this point in the history
…yteArray and mnemonic
  • Loading branch information
beeman committed Nov 21, 2022
1 parent 8aa9de3 commit c9ba063
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 10 deletions.
66 changes: 66 additions & 0 deletions Runtime/Keypair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,37 @@ public static Keypair[] FromMnemonicSet(string mnemonic, int from = 0, int to =

return keypairs.ToArray();
}

/// <summary>
/// Returns an instance of Keypair from a mnemonic, byte array or secret key
/// </summary>
/// <param name="secret"></param>
/// <returns></returns>
public static Keypair FromSecret(string secret)
{
Keypair keypair;
if (IsMnemonic(secret))
{
keypair = FromMnemonic(secret);
}
else if (IsByteArray(secret))
{
keypair = ParseByteArray(secret);
}
else
{
keypair = FromSecretKey(secret);
}

// throw exception if keypair is null
if (keypair == null)
{
throw new System.Exception("Invalid secret");
}

return keypair;
}

/// <summary>
/// Returns an instance of Keypair from a secret key
/// </summary>
Expand Down Expand Up @@ -159,5 +189,41 @@ private static Keypair FromSeed(byte[] seed)
var privateKey = Ed25519.ExpandedPrivateKeyFromSeed(seed);
return FromSecretKey(Encoders.Base58.EncodeData(privateKey));
}

/// <summary>
/// Takes a string as input and checks if it is a valid mnemonic
/// </summary>
/// <param name="secret"></param>
/// <returns></returns>
private static bool IsMnemonic(string secret)
{
return secret.Split(' ').Length is 12 or 24;
}

/// <summary>
/// Takes a string as input and checks if it is a valid byte array
/// </summary>
/// <param name="secret"></param>
/// <returns></returns>
private static bool IsByteArray(string secret)
{
return secret.StartsWith('[') && secret.EndsWith(']');
}

/// <summary>
/// Takes a string as input and tries to parse it into a Keypair
/// </summary>
/// <param name="secret"></param>
/// <returns></returns>
private static Keypair ParseByteArray(string secret)
{
var parsed = secret
.Replace(",]", "]")
.Trim('[', ']')
.Split(',')
.Select(byte.Parse).ToArray();

return FromByteArray(parsed);
}
}
}
58 changes: 48 additions & 10 deletions Tests/Runtime/KeypairTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,85 @@ public void TestMnemonicGeneration24()
}


[Test]
public void TestCreateImportKeypairFromSecret()
{
var kp1 = Keypair.Random();

var byteArray = "[" + kp1.SecretKey.KeyBytes.Aggregate("", (current, b) => current + (b + ",")) + "]";
var fromByteArray = Keypair.FromSecret(byteArray);

Assert.AreEqual(fromByteArray.SecretKey, kp1.SecretKey);
Assert.AreEqual(fromByteArray.PublicKey, kp1.PublicKey);
Assert.AreEqual(fromByteArray.Mnemonic, null);

var fromMnemonic = Keypair.FromSecret(kp1.Mnemonic.ToString());
Assert.AreEqual(fromMnemonic.SecretKey, kp1.SecretKey);
Assert.AreEqual(fromMnemonic.PublicKey, kp1.PublicKey);
Assert.AreEqual(fromMnemonic.Mnemonic.ToString(), kp1.Mnemonic.ToString());

var fromSecretKey = Keypair.FromSecret(kp1.SecretKey.ToString());
Assert.AreEqual(fromSecretKey.SecretKey, kp1.SecretKey);
Assert.AreEqual(fromSecretKey.PublicKey, kp1.PublicKey);
Assert.AreEqual(fromSecretKey.Mnemonic, null);
}


[Test]
public void TestCreateImportKeypair()
{
var kp1 = Keypair.Random();
var kp2 = Keypair.FromSecretKey(kp1.SecretKey);
var kpSecret = Keypair.FromSecret(kp1.SecretKey);
Assert.AreEqual(kp1.SecretKey, kp2.SecretKey);
Assert.AreEqual(kp1.PublicKey, kp2.PublicKey);
Assert.AreEqual(kpSecret.PublicKey, kp2.PublicKey);
}

[Test]
public void TestImportFromByteArray()
{
var kp = Keypair.FromByteArray(KeypairFixture.TestSecretByteArray);
var kpSecret = Keypair.FromSecret("[" + KeypairFixture.TestSecretByteArray.Aggregate("", (current, b) => current + (b + ",")) + "]");
Assert.AreEqual(kp.PublicKey.ToString(), KeypairFixture.TestPublicKey);
Assert.AreEqual(kpSecret.PublicKey.ToString(), KeypairFixture.TestPublicKey);
}

[Test]
public void TestImportExistingSecretKey()
{
var kp = Keypair.FromSecretKey(KeypairFixture.TestSecretKey);
var kpSecret = Keypair.FromSecret(KeypairFixture.TestSecretKey);
Assert.AreEqual(kp.PublicKey.ToString(), KeypairFixture.TestPublicKey);
Assert.AreEqual(kpSecret.PublicKey.ToString(), KeypairFixture.TestPublicKey);
}

[Test]
public void TestImportMnemonic12()
{
var keypair = Keypair.FromMnemonic(KeypairFixture.TestMnemonic12);

Assert.AreEqual(keypair.PublicKey.ToString(), KeypairFixture.TestMnemonic12Keypair.PublicKey.ToString());
Assert.AreEqual(keypair.SecretKey.ToString(), KeypairFixture.TestMnemonic12Keypair.SecretKey.ToString());
Assert.AreEqual(keypair.Mnemonic.ToString(), KeypairFixture.TestMnemonic12Keypair.Mnemonic.ToString());
var kp = Keypair.FromMnemonic(KeypairFixture.TestMnemonic12);
var kpSecret = Keypair.FromSecret(KeypairFixture.TestMnemonic12);

Assert.AreEqual(kp.PublicKey.ToString(), KeypairFixture.TestMnemonic12Keypair.PublicKey.ToString());
Assert.AreEqual(kpSecret.PublicKey.ToString(), KeypairFixture.TestMnemonic12Keypair.PublicKey.ToString());
Assert.AreEqual(kp.SecretKey.ToString(), KeypairFixture.TestMnemonic12Keypair.SecretKey.ToString());
Assert.AreEqual(kpSecret.SecretKey.ToString(), KeypairFixture.TestMnemonic12Keypair.SecretKey.ToString());
Assert.AreEqual(kp.Mnemonic.ToString(), KeypairFixture.TestMnemonic12Keypair.Mnemonic.ToString());
Assert.AreEqual(kpSecret.Mnemonic.ToString(), KeypairFixture.TestMnemonic12Keypair.Mnemonic.ToString());
}

[Test]
public void TestImportMnemonic24()
{
var keypair = Keypair.FromMnemonic(KeypairFixture.TestMnemonic24);

Assert.AreEqual(keypair.PublicKey.ToString(), KeypairFixture.TestMnemonic24Keypair.PublicKey.ToString());
Assert.AreEqual(keypair.SecretKey.ToString(), KeypairFixture.TestMnemonic24Keypair.SecretKey.ToString());
Assert.AreEqual(keypair.Mnemonic.ToString(), KeypairFixture.TestMnemonic24Keypair.Mnemonic.ToString());
var kp = Keypair.FromMnemonic(KeypairFixture.TestMnemonic24);
var kpSecret = Keypair.FromSecret(KeypairFixture.TestMnemonic24);

Assert.AreEqual(kp.PublicKey.ToString(), KeypairFixture.TestMnemonic24Keypair.PublicKey.ToString());
Assert.AreEqual(kpSecret.PublicKey.ToString(), KeypairFixture.TestMnemonic24Keypair.PublicKey.ToString());
Assert.AreEqual(kp.SecretKey.ToString(), KeypairFixture.TestMnemonic24Keypair.SecretKey.ToString());
Assert.AreEqual(kpSecret.SecretKey.ToString(), KeypairFixture.TestMnemonic24Keypair.SecretKey.ToString());
Assert.AreEqual(kp.Mnemonic.ToString(), KeypairFixture.TestMnemonic24Keypair.Mnemonic.ToString());
Assert.AreEqual(kpSecret.Mnemonic.ToString(), KeypairFixture.TestMnemonic24Keypair.Mnemonic.ToString());
}

[Test]
Expand Down

0 comments on commit c9ba063

Please sign in to comment.