Skip to content

Commit

Permalink
Updated pact API implementation to allow for manual networkId
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterling Long committed Apr 11, 2023
1 parent f13abab commit 0077e61
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.5

- Added `setNetworkId` and `getNodeUrl` to `IPactApiV1` interface and updated implementation

## 2.1.4

- Added `getNetworkId` to `IPactApiV1` interface and updated implementation
Expand Down
3 changes: 3 additions & 0 deletions lib/models/pact_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class ContinuationMessage {

@JsonSerializable(includeIfNull: false)
class CommandPayload {
/// The exec message to send to the chain.
ExecMessage? exec;

/// The continuation message to send to the chain, used to continue a pact.
ContinuationMessage? cont;

CommandPayload({
Expand Down
12 changes: 11 additions & 1 deletion lib/pact_api/i_pact_api_v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ abstract class IPactApiV1 {
/// Set the target node that the API to send requests to.
/// Example: https://api.chainweb.com
/// This will fetch the networkId from the node's config and store it for future use.
Future<void> setNodeUrl({required String nodeUrl});
/// Returns true if it was able to get the networkId from the node.
Future<bool> setNodeUrl({required String nodeUrl});

/// Get the nodeUrl set by [setNodeUrl].
String? getNodeUrl();

/// Set the networkId. This is used to build the url for the pact api.
/// Not necessary if [setNodeUrl] successfully fetched the networkId from the node.
/// When running in flutter web, [setNodeUrl] with fail due to CORS, and you will
/// have to manually set the networkId with this funcion.
void setNetworkId({required String networkId});

/// Get the networkId of the node set by [setNodeUrl].
/// Returns null if the nodeUrl has not been set.
Expand Down
28 changes: 21 additions & 7 deletions lib/pact_api/pact_api_v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,36 @@ import 'package:kadena_dart_sdk/pact_api/i_pact_api_v1.dart';
import 'package:http/http.dart' as http;

class PactApiV1 extends IPactApiV1 {
String _nodeUrl = '';
String? _nodeUrl;
String? _networkId;

@override
Future<void> setNodeUrl({required String nodeUrl}) async {
Future<bool> setNodeUrl({required String nodeUrl}) async {
// Remove trailing slash if present
_nodeUrl = nodeUrl.endsWith('/')
? nodeUrl.substring(0, nodeUrl.length - 1)
: nodeUrl;

// Get the networkId from the node's config
http.Response response = await http.get(Uri.parse('$_nodeUrl/config'));
Map<String, dynamic> config = jsonDecode(response.body);
_networkId = config['chainwebVersion'];
try {
// Get the networkId from the node's config
http.Response response = await http.get(Uri.parse('$_nodeUrl/config'));
Map<String, dynamic> config = jsonDecode(response.body);
_networkId = config['chainwebVersion'];
} catch (_) {
return false;
}

return true;
}

// return _networkId;
@override
String? getNodeUrl() {
return _nodeUrl;
}

@override
void setNetworkId({required String networkId}) {
_networkId = networkId;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: kadena_dart_sdk
description: A dart package for interacting with the Kadena blockchain.
version: 2.1.4
version: 2.1.5
repository: https://github.com/Eucalyptus-Labs/kadena-quicksign-dart

environment:
Expand Down
6 changes: 3 additions & 3 deletions test/signing_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
SignRequest request1 = signingApi.parseSignRequest(
request: request,
);
print(request1);
// print(request1);
PactCommandPayload pcp = signingApi.constructPactCommandPayload(
request: request1,
signingPubKey: kp1.publicKey,
Expand Down Expand Up @@ -50,7 +50,7 @@ void main() {
);

if (request1.caps != null) {
print('request1.caps: ${request1.caps}');
// print('request1.caps: ${request1.caps}');
expect(pactCommand.signers[0].clist != null, true);
expect(
pactCommand.signers[0].clist!.length,
Expand Down Expand Up @@ -292,7 +292,7 @@ void main() {
),
throwsA(
predicate((e) {
print((e as QuicksignResult).error!.type);
// print((e as QuicksignResult).error!.type);
return e is QuicksignResult &&
e.error != null &&
e.error!.type == QuicksignError.emptyList;
Expand Down

0 comments on commit 0077e61

Please sign in to comment.