public async Task CreatedItemMatchesGetItem() { var bytes = new byte[] { 15, 97, 136, 0, 76, 187, 168, 28, 239, 85, 170, 23, 77, 81, 248, 159, }; var str = bytes.ToHexString(); var collectionId = await CreateTestAliceCollection(); var properties = Guid.NewGuid().ToByteArray(); var createItem = new CreateItem(collectionId, properties, new Address(Configuration.Alice.Address)); using var client = CreateClient(); var itemCreatedTask = new TaskCompletionSource <ItemCreated>(); client.ItemManagement.ItemCreated += (sender, created) => { if (created.Key.CollectionId == collectionId) { itemCreatedTask.SetResult(created); } }; client.ItemManagement.CreateItem(createItem, new Address(Configuration.Alice.Address), Configuration.Alice.PrivateKey); var key = (await itemCreatedTask.Task.WithTimeout(TimeSpan.FromSeconds(30))).Key; var item = client.ItemManagement.GetNftItem(key); Assert.Equal(properties, item !.Data); Assert.Equal(collectionId, item !.CollectionId); Assert.Equal(AddressUtils.GetPublicKeyFromAddr(Configuration.Alice.Address).Bytes, item !.Owner.Bytes); }
public async Task TransferFromChangesOwnerIfMadeByApprovedAccount() { var key = await CreateTestAliceItem(); using var client = CreateClient(); client.ItemManagement.Approve(new Approve(new Address(Configuration.Bob.Address), key), new Address(Configuration.Alice.Address), Configuration.Alice.PrivateKey); await WaitBlocks(2); client.ItemManagement.TransferFrom(new TransferFrom(new Address(Configuration.Alice.Address), new Address(Configuration.Charlie.Address), key, 0), new Address(Configuration.Bob.Address), Configuration.Bob.PrivateKey); await WaitBlocks(2); var item = client.ItemManagement.GetNftItem(key); Assert.Equal(AddressUtils.GetPublicKeyFromAddr(Configuration.Charlie.Address).Bytes, item !.Owner.Bytes); var approvedList = client.ItemManagement.GetApproved(key); if (approvedList != null) { Assert.DoesNotContain(approvedList.ApprovedAccounts, a => a.Bytes.ToHexString().Equals(AddressUtils.GetPublicKeyFromAddr(Configuration.Bob.Address).Bytes.ToHexString())); } }
public void Ok() { BigInteger seed = new BigInteger(DateTime.Now.Ticks) << DateTime.Now.Second; var rnd = new Random(BitConverter.ToInt32(seed.ToByteArray().Take(4).ToArray())); var publicKey = new byte[32]; var secretKey = new byte[64]; var payload = new byte[64]; rnd.NextBytes(publicKey); rnd.NextBytes(secretKey); rnd.NextBytes(payload); var pk = AddressUtils.GetPublicKeyFromAddr("5GWYBLjRtCQLXQmcyyRa6KaF1ihuqLjvVDE2gswJsEMxd9Qm"); var kp = new SR25519Keypair(pk.Bytes, secretKey); var sig = SR25519.Sign(payload, (ulong)payload.Length, kp); var arrayNotSame = new Func <byte[], bool>((btArr) => { return(btArr.GroupBy((i) => i).Count() > 50); }); Assert.True(sig.Length == 64); Assert.True(arrayNotSame(sig)); }
public string GetTypeEncoded() { var aupk = AddressUtils.GetPublicKeyFromAddr(Symbols); var str = BitConverter.ToString(aupk.Bytes).Replace("-", ""); return($"{Hash.GetStorageKey(Hasher.BLAKE2, aupk.Bytes)}{str}"); }
public TokensList?AddressTokens(AddressTokens addressTokens) { var publicKey = AddressUtils.GetPublicKeyFromAddr(addressTokens.Owner).Bytes; return(_nftClient.MakeCallWithReconnect(application => application.GetStorageObject <TokensList, DoubleMapKey <ulong, byte[]> >( DoubleMapKey.Create(addressTokens.CollectionId, publicKey), Module, AddressTokensStorage), _nftClient.Settings.MaxReconnectCount)); }
private async void SubmitRegisterProductExtrinsicAsync() { IsBusy = true; try { if (!App.IsPolkadotApiConnected) { App.ConnectToNode(); } _ = Task.Run(async() => { try { var ser = PolkadotApi.Serializer; var sender = new Address("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"); var pub = AddressUtils.GetPublicKeyFromAddr(sender); var secret = "0x33A6F3093F158A7109F679410BEF1A0C54168145E0CECB4DF006C1C2FFFB1F09925A225D97AA00682D6A59B95B18780C10D7032336E88F3442B42361F4A66011"; var encodedExtrinsic = ser.Serialize(new RegisterProductCall( new Identifier("00012345678905"), pub, //Empty.Instance //OneOf.OneOf<Empty, ProductPropertyList>.FromT0(Empty.Instance) new ProductPropertyList(new[] { new ProductProperty( new Identifier("name"), new Identifier("Chocolate frog") ) }) ) ); Trace.WriteLine(encodedExtrinsic.ToPrefixedHexString()); var tcs = new TaskCompletionSource <string>(); var sid = PolkadotApi.SubmitAndSubcribeExtrinsic(encodedExtrinsic, "ProductRegistry", "register_product", sender, secret, str => tcs.SetResult(str)); Trace.WriteLine(sid); var result = await tcs.Task.WithTimeout(TimeSpan.FromMinutes(1)).ConfigureAwait(false); PolkadotApi.UnsubscribeStorage(sid); Trace.WriteLine(result); } catch (Exception ex) { Trace.WriteLine(ex); } }); } catch (System.Exception ex) { Trace.WriteLine(ex); } finally { IsBusy = false; } }
public async Task GetOwnerReturnsItCreatorAfterCreation() { var key = await CreateTestAliceItem(); using var client = CreateClient(); var owner = client.ItemManagement.GetOwner(key); Assert.Equal(AddressUtils.GetPublicKeyFromAddr(Configuration.Alice.Address).Bytes, owner !.Bytes); }
private void SubmitTransferExtrinsic() { IsBusy = true; try { if (!App.IsPolkadotApiConnected) { App.ConnectToNode(); } _ = Task.Run(async() => { try { var sender = new Address("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"); //var pub = AddressUtils.GetPublicKeyFromAddr(sender); var secret = "0x33A6F3093F158A7109F679410BEF1A0C54168145E0CECB4DF006C1C2FFFB1F09925A225D97AA00682D6A59B95B18780C10D7032336E88F3442B42361F4A66011"; var recipient = "5Ef1wcrhb5CVyZjpdYh9Keg81tgUPcsYi9uhNHC9uqjo7956"; var pub = AddressUtils.GetPublicKeyFromAddr(recipient); var compactAmount = Scale.EncodeCompactInteger(BigInteger.Parse("10")); var buf = new byte[pub.Bytes.Length + compactAmount.Bytes.Length]; pub.Bytes.CopyTo(buf.AsMemory()); compactAmount.Bytes.CopyTo(buf.AsMemory(pub.Bytes.Length)); Trace.WriteLine("Transfer Buf: " + buf.ToPrefixedHexString()); var call = new TransferCall(pub, BigInteger.Parse("10")); var encodedCall = PolkadotApi.Serializer.Serialize(call); Trace.WriteLine("Transfer Call: " + encodedCall.ToPrefixedHexString()); var tcs = new TaskCompletionSource <string>(); var sid = PolkadotApi.SubmitAndSubcribeExtrinsic(encodedCall, "balances", "transfer", sender, secret, str => tcs.SetResult(str)); Trace.WriteLine(sid); var result = await tcs.Task.WithTimeout(TimeSpan.FromSeconds(30)); PolkadotApi.UnsubscribeStorage(sid); Trace.WriteLine(result); } catch (Exception ex) { Trace.WriteLine(ex); } }); } catch (System.Exception ex) { Trace.WriteLine(ex); } finally { IsBusy = false; } }
public void Create() { if (_savingInProgress) { return; } _savingInProgress = true; foreach (var validator in AllValidators()) { validator.Validate(); } if (AllValidators().SelectMany(v => v.ValidationErrors).Any()) { return; } var size = (uint)CustomDataSize(); var address = GameSettings.Address; var publicKey = AddressUtils.GetPublicKeyFromAddr(address); var fieldsSchema = _fieldScripts .Select(f => new NftFieldSchema(f.FieldName, f.FieldSize ?? 0)) .ToList(); void Cleanup() { NftClient.CollectionManagement.CollectionCreated -= Handler; _updateQueue.Enqueue(() => onCloseCollection?.Invoke()); _savingInProgress = false; } void Handler(object sender, Created created) { if (publicKey.Bytes.SequenceEqual(created.Account.Bytes)) { var schema = new NftCollectionSchema(fieldsSchema, created.Id); schema.Save(); Cleanup(); } } NftClient.CollectionManagement.CollectionCreated += Handler; var createCollection = new CreateCollection(nameField.text, descriptionField.text, tokenPrefixField.text, new CollectionMode(new Nft(size))); NftClient.CollectionManagement.CreateCollection(createCollection, new Address(address), GameSettings.PrivateKey); }
public void Serialize(Stream stream, object value, IBinarySerializer serializer, object[] parameters) { var address = (Address)value; if (address == null) { return; } var key = AddressUtils.GetPublicKeyFromAddr(address).Bytes; serializer.Serialize(key, stream); }
public async Task TransferFromFailsForNotApprovedAccount() { var key = await CreateTestAliceItem(); using var client = CreateClient(); client.ItemManagement.TransferFrom(new TransferFrom(new Address(Configuration.Alice.Address), new Address(Configuration.Charlie.Address), key, 0), new Address(Configuration.Bob.Address), Configuration.Bob.PrivateKey); await WaitBlocks(2); var item = client.ItemManagement.GetNftItem(key); Assert.Equal(AddressUtils.GetPublicKeyFromAddr(Configuration.Alice.Address).Bytes, item !.Owner.Bytes); }
static void Main(string[] args) { // This is a manual test due to lack of test DOTs if (args.Length < 4) { Console.WriteLine("This is intended to be a manual test"); Console.WriteLine("Usage: "); Console.WriteLine("<sender address> <recipient address> <amount in fDOTs> <sender private key (hex)>"); Console.WriteLine("success"); } else { var senderAddr = args[0]; var recipientAddr = args[1]; var amountStr = args[2]; var senderPrivateKeyStr = args[3]; using (IApplication app = PolkaApi.GetAppication()) { // Receiving address public key var pk = AddressUtils.GetPublicKeyFromAddr(recipientAddr); // Compact-encode amount var compactAmount = Scale.EncodeCompactInteger(BigInteger.Parse(amountStr)); var buf = new byte[pk.Bytes.Length + compactAmount.Bytes.Length]; pk.Bytes.CopyTo(buf.AsMemory()); compactAmount.Bytes.CopyTo(buf.AsMemory(pk.Bytes.Length)); app.Connect(); var exHash = app.SubmitExtrinsic(buf, "balances", "transfer", new Address(senderAddr), senderPrivateKeyStr); Console.WriteLine($"Sent extrinsic with hash: {exHash} "); Console.WriteLine("Now let's try to cancel it... "); try { app.RemoveExtrinsic(exHash); } catch (ApplicationException) { Console.WriteLine("Yeah, looks like canceling is not yet supported"); } app.Disconnect(); } } }
public async Task ApproveAddsAccountToApprovedList() { var key = await CreateTestAliceItem(); using var client = CreateClient(); var publicKey2 = AddressUtils.GetPublicKeyFromAddr(Configuration.Bob.Address); var approveListBefore = client.ItemManagement.GetApproved(key); if (approveListBefore != null) { Assert.DoesNotContain(approveListBefore !.ApprovedAccounts, a => a.Bytes.ToHexString().Equals(publicKey2.Bytes.ToHexString())); } client.ItemManagement.Approve(new Approve(new Address(Configuration.Bob.Address), key), new Address(Configuration.Alice.Address), Configuration.Alice.PrivateKey); await WaitBlocks(2); var approveList = client.ItemManagement.GetApproved(key); Assert.Contains(approveList !.ApprovedAccounts, a => a.Bytes.ToHexString().Equals(publicKey2.Bytes.ToHexString())); }
static void Main(string[] args) { // This is a manual test due to lack of test DOTs //if (args.Length < 4) //{ // Console.WriteLine("This is intended to be a manual test"); // Console.WriteLine("Usage: "); // Console.WriteLine("<sender address> <recipient address> <amount in fDOTs> <sender private key (hex)>"); // Console.WriteLine("success"); //} //else //{ var senderAddr = "5GuuxfuxbvaiwteUrV9U7Mj2Fz7TWK84WhLaZdMMJRvSuzr4"; var recipientAddr = "5HQdHxuPgQ1BpJasmm5ZzfSk5RDvYiH6YHfDJVE8jXmp4eig"; var amountStr = "1000000000000"; var senderPrivateKeyStr = "0xa81056d713af1ff17b599e60d287952e89301b5208324a0529b62dc7369c745defc9c8dd67b7c59b201bc164163a8978d40010c22743db142a47f2e064480d4b"; using (IApplication app = PolkaApi.GetAppication()) { // Receiving address public key var pk = AddressUtils.GetPublicKeyFromAddr(recipientAddr); // Compact-encode amount var compactAmount = Scale.EncodeCompactInteger(BigInteger.Parse(amountStr)); var buf = new byte[pk.Bytes.Length + compactAmount.Bytes.Length]; pk.Bytes.CopyTo(buf.AsMemory()); compactAmount.Bytes.CopyTo(buf.AsMemory(pk.Bytes.Length)); app.Connect(); var result = app.SubmitExtrinsic(buf, "balances", "transfer", new Address(senderAddr), senderPrivateKeyStr); Console.WriteLine(result); app.Disconnect(); } // } }
static void Main(string[] args) { // This is a manual test due to lack of test DOTs if (args.Length < 4) { Console.WriteLine("This is intended to be a manual test"); Console.WriteLine("Usage: "); Console.WriteLine("<sender address> <recipient address> <amount in fDOTs> <sender private key (hex)>"); Console.WriteLine("success"); } else { var senderAddr = args[0]; var recipientAddr = args[1]; var amountStr = args[2]; var senderPrivateKeyStr = args[3]; using (IApplication app = PolkaApi.GetAppication()) { // Receiving address public key var pk = AddressUtils.GetPublicKeyFromAddr(recipientAddr); // Compact-encode amount var compactAmount = Scale.EncodeCompactInteger(BigInteger.Parse(amountStr)); var buf = new byte[pk.Bytes.Length + compactAmount.Bytes.Length]; pk.Bytes.CopyTo(buf.AsMemory()); compactAmount.Bytes.CopyTo(buf.AsMemory(pk.Bytes.Length)); app.Connect(); var result = app.SubmitExtrinsic(buf, "balances", "transfer", new Address(senderAddr), senderPrivateKeyStr); Console.WriteLine(result); app.Disconnect(); } } }
public ulong?BalanceOf(GetBalanceOf getBalanceOf) { return(_nftClient.MakeCallWithReconnect(application => application.GetStorageObject <ulong?, DoubleMapKey <ulong, byte[]> >(new DoubleMapKey <ulong, byte[]>(getBalanceOf.CollectionId, AddressUtils.GetPublicKeyFromAddr(getBalanceOf.Account).Bytes), Module, BalanceStorage), _nftClient.Settings.MaxReconnectCount)); }