Skip to content

Commit b7fe839

Browse files
committed
add listall and delete all for kvStore
1 parent 93eef89 commit b7fe839

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

.dart_tool/package_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@
656656
"languageVersion": "3.2"
657657
}
658658
],
659-
"generated": "2024-04-16T10:54:09.162011Z",
659+
"generated": "2024-04-18T09:13:15.973441Z",
660660
"generator": "pub",
661661
"generatorVersion": "3.3.3"
662662
}

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cSpell.words": ["keypair", "polkadot", "tfchain"]
2+
"cSpell.words": ["keypair", "polkadot", "sublist", "tfchain", "TFKV", "twoxx"]
33
}

packages/tfchain_client/bin/tfchain_client.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ void main() async {
2424
"sr25519");
2525
await client.connect();
2626

27-
// final extrinsic =
28-
// client.kvStrore.set(KVStoreSetOptions(key: "mnemonic", value: "value"));
2927
// await client.apply(extrinsic);
3028
client.kvStrore.list();
3129

packages/tfchain_client/lib/src/client.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ class Client extends QueryClient {
9898
checkInputs();
9999
if (keypairType == "sr25519") {
100100
keypair = await signer.fromMnemonic(mnemonic, Signer.KPType.sr25519);
101-
print("ADDRESS ${keypair!.address}");
102101
address = keypair!.address;
103102
} else {
104103
keypair = await signer.fromMnemonic(mnemonic, Signer.KPType.ed25519);
@@ -140,6 +139,10 @@ class Client extends QueryClient {
140139
final encodedCall = runtimeCall.encode();
141140
final nonce = await api.rpc.system.accountNextIndex(keyring.address);
142141

142+
// await api.rpc.state.queryStorageAt(keys)
143+
// state vs systemApi
144+
// different naming between polkadot vs flutter
145+
// how to make rpc call on state
143146
final payloadToSign = SigningPayload(
144147
method: encodedCall,
145148
specVersion: specVersion,

packages/tfchain_client/lib/src/kvstore.dart

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import 'dart:convert';
2-
3-
import 'package:tfchain_client/generated/dev/dev.dart';
4-
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime.dart';
1+
import 'dart:typed_data';
2+
import 'package:polkadart/polkadart.dart';
53
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime_call.dart';
64
import 'package:tfchain_client/models/kvstore.dart';
75
import 'package:tfchain_client/tfchain_client.dart';
@@ -25,16 +23,53 @@ class KVStore {
2523

2624
Future<String> get(KVStoreGetOptions options) async {
2725
final res = await client.api.query.tFKVStore
28-
.tFKVStore(client.keypair!.publicKey.bytes, options.key.codeUnits);
26+
.tFKVStore(client.keypair!.publicKey.bytes, []);
27+
print(res);
2928
return String.fromCharCodes(res);
3029
}
3130

32-
void list() async {
33-
// final queries = client.api.query.
34-
// final res = await queries.tFKVStore.
31+
Future<Map<String, String>> list() async {
32+
final moduleHash =
33+
Hasher.twoxx128.hash(Uint8List.fromList('TFKVStore'.codeUnits));
34+
35+
final functionHash =
36+
Hasher.twoxx128.hash(Uint8List.fromList('TFKVStore'.codeUnits));
37+
38+
final keyHash = Hasher.blake2b128
39+
.hash(Uint8List.fromList(client.keypair!.publicKey.bytes));
40+
41+
Uint8List partialKey = Uint8List.fromList([
42+
...moduleHash,
43+
...functionHash,
44+
...keyHash,
45+
...client.keypair!.publicKey.bytes
46+
]);
47+
48+
final keys = await client.api.rpc.state.getKeys(key: partialKey);
49+
50+
final keysValues = await client.api.rpc.state.queryStorageAt(keys);
51+
52+
Map<String, String> keyValueMap = {};
53+
54+
for (var change in keysValues[0].changes) {
55+
final key = change.key;
56+
var value = change.value;
57+
58+
final added = key.sublist(partialKey.length + 17);
59+
60+
value = value!.sublist(1);
61+
keyValueMap[String.fromCharCodes(added)] = String.fromCharCodes(value);
62+
}
63+
64+
return keyValueMap;
3565
}
3666

37-
void deleteAll(){
67+
void deleteAll() async {
68+
Map<String, String> keys = await list();
3869

70+
for (String key in keys.keys) {
71+
final extrinsic = delete(KVStoreGetOptions(key: key));
72+
await client.apply(extrinsic);
73+
}
3974
}
4075
}

packages/tfchain_client/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
moment_dart: ^2.0.2
1616
signer:
1717
path: ../signer
18+
hashlib: ^1.13.1
1819

1920
dev_dependencies:
2021
lints: ^3.0.0

0 commit comments

Comments
 (0)