/// <summary> Gets public key from. </summary> /// <remarks> 19.09.2020. </remarks> /// <exception cref="ApplicationException"> Thrown when an Application error condition occurs. </exception> /// <param name="address"> The address. </param> /// <returns> An array of byte. </returns> public static byte[] GetPublicKeyFrom(string address) { int PUBLIC_KEY_LENGTH = 32; var pubkByteList = new List <byte>(); var bs58decoded = SimpleBase.Base58.Bitcoin.Decode(address).ToArray(); int len = bs58decoded.Length; if (len == 35) { byte[] ssPrefixed = { 0x53, 0x53, 0x35, 0x38, 0x50, 0x52, 0x45 }; pubkByteList.AddRange(ssPrefixed); pubkByteList.AddRange(bs58decoded.Take(PUBLIC_KEY_LENGTH + 1)); var blake2bHashed = HashExtension.Blake2(pubkByteList.ToArray(), 512); if (bs58decoded[PUBLIC_KEY_LENGTH + 1] != blake2bHashed[0] || bs58decoded[PUBLIC_KEY_LENGTH + 2] != blake2bHashed[1]) { throw new ApplicationException("Address checksum is wrong."); } return(bs58decoded.Skip(1).Take(PUBLIC_KEY_LENGTH).ToArray()); } throw new ApplicationException("Address checksum is wrong."); }
/// <summary> Gets address from. </summary> /// <remarks> 19.09.2020. </remarks> /// <param name="bytes"> The bytes. </param> /// <returns> The address from. </returns> public static string GetAddressFrom(byte[] bytes) { int SR25519_PUBLIC_SIZE = 32; int PUBLIC_KEY_LENGTH = 32; var plainAddr = Enumerable .Repeat((byte)0x2A, 35) .ToArray(); bytes.CopyTo(plainAddr.AsMemory(1)); var ssPrefixed = new byte[SR25519_PUBLIC_SIZE + 8]; var ssPrefixed1 = new byte[] { 0x53, 0x53, 0x35, 0x38, 0x50, 0x52, 0x45 }; ssPrefixed1.CopyTo(ssPrefixed, 0); plainAddr.AsSpan(0, SR25519_PUBLIC_SIZE + 1).CopyTo(ssPrefixed.AsSpan(7)); var blake2bHashed = HashExtension.Blake2(ssPrefixed, 0, SR25519_PUBLIC_SIZE + 8); plainAddr[1 + PUBLIC_KEY_LENGTH] = blake2bHashed[0]; plainAddr[2 + PUBLIC_KEY_LENGTH] = blake2bHashed[1]; var addrCh = SimpleBase.Base58.Bitcoin.Encode(plainAddr).ToArray(); return(new string(addrCh)); }
internal static UnCheckedExtrinsic SubmitExtrinsic(bool signed, Account account, Method method, Era era, uint nonce, uint tip, Hash genesis, Hash startEra) { var uncheckedExtrinsic = new UnCheckedExtrinsic(signed, account, method, era, nonce, tip, genesis, startEra); if (!signed) { return(uncheckedExtrinsic); } var payload = uncheckedExtrinsic.GetPayload().Encode(); /// Payloads longer than 256 bytes are going to be `blake2_256`-hashed. if (payload.Length > 256) { payload = HashExtension.Blake2(payload, 256); } byte[] signature; switch (account.KeyType) { case KeyType.SR25519: signature = Sr25519v091.SignSimple(account.PublicKey, account.PrivateKey, payload); break; case KeyType.ED25519: signature = Ed25519.Sign(payload, account.PrivateKey); break; default: throw new Exception($"Unknown key type found '{account.KeyType}'."); } uncheckedExtrinsic.AddPayloadSignature(signature); return(uncheckedExtrinsic); }