@chaoschain/sdk 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -709,7 +709,12 @@ var ChaosAgent = class {
709
709
  */
710
710
  async registerIdentity(metadata) {
711
711
  const uri = metadata ? `data:application/json,${JSON.stringify(metadata)}` : "";
712
- const tx = await this.identityContract.registerAgent(uri);
712
+ let tx;
713
+ if (uri) {
714
+ tx = await this.identityContract["register(string)"](uri);
715
+ } else {
716
+ tx = await this.identityContract["register()"]();
717
+ }
713
718
  const receipt = await tx.wait();
714
719
  const event = receipt.logs.map((log) => {
715
720
  try {
@@ -720,9 +725,9 @@ var ChaosAgent = class {
720
725
  } catch {
721
726
  return null;
722
727
  }
723
- }).find((e) => e?.name === "AgentRegistered");
728
+ }).find((e) => e?.name === "Registered");
724
729
  if (!event) {
725
- throw new Error("AgentRegistered event not found");
730
+ throw new Error("Registered event not found");
726
731
  }
727
732
  return {
728
733
  agentId: event.args.agentId,
@@ -735,7 +740,7 @@ var ChaosAgent = class {
735
740
  */
736
741
  async getAgentMetadata(agentId) {
737
742
  try {
738
- const uri = await this.identityContract.getAgentURI(agentId);
743
+ const uri = await this.identityContract.tokenURI(agentId);
739
744
  if (!uri) {
740
745
  return null;
741
746
  }
@@ -762,7 +767,7 @@ var ChaosAgent = class {
762
767
  * Set agent URI
763
768
  */
764
769
  async setAgentUri(agentId, uri) {
765
- const tx = await this.identityContract.setAgentURI(agentId, uri);
770
+ const tx = await this.identityContract.setAgentUri(agentId, uri);
766
771
  const receipt = await tx.wait();
767
772
  return receipt.hash;
768
773
  }
@@ -845,71 +850,152 @@ var ChaosAgent = class {
845
850
  }
846
851
  }
847
852
  /**
848
- * Give feedback to an agent
853
+ * Give feedback to an agent (ERC-8004 v1.0)
854
+ *
855
+ * @param params Feedback parameters including agentId, rating, feedbackUri, and optional auth
856
+ * @returns Transaction hash
849
857
  */
850
858
  async giveFeedback(params) {
851
- const { agentId, rating, feedbackUri } = params;
859
+ const { agentId, rating, feedbackUri, feedbackData } = params;
852
860
  if (rating < 0 || rating > 100) {
853
861
  throw new Error("Rating must be between 0 and 100");
854
862
  }
855
- const tx = await this.reputationContract.giveFeedback(agentId, rating, feedbackUri);
863
+ const score = rating;
864
+ const tag1 = feedbackData?.tag1 || ethers.ethers.ZeroHash;
865
+ const tag2 = feedbackData?.tag2 || ethers.ethers.ZeroHash;
866
+ const feedbackContent = feedbackData?.content || feedbackUri;
867
+ const feedbackHash = ethers.ethers.keccak256(ethers.ethers.toUtf8Bytes(feedbackContent));
868
+ const feedbackAuth = feedbackData?.feedbackAuth || "0x";
869
+ const tx = await this.reputationContract.giveFeedback(
870
+ agentId,
871
+ score,
872
+ tag1,
873
+ tag2,
874
+ feedbackUri,
875
+ feedbackHash,
876
+ feedbackAuth
877
+ );
856
878
  const receipt = await tx.wait();
857
879
  return receipt.hash;
858
880
  }
859
881
  /**
860
- * Revoke feedback
882
+ * Revoke feedback (ERC-8004 v1.0)
883
+ * @param agentId Agent ID that received the feedback
884
+ * @param feedbackIndex Index of the feedback to revoke (uint64)
861
885
  */
862
- async revokeFeedback(feedbackId) {
863
- const tx = await this.reputationContract.revokeFeedback(feedbackId);
886
+ async revokeFeedback(agentId, feedbackIndex) {
887
+ const tx = await this.reputationContract.revokeFeedback(agentId, feedbackIndex);
864
888
  const receipt = await tx.wait();
865
889
  return receipt.hash;
866
890
  }
867
891
  /**
868
- * Append response to feedback
892
+ * Append response to feedback (ERC-8004 v1.0)
893
+ * @param agentId Agent ID that received the feedback
894
+ * @param clientAddress Address of the client who gave feedback
895
+ * @param feedbackIndex Index of the feedback
896
+ * @param responseUri URI containing the response data
897
+ * @param responseHash Hash of the response content
869
898
  */
870
- async appendResponse(feedbackId, responseUri) {
871
- const tx = await this.reputationContract.appendResponse(feedbackId, responseUri);
899
+ async appendResponse(agentId, clientAddress, feedbackIndex, responseUri, responseHash) {
900
+ const tx = await this.reputationContract.appendResponse(
901
+ agentId,
902
+ clientAddress,
903
+ feedbackIndex,
904
+ responseUri,
905
+ responseHash
906
+ );
872
907
  const receipt = await tx.wait();
873
908
  return receipt.hash;
874
909
  }
875
910
  /**
876
- * Get feedback details
911
+ * Read specific feedback (ERC-8004 v1.0)
912
+ * @param agentId Agent ID that received the feedback
913
+ * @param clientAddress Address of the client who gave feedback
914
+ * @param index Index of the feedback
915
+ */
916
+ async readFeedback(agentId, clientAddress, index) {
917
+ const feedback = await this.reputationContract.readFeedback(agentId, clientAddress, index);
918
+ return {
919
+ score: Number(feedback.score),
920
+ tag1: feedback.tag1,
921
+ tag2: feedback.tag2,
922
+ isRevoked: feedback.isRevoked
923
+ };
924
+ }
925
+ /**
926
+ * Read all feedback for an agent (ERC-8004 v1.0)
927
+ * @param agentId Agent ID
928
+ * @param clientAddresses Array of client addresses (empty array for all clients)
929
+ * @param tag1 First tag filter (ZeroHash for no filter)
930
+ * @param tag2 Second tag filter (ZeroHash for no filter)
931
+ * @param includeRevoked Whether to include revoked feedback
932
+ */
933
+ async readAllFeedback(agentId, clientAddresses = [], tag1 = ethers.ethers.ZeroHash, tag2 = ethers.ethers.ZeroHash, includeRevoked = false) {
934
+ const result = await this.reputationContract.readAllFeedback(
935
+ agentId,
936
+ clientAddresses,
937
+ tag1,
938
+ tag2,
939
+ includeRevoked
940
+ );
941
+ return {
942
+ clients: result.clients,
943
+ scores: result.scores.map((s) => Number(s)),
944
+ tag1s: result.tag1s,
945
+ tag2s: result.tag2s,
946
+ revokedStatuses: result.revokedStatuses
947
+ };
948
+ }
949
+ /**
950
+ * Get summary statistics (ERC-8004 v1.0)
951
+ * @param agentId Agent ID
952
+ * @param clientAddresses Array of client addresses (empty array for all clients)
953
+ * @param tag1 First tag filter (ZeroHash for no filter)
954
+ * @param tag2 Second tag filter (ZeroHash for no filter)
877
955
  */
878
- async getFeedback(feedbackId) {
879
- const feedback = await this.reputationContract.getFeedback(feedbackId);
956
+ async getSummary(agentId, clientAddresses = [], tag1 = ethers.ethers.ZeroHash, tag2 = ethers.ethers.ZeroHash) {
957
+ const result = await this.reputationContract.getSummary(agentId, clientAddresses, tag1, tag2);
880
958
  return {
881
- feedbackId: feedback.feedbackId,
882
- fromAgent: feedback.fromAgent,
883
- toAgent: feedback.toAgent,
884
- rating: feedback.rating,
885
- feedbackUri: feedback.feedbackURI,
886
- timestamp: Number(feedback.timestamp),
887
- revoked: feedback.revoked
959
+ count: result.count,
960
+ averageScore: Number(result.averageScore)
888
961
  };
889
962
  }
890
963
  /**
891
- * Get agent feedback IDs
964
+ * Get list of clients who gave feedback
965
+ * @param agentId Agent ID
966
+ */
967
+ async getClients(agentId) {
968
+ return this.reputationContract.getClients(agentId);
969
+ }
970
+ /**
971
+ * Get last feedback index for a client
972
+ * @param agentId Agent ID
973
+ * @param clientAddress Client address
892
974
  */
893
- async getAgentFeedback(agentId, offset = 0, limit = 10) {
894
- return this.reputationContract.getAgentFeedback(agentId, offset, limit);
975
+ async getLastIndex(agentId, clientAddress) {
976
+ return this.reputationContract.getLastIndex(agentId, clientAddress);
895
977
  }
896
978
  /**
897
- * Get agent reputation stats
979
+ * Get identity registry address from reputation contract
898
980
  */
899
- async getAgentStats(agentId) {
900
- return this.reputationContract.getAgentStats(agentId);
981
+ async getIdentityRegistry() {
982
+ return this.reputationContract.getIdentityRegistry();
901
983
  }
902
984
  // ============================================================================
903
985
  // Validation Registry Methods
904
986
  // ============================================================================
905
987
  /**
906
- * Request validation from another agent
988
+ * Request validation from a validator (ERC-8004 v1.0)
989
+ * @param validatorAddress Address of the validator
990
+ * @param agentId Agent ID requesting validation
991
+ * @param requestUri URI containing validation request data
992
+ * @param requestHash Hash of the request content (bytes32)
907
993
  */
908
- async requestValidation(params) {
909
- const { validatorAgentId, requestUri, requestHash } = params;
910
- const hashBytes = ethers.ethers.id(requestHash);
911
- const tx = await this.validationContract.requestValidation(
912
- validatorAgentId,
994
+ async requestValidation(validatorAddress, agentId, requestUri, requestHash) {
995
+ const hashBytes = requestHash.startsWith("0x") ? requestHash : ethers.ethers.id(requestHash);
996
+ const tx = await this.validationContract.validationRequest(
997
+ validatorAddress,
998
+ agentId,
913
999
  requestUri,
914
1000
  hashBytes
915
1001
  );
@@ -917,76 +1003,112 @@ var ChaosAgent = class {
917
1003
  return receipt.hash;
918
1004
  }
919
1005
  /**
920
- * Respond to validation request
921
- */
922
- async respondToValidation(requestId, approved, responseUri) {
923
- const tx = await this.validationContract.respondToValidation(
924
- requestId,
925
- approved,
926
- responseUri
1006
+ * Respond to validation request (ERC-8004 v1.0)
1007
+ * @param requestHash Hash of the original validation request (bytes32)
1008
+ * @param response Response score (0-100, where 100 = approved)
1009
+ * @param responseUri URI containing response data
1010
+ * @param responseHash Hash of the response content (bytes32)
1011
+ * @param tag Optional tag for categorization (bytes32)
1012
+ */
1013
+ async respondToValidation(requestHash, response, responseUri, responseHash, tag = ethers.ethers.ZeroHash) {
1014
+ if (response < 0 || response > 100) {
1015
+ throw new Error("Response must be between 0 and 100");
1016
+ }
1017
+ const reqHashBytes = requestHash.startsWith("0x") ? requestHash : ethers.ethers.id(requestHash);
1018
+ const resHashBytes = responseHash.startsWith("0x") ? responseHash : ethers.ethers.id(responseHash);
1019
+ const tagBytes = tag.startsWith("0x") ? tag : ethers.ethers.ZeroHash;
1020
+ const tx = await this.validationContract.validationResponse(
1021
+ reqHashBytes,
1022
+ response,
1023
+ responseUri,
1024
+ resHashBytes,
1025
+ tagBytes
927
1026
  );
928
1027
  const receipt = await tx.wait();
929
1028
  return receipt.hash;
930
1029
  }
931
1030
  /**
932
- * Get validation request details
1031
+ * Get validation status (ERC-8004 v1.0)
1032
+ * @param requestHash Hash of the validation request (bytes32)
933
1033
  */
934
- async getValidationRequest(requestId) {
935
- const request = await this.validationContract.getValidationRequest(requestId);
1034
+ async getValidationStatus(requestHash) {
1035
+ const hashBytes = requestHash.startsWith("0x") ? requestHash : ethers.ethers.id(requestHash);
1036
+ const result = await this.validationContract.getValidationStatus(hashBytes);
936
1037
  return {
937
- requestId: request.requestId,
938
- requester: request.requester,
939
- validator: request.validator,
940
- requestUri: request.requestURI,
941
- requestHash: request.requestHash,
942
- status: request.status,
943
- responseUri: request.responseURI,
944
- timestamp: Number(request.timestamp)
1038
+ validatorAddress: result.validatorAddress,
1039
+ agentId: result.agentId,
1040
+ response: Number(result.response),
1041
+ responseHash: result.responseHash,
1042
+ tag: result.tag,
1043
+ lastUpdate: result.lastUpdate
945
1044
  };
946
1045
  }
947
1046
  /**
948
- * Get agent validation requests
1047
+ * Get validation summary statistics (ERC-8004 v1.0)
1048
+ * @param agentId Agent ID
1049
+ * @param validatorAddresses Array of validator addresses (empty for all)
1050
+ * @param tag Tag filter (ZeroHash for no filter)
949
1051
  */
950
- async getAgentValidationRequests(agentId, asValidator = false, offset = 0, limit = 10) {
951
- return this.validationContract.getAgentValidationRequests(
952
- agentId,
953
- asValidator,
954
- offset,
955
- limit
956
- );
1052
+ async getValidationSummary(agentId, validatorAddresses = [], tag = ethers.ethers.ZeroHash) {
1053
+ const tagBytes = tag.startsWith("0x") ? tag : ethers.ethers.ZeroHash;
1054
+ const result = await this.validationContract.getSummary(agentId, validatorAddresses, tagBytes);
1055
+ return {
1056
+ count: result.count,
1057
+ avgResponse: Number(result.avgResponse)
1058
+ };
957
1059
  }
958
1060
  /**
959
- * Get validation statistics for an agent
1061
+ * Get all validation request hashes for an agent
1062
+ * @param agentId Agent ID
960
1063
  */
961
- async getValidationStats(agentId) {
962
- return this.validationContract.getValidationStats(agentId);
1064
+ async getAgentValidations(agentId) {
1065
+ return this.validationContract.getAgentValidations(agentId);
1066
+ }
1067
+ /**
1068
+ * Get all validation requests for a validator
1069
+ * @param validatorAddress Validator address
1070
+ */
1071
+ async getValidatorRequests(validatorAddress) {
1072
+ return this.validationContract.getValidatorRequests(validatorAddress);
1073
+ }
1074
+ /**
1075
+ * Get identity registry address from validation contract
1076
+ */
1077
+ async getValidationIdentityRegistry() {
1078
+ return this.validationContract.getIdentityRegistry();
963
1079
  }
964
1080
  // ============================================================================
965
1081
  // Event Listening
966
1082
  // ============================================================================
967
1083
  /**
968
- * Listen for AgentRegistered events
1084
+ * Listen for Registered events
969
1085
  */
970
1086
  onAgentRegistered(callback) {
971
- this.identityContract.on("AgentRegistered", callback);
1087
+ this.identityContract.on("Registered", callback);
972
1088
  }
973
1089
  /**
974
- * Listen for FeedbackGiven events
1090
+ * Listen for NewFeedback events (ERC-8004 v1.0)
975
1091
  */
976
- onFeedbackGiven(callback) {
977
- this.reputationContract.on("FeedbackGiven", callback);
1092
+ onNewFeedback(callback) {
1093
+ this.reputationContract.on("NewFeedback", callback);
978
1094
  }
979
1095
  /**
980
- * Listen for ValidationRequested events
1096
+ * Listen for ResponseAppended events (ERC-8004 v1.0)
981
1097
  */
982
- onValidationRequested(callback) {
983
- this.validationContract.on("ValidationRequested", callback);
1098
+ onResponseAppended(callback) {
1099
+ this.reputationContract.on("ResponseAppended", callback);
984
1100
  }
985
1101
  /**
986
- * Listen for ValidationResponded events
1102
+ * Listen for ValidationRequest events (ERC-8004 v1.0)
987
1103
  */
988
- onValidationResponded(callback) {
989
- this.validationContract.on("ValidationResponded", callback);
1104
+ onValidationRequest(callback) {
1105
+ this.validationContract.on("ValidationRequest", callback);
1106
+ }
1107
+ /**
1108
+ * Listen for ValidationResponse events (ERC-8004 v1.0)
1109
+ */
1110
+ onValidationResponse(callback) {
1111
+ this.validationContract.on("ValidationResponse", callback);
990
1112
  }
991
1113
  /**
992
1114
  * Remove all event listeners
@@ -3132,25 +3254,62 @@ var ChaosChainSDK = class {
3132
3254
  return { feedbackTxHash: txHash, feedbackUri };
3133
3255
  }
3134
3256
  /**
3135
- * Get agent reputation score
3257
+ * Get agent reputation score (ERC-8004 v1.0)
3258
+ */
3259
+ async getReputationScore(agentId) {
3260
+ const summary = await this.chaosAgent.getSummary(agentId, [], ethers.ethers.ZeroHash, ethers.ethers.ZeroHash);
3261
+ return summary.averageScore;
3262
+ }
3263
+ /**
3264
+ * Read all feedback for an agent
3265
+ */
3266
+ async readAllFeedback(agentId, clientAddresses = [], tag1 = ethers.ethers.ZeroHash, tag2 = ethers.ethers.ZeroHash, includeRevoked = false) {
3267
+ return this.chaosAgent.readAllFeedback(agentId, clientAddresses, tag1, tag2, includeRevoked);
3268
+ }
3269
+ /**
3270
+ * Get feedback summary statistics
3136
3271
  */
3137
- async getReputationScore(_agentId) {
3138
- return 0;
3272
+ async getFeedbackSummary(agentId, clientAddresses = [], tag1 = ethers.ethers.ZeroHash, tag2 = ethers.ethers.ZeroHash) {
3273
+ return this.chaosAgent.getSummary(agentId, clientAddresses, tag1, tag2);
3274
+ }
3275
+ /**
3276
+ * Get clients who gave feedback
3277
+ */
3278
+ async getClients(agentId) {
3279
+ return this.chaosAgent.getClients(agentId);
3139
3280
  }
3140
3281
  // ============================================================================
3141
3282
  // ERC-8004 Validation Methods
3142
3283
  // ============================================================================
3143
3284
  /**
3144
- * Request validation from validator
3285
+ * Request validation from validator (ERC-8004 v1.0)
3286
+ */
3287
+ async requestValidation(validatorAddress, agentId, requestUri, requestHash) {
3288
+ return this.chaosAgent.requestValidation(validatorAddress, agentId, requestUri, requestHash);
3289
+ }
3290
+ /**
3291
+ * Respond to validation request (ERC-8004 v1.0)
3292
+ */
3293
+ async respondToValidation(requestHash, response, responseUri, responseHash, tag) {
3294
+ return this.chaosAgent.respondToValidation(requestHash, response, responseUri, responseHash, tag);
3295
+ }
3296
+ /**
3297
+ * Get validation status
3145
3298
  */
3146
- async requestValidation(params) {
3147
- return this.chaosAgent.requestValidation(params);
3299
+ async getValidationStatus(requestHash) {
3300
+ return this.chaosAgent.getValidationStatus(requestHash);
3148
3301
  }
3149
3302
  /**
3150
- * Respond to validation request
3303
+ * Get validation summary for an agent
3151
3304
  */
3152
- async respondToValidation(requestId, approved, responseUri) {
3153
- return this.chaosAgent.respondToValidation(requestId, approved, responseUri);
3305
+ async getValidationSummary(agentId, validatorAddresses = [], tag = ethers.ethers.ZeroHash) {
3306
+ return this.chaosAgent.getValidationSummary(agentId, validatorAddresses, tag);
3307
+ }
3308
+ /**
3309
+ * Get validation stats (alias for getValidationSummary)
3310
+ */
3311
+ async getValidationStats(agentId) {
3312
+ return this.getValidationSummary(agentId);
3154
3313
  }
3155
3314
  // ============================================================================
3156
3315
  // x402 Crypto Payment Methods
@@ -3200,6 +3359,36 @@ var ChaosChainSDK = class {
3200
3359
  }
3201
3360
  return this.x402PaymentManager.getPaymentHistory(limit);
3202
3361
  }
3362
+ /**
3363
+ * Calculate total cost including protocol fee (2.5%)
3364
+ */
3365
+ calculateTotalCost(amount, currency = "USDC") {
3366
+ const amountNum = parseFloat(amount);
3367
+ const fee = amountNum * 0.025;
3368
+ const total = amountNum + fee;
3369
+ return {
3370
+ amount: amountNum.toFixed(6),
3371
+ fee: fee.toFixed(6),
3372
+ total: total.toFixed(6),
3373
+ currency
3374
+ };
3375
+ }
3376
+ /**
3377
+ * Get ETH balance
3378
+ */
3379
+ async getETHBalance() {
3380
+ const balance = await this.provider.getBalance(this.getAddress());
3381
+ return ethers.ethers.formatEther(balance);
3382
+ }
3383
+ /**
3384
+ * Get USDC balance (if USDC contract exists on network)
3385
+ */
3386
+ async getUSDCBalance() {
3387
+ if (!this.x402PaymentManager) {
3388
+ throw new Error("x402 payments not enabled - cannot get USDC balance");
3389
+ }
3390
+ return "0.0";
3391
+ }
3203
3392
  // ============================================================================
3204
3393
  // Traditional Payment Methods (Cards, Google Pay, Apple Pay, PayPal)
3205
3394
  // ============================================================================