From a455450e0a74b28dba815963b38f019256c21885 Mon Sep 17 00:00:00 2001 From: Rakshith G Date: Fri, 12 Jun 2020 16:07:12 +0530 Subject: [PATCH] tezosNodeReader dev --- example/lib/main.dart | 151 +++++++++++++++++++++++++++++------ example/pubspec.lock | 21 +++++ lib/tezster_dart.dart | 177 +++++++++++++++++++++++++++++++++++++++++- pubspec.lock | 21 +++++ pubspec.yaml | 3 +- 5 files changed, 348 insertions(+), 25 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 00d0e5a..d25f1e7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -17,56 +17,161 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - example() async { - //Generate mnemonic - String mnemonic = TezsterDart.generateMnemonic(); // strength is optional, by default it's 256 ==> Generates 24 words. + //Generate mnemonic + String mnemonic = TezsterDart + .generateMnemonic(); // strength is optional, by default it's 256 ==> Generates 24 words. print("mnemonic ===> $mnemonic"); - //mnemonic ===> 24 random words, [If strength parameter is changed the words length differs.] - - //Generate keys from mnemonic + //mnemonic ===> 24 random words, [If strength parameter is changed the words length differs.] + + //Generate keys from mnemonic List keys = await TezsterDart.getKeysFromMnemonic( - mnemonic: "luxury bulb roast timber sense stove sugar sketch goddess host meadow decorate gather salmon funny person canoe daring machine network camp moment wrong dice", + mnemonic: + "luxury bulb roast timber sense stove sugar sketch goddess host meadow decorate gather salmon funny person canoe daring machine network camp moment wrong dice", ); print("keys ===> $keys"); - //keys ===> [privateKey, publicKey, publicKeyHash] - //Accessing: private key ===> keys[0] | public key ===> keys[1] | public Key Hash ===> identity[2] all of type string - - //Create / Unlock identity from mnemonic and passphrase. + //keys ===> [privateKey, publicKey, publicKeyHash] + //Accessing: private key ===> keys[0] | public key ===> keys[1] | public Key Hash ===> identity[2] all of type string + + //Create / Unlock identity from mnemonic and passphrase. List identity = await TezsterDart.getKeysFromMnemonicAndPassphrase( mnemonic: "cannon rabbit obvious drama slogan net acoustic donor core acoustic clinic poem travel plunge winter", passphrase: "5tjpU0cimq", ); print("identity ===> $identity"); - // identityWithMnemonic ===> [privateKey, publicKey, publicKeyHash] - // Accessing: private key ===> identity[0] | public key ===> identity[1] | public Key Hash ===> identity[2] all of type string. + // identityWithMnemonic ===> [privateKey, publicKey, publicKeyHash] + // Accessing: private key ===> identity[0] | public key ===> identity[1] | public Key Hash ===> identity[2] all of type string. - //Sign operation with public-Key and forged operation + //Sign operation with public-Key and forged operation List signOpGrp = await TezsterDart.signOperationGroup( - privateKey: "edskRdVS5H9YCRAG8yqZkX2nUTbGcaDqjYgopkJwRuPUnYzCn3t9ZGksncTLYe33bFjq29pRhpvjQizCCzmugMGhJiXezixvdC", - forgedOperation: "713cb068fe3ac078351727eb5c34279e22b75b0cf4dc0a8d3d599e27031db136040cb9f9da085607c05cac1ca4c62a3f3cfb8146aa9b7f631e52f877a1d363474404da8130b0b940ee", + privateKey: + "edskRdVS5H9YCRAG8yqZkX2nUTbGcaDqjYgopkJwRuPUnYzCn3t9ZGksncTLYe33bFjq29pRhpvjQizCCzmugMGhJiXezixvdC", + forgedOperation: + "713cb068fe3ac078351727eb5c34279e22b75b0cf4dc0a8d3d599e27031db136040cb9f9da085607c05cac1ca4c62a3f3cfb8146aa9b7f631e52f877a1d363474404da8130b0b940ee", ); print("signOperationGroup ===> $signOpGrp"); - //signOperationGroup ===> [hexSignature, signedOpBytes] - //Accessing: hex signature ===> signOpGrp[0] | signed Operation bytes ===> signOpGrp[1] all of type string + //signOperationGroup ===> [hexSignature, signedOpBytes] + //Accessing: hex signature ===> signOpGrp[0] | signed Operation bytes ===> signOpGrp[1] all of type string - //Unlock fundraiser identity. - List identityFundraiser = await TezsterDart.unlockFundraiserIdentity( + //Unlock fundraiser identity. + List identityFundraiser = + await TezsterDart.unlockFundraiserIdentity( mnemonic: "cannon rabbit obvious drama slogan net acoustic donor core acoustic clinic poem travel plunge winter", email: "lkbpoife.tobqgidu@tezos.example.org", passphrase: "5tjpU0cimq", ); print("identityFundraiser ===> $identityFundraiser"); - //identityFundraiser ===> [privateKey, publicKey, publicKeyHash] - //Accessing: private key ===> identityFundraiser[0] | public key ===> identityFundraiser[1] | public Key Hash ===> identityFundraiser[2] all of type string. + //identityFundraiser ===> [privateKey, publicKey, publicKeyHash] + //Accessing: private key ===> identityFundraiser[0] | public key ===> identityFundraiser[1] | public Key Hash ===> identityFundraiser[2] all of type string. + } + + request() async { + String testNetServer = "https://testnet.tezster.tech"; + + dynamic response = await TezsterDart.performGetRequest( + server: testNetServer, + command: "chains/main/blocks", + ); + print("Response ===> $response"); + + dynamic getBlock = await TezsterDart.getBlock( + server: testNetServer, + hash: "", + chainid: "", + ); + print("GET-Block ===> $getBlock"); + + dynamic blockhead = await TezsterDart.getBlockHead( + server: testNetServer, + ); + print("Block-Head ===> $blockhead"); + + dynamic getAccForBlock = await TezsterDart.getAccountForBlock( + server: testNetServer, + accountHash: "", + blockHash: "", + chainid: "", + ); + print("GET-Acc-ForBlock ===> $getAccForBlock"); + + // dynamic getCounterForAccount = await TezsterDart.getCounterForAccount( + // server: testNetServer, + // accountHash: "", + // chainid: "", + // ); + // print("GET-Counter-ForAccount ===> $getCounterForAccount"); + + // dynamic getSpendableBalanceForAccount = + // await TezsterDart.getSpendableBalanceForAccount( + // server: testNetServer, + // accountHash: "", + // chainid: "", + // ); + // print( + // "GET-SpendableBalance-ForAccount ===> $getSpendableBalanceForAccount"); + + dynamic getAccountManagerForBlock = + await TezsterDart.getAccountManagerForBlock( + server: testNetServer, + chainid: "", + accountHash: "", + block: "", + ); + print("GET-Account-Manager-ForBlock ===> $getAccountManagerForBlock"); + + // dynamic isImplicitAndEmpty = await TezsterDart.isImplicitAndEmpty( + // server: testNetServer, + // accountHash: "", + // ); + // print("Is-ImplicitAndEmpty ===> $isImplicitAndEmpty"); + + dynamic isManagerKeyRevealedForAccount = + await TezsterDart.isManagerKeyRevealedForAccount( + server: testNetServer, + accountHash: "", + ); + print( + "Is-ManagerKey-Revealed-ForAccount ===> $isManagerKeyRevealedForAccount"); + + dynamic getContractStorage = await TezsterDart.getContractStorage( + server: testNetServer, + accountHash: "", + block: "", + chainid: "", + ); + print("GET-Contract-Storage ===> $getContractStorage"); + + dynamic getValueForBigMapKey = await TezsterDart.getValueForBigMapKey( + server: testNetServer, + key: "", + block: "", + chainid: "", + index: 1, + ); + print("GET-Value-ForBigMapKey ===> $getValueForBigMapKey"); + + dynamic getMempoolOperation = await TezsterDart.getMempoolOperation( + server: testNetServer, + chainid: "", + operationGroupId: "", + ); + print("GET-MempoolOperation ===> $getMempoolOperation"); + + dynamic getMempoolOperationsForAccount = + await TezsterDart.getMempoolOperationsForAccount( + server: testNetServer, + chainid: "", + ); + print("GET-MempoolOperations-ForAccount ===> $getMempoolOperationsForAccount"); } @override void initState() { super.initState(); - example(); + // example(); + request(); } @override diff --git a/example/pubspec.lock b/example/pubspec.lock index 4187c56..b991210 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -116,6 +116,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + http: + dependency: transitive + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.1" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.4" image: dependency: transitive description: @@ -151,6 +165,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.4" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.9.0" petitparser: dependency: transitive description: diff --git a/lib/tezster_dart.dart b/lib/tezster_dart.dart index 532b324..433ea32 100644 --- a/lib/tezster_dart.dart +++ b/lib/tezster_dart.dart @@ -11,6 +11,7 @@ import 'package:bs58check/bs58check.dart' as bs58check; import "package:unorm_dart/unorm_dart.dart" as unorm; import 'package:flutter_sodium/flutter_sodium.dart'; import 'package:convert/convert.dart'; +import 'package:http/http.dart' as http; class TezsterDart { static String generateMnemonic({int strength = 256}) { @@ -103,7 +104,7 @@ class TezsterDart { List mnemonicsBuffer = stringNormalize(mnemonic); String m = String.fromCharCodes(mnemonicsBuffer); - List normalizedPassphrase= stringNormalize("$email" + "$passphrase"); + List normalizedPassphrase = stringNormalize("$email" + "$passphrase"); String normString = String.fromCharCodes(normalizedPassphrase); String p = "mnemonic" + normString; Uint8List seed = PBKDF2(hashAlgorithm: sha512).generateKey(m, p, 2048, 32); @@ -115,4 +116,178 @@ class TezsterDart { String pkKeyHash = GenerateKeys.computeKeyHash(pk); return [skKey, pkKey, pkKeyHash]; } + + static dynamic performGetRequest({ + String server, + String command = "", + }) async { + assert(server != null); + String url = "$server/$command"; + http.Response response = await http.get(url); + if (response.statusCode != 200) { + return "Invalid url"; + } + dynamic data = jsonDecode(response.body); + return data; + } + + static dynamic getBlock({ + String server, + String hash = "head", + String chainid = "main", + }) async { + assert(server != null); + dynamic response = await performGetRequest( + server: server, + command: "chains/$chainid/blocks/$hash", + ); + return response; + } + + static dynamic getBlockHead({ + String server, + }) async { + assert(server != null); + dynamic response = await getBlock(server: server); + return response; + } + + static dynamic getAccountForBlock({ + String server, + String blockHash, + String accountHash, + String chainid = "main", + }) async { + dynamic response = await performGetRequest( + server: server, + command: + "chains/$chainid/blocks/$blockHash/context/contracts/$accountHash}", + ); + return response; + } + + static dynamic getCounterForAccount({ + String server, + String accountHash, + String chainid = "main", + }) async { + dynamic counter = await performGetRequest( + server: server, + command: + "chains/$chainid/blocks/head/context/contracts/$accountHash/counter", + ); + return int.parse(counter, radix: 10); + } + + static dynamic getSpendableBalanceForAccount({ + String server, + String accountHash, + String chainid = "main", + }) async { + dynamic account = await performGetRequest( + server: server, + command: "chains/$chainid/blocks/head/context/contracts/$accountHash"); + return int.parse(account.toString(), radix: 10); + } + + static dynamic getAccountManagerForBlock({ + String server, + String block, + String accountHash, + String chainid = "main", + }) async { + try { + dynamic result = await performGetRequest( + server: server, + command: + "chains/$chainid/blocks/$block/context/contracts/$accountHash/manager_key", + ); + if (result.toString() == null) { + return ""; + } + return result.toString(); + } catch (e) { + throw (e); + } + } + + static dynamic isImplicitAndEmpty({ + String server, + String accountHash, + }) async { + dynamic account = await getAccountForBlock( + server: server, + blockHash: "head", + accountHash: accountHash, + ); + bool isImplicit = accountHash.toLowerCase().startsWith("tz"); + bool isEmpty = account.balance == 0; + return (isImplicit && isEmpty) ? true : false; + } + + static dynamic isManagerKeyRevealedForAccount({ + String server, + String accountHash, + }) async { + dynamic managerKey = await getAccountManagerForBlock( + server: server, + block: "head", + accountHash: accountHash, + ); + return managerKey.toString().length > 0 ? true : false; + } + + static dynamic getContractStorage({ + String server, + String accountHash, + String chainid = "main", + String block = "head", + }) async { + dynamic response = performGetRequest( + server: server, + command: + "chains/$chainid/blocks/$block/context/contracts/$accountHash/storage", + ); + return response; + } + + static dynamic getValueForBigMapKey({ + String server, + num index, + String key, + String block = "main", + String chainid = "head", + }) async { + dynamic response = performGetRequest( + server: server, + command: "chains/$chainid/blocks/$block/context/big_maps/$index/$key", + ); + return response; + } + + static dynamic getMempoolOperation({ + String server, + String operationGroupId, + String chainid = "main", + }) async { + dynamic mempoolContent = performGetRequest( + server: server, + command: "chains/$chainid/mempool/pending_operations", + ); + //TODO: mempoolContent + return mempoolContent; + } + + static dynamic getMempoolOperationsForAccount({ + String server, + String accountHash, + String chainid = "main", + }) async { + dynamic mempoolContent = await performGetRequest( + server: server, + command: "chains/$chainid/mempool/pending_operations", + ); + //TODO : Modification to be done. + return mempoolContent; + } } diff --git a/pubspec.lock b/pubspec.lock index 89b74d4..f844fe7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -109,6 +109,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.1" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.4" image: dependency: transitive description: @@ -144,6 +158,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.4" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.9.0" petitparser: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d8647b6..cb7b477 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -19,7 +19,8 @@ dependencies: unorm_dart: ^0.1.2 password_hash: ^2.0.0 convert: ^2.1.1 + http: ^0.12.1 dev_dependencies: flutter_test: - sdk: flutter \ No newline at end of file + sdk: flutter