# Docs helper

Docs helper allows to easily perform all the operations related to the commercio.network docs module.

# Provided operations

  1. Creates a new transaction that allows to share the document associated with the given contentUri and having the given metadata and checksum.
    If encryptedData is specified, encrypts the proper data for the specified recipients and then sends the transaction to the blockchain
static Future<TransactionResult> shareDocument(
  String contentUri,
  CommercioDocMetadata metadata,
  CommercioDocChecksum checksum,
  List<String> recipientsDids,
  Wallet wallet, {
  Key aesKey = null,
  List<EncryptedData> encryptedData = const [],
}) async
1
2
3
4
5
6
7
8
9
  1. Returns the list of all the CommercioDoc that the specified address has sent
static Future<List<CommercioDoc>> getSendDocuments(
  String address,
  Wallet wallet,
) async
1
2
3
4
  1. Returns the list of all the CommercioDoc that the specified address has received
static Future<List<CommercioDoc>> getReceivedDocuments(
  String address,
  Wallet wallet,
) async
1
2
3
4
  1. Creates a new transaction which tells the recipient that the document having the specified documentId and present inside the transaction with hash txHash has been properly seen.
    The proof is an optional field that indicates proof of reading.
static Future<TransactionResult> sendDocumentReceipt(
  String recipient,
  String txHash,
  String documentId,
  Wallet wallet, {
  String proof = null,
}) 
1
2
3
4
5
6
7
  1. Returns the list of all the CommercioDocReceipt that have been sent from the given address
Future<List<CommercioDocReceipt>> getSentReceipts(
  String address,
  Wallet wallet,
) async 
1
2
3
4
  1. Returns the list of all the CommercioDocRecepit that have been received from the given address
Future<List<CommercioDocReceipt>> getReceivedReceipts(
  String address,
  Wallet wallet,
) async 
1
2
3
4

# Usage examples

You can reach the examples code here

import 'package:commerciosdk/export.dart';
import 'package:uuid/uuid.dart';
import 'commons.dart';

void main() async {
  final info = NetworkInfo(
    bech32Hrp: "did:com:",
    lcdUrl: "http://localhost:1317",
  );

  final userMnemonic = [
    "will",
    "hard",
    "topic",
    "spray",
    "beyond",
    "ostrich",
    "moral",
    "morning",
    "gas",
    "loyal",
    "couch",
    "horn",
    "boss",
    "across",
    "age",
    "post",
    "october",
    "blur",
    "piece",
    "wheel",
    "film",
    "notable",
    "word",
    "man"
  ];

  final userWallet = Wallet.derive(userMnemonic, info);

  final recipientMnemonic = [
    "crisp",
    "become",
    "thumb",
    "fetch",
    "forest",
    "senior",
    "polar",
    "slush",
    "wise",
    "wash",
    "doctor",
    "sunset",
    "skate",
    "disease",
    "power",
    "tool",
    "sock",
    "upper",
    "diary",
    "what",
    "trap",
    "artist",
    "wood",
    "cereal"
  ];

  final recipientWallet = Wallet.derive(recipientMnemonic, info);

  // --- Share a document
  final docRecipientDid = recipientWallet.bech32Address;
  final pair = await _shareDoc([docRecipientDid], userWallet);

  // --- Share receipt
  final receiptRecipientDid = userWallet.bech32Address;
  await _sendReceipt(
          pair.first, 
          pair.second, 
          receiptRecipientDid, 
          recipientWallet
  );
}

class Pair<F, S> {
  final F first;
  final S second;

  Pair(this.first, this.second);
}

/// Shows how to share a document to the given recipients.
/// Documentation: https://docs.commercio.network/x/docs/tx/send-document.html
Future<Pair<String, String>> _shareDoc(
  List<String> recipients,
  Wallet wallet,
) async {
  final docId = new Uuid().v4();
  final response = await DocsHelper.shareDocument(
    id: docId,
    contentUri: "https://example.com/document",
    metadata: CommercioDocMetadata(
      contentUri: "https://example.com/document/metadata",
      schema: CommercioDocMetadataSchema(
        uri: "https://example.com/custom/metadata/schema",
        version: "1.0.0",
      ),
    ),
    recipients: recipients,
    fees: [StdCoin(denom: "ucommercio", amount: "10000")],
    wallet: wallet,
  );
  checkResponse(response);
  return Pair(docId, response.hash);
}

/// Shows how to send a document receipt to the specified [recipient] for
/// the given [docId] present inside the transaction having the given [txHash].
Future<void> _sendReceipt(
  String docId,
  String txHash,
  String recipient,
  Wallet wallet,
) async {
  final response = await DocsHelper.sendDocumentReceipt(
    recipient: recipient,
    txHash: txHash,
    documentId: docId,
    wallet: wallet,
  );
  checkResponse(response);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130