# 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 metadata, document id , recipients and with the sender wallet. Optional fields are contentUri, doSign, checksum, fee and the broadcasting mode.

    If encryptedData is specified, encrypts the proper data and optional aesKey for the specified recipients and then sends the transaction to the blockchain. If doSign is provided then also the checksum field is required.

    suspend fun shareDocument(
         id: String,
         metadata: CommercioDoc.Metadata,
         recipients: List<Did>,
         wallet: Wallet,
         doSign: CommercioDoc.CommercioDoSign? = null,
         checksum: CommercioDoc.Checksum? = null,
         aesKey: SecretKey = KeysHelper.generateAesKey(),
         encryptedData: List<EncryptedData> = listOf(),
         fee: StdFee? = null,
         contentUri: String = "",
         mode: BroadcastingMode? = null
    ): TxResponse
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
  2. Create a new transaction that allows to share a list of previously generated documents commercioDocsList signing the transaction with wallet. Optionally fee and broadcasting mode parameters can be specified.

    suspend fun shareDocumentsList(
        commercioDocs: List<CommercioDoc>,
        wallet: Wallet,
        fee: StdFee? = null,
        mode: BroadcastingMode? = null
    ): TxResponse
    
    1
    2
    3
    4
    5
    6
  3. Returns the list of all the CommercioDoc that the specified address has sent.

    suspend fun getSentDocuments(address: String, networkInfo: NetworkInfo): List<CommercioDoc>
    
    1
  4. Returns the list of all the CommercioDoc that the specified address has received.

    suspend fun getReceivedDocuments(address: String, networkInfo: NetworkInfo): List<CommercioDoc>
    
    1
  5. 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; optionally proof of reading, fee and broadcasting mode.

    suspend fun sendDocumentReceipt(
        recipient: Did,
        txHash: String,
        documentId: String,
        proof: String = "",
        wallet: Wallet,
        fee: StdFee? = null,
        mode: BroadcastingMode? = null
    ): TxResponse
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  6. Creates a new transaction which sends a list of previously generated receipts commercioDocReceiptsList. Optionally fee and broadcasting mode parameters can be specified.

    suspend fun sendDocumentReceiptsList(
        commercioDocReceipts: List<CommercioDocReceipt>,
        wallet: Wallet,
        fee: StdFee? = null,
        mode: BroadcastingMode? = null
    ): TxResponse 
    
    1
    2
    3
    4
    5
    6
  7. Returns the list of all the CommercioDocReceipt that have been sent from the given address.

    suspend fun getSentReceipts(address: String, networkInfo: NetworkInfo): List<CommercioDocReceipt>
    
    1
  8. Returns the list of all the CommercioDocReceipt that have been received from the given address

    suspend fun getReceivedReceipts(address: String, networkInfo: NetworkInfo): List<CommercioDocReceipt>
    
    1

# Usage examples

    val info = NetworkInfo(
        bech32Hrp = "did:com:", 
        lcdUrl = "http://localhost:1317"
    )

    val userMnemonic = listOf(
        "will",
        "hard",
        "topic",
        "spray",
        "beyond",
        "ostrich",
        "moral",
        "morning",
        "gas",
        "loyal",
        "couch",
        "horn",
        "boss",
        "across",
        "age",
        "post",
        "october",
        "blur",
        "piece",
        "wheel",
        "film",
        "notable",
        "word",
        "man"
    )
    
    val senderWallet = Wallet.derive(mnemonic = userMnemonic, networkInfo = info)
    
    val recipientMnemonic = listOf(
            "crisp",
            "become",
            "thumb",
            "fetch",
            "forest",
            "senior",
            "polar",
            "slush",
            "wise",
            "wash",
            "doctor",
            "sunset",
            "skate",
            "disease",
            "power",
            "tool",
            "sock",
            "upper",
            "diary",
            "what",
            "trap",
            "artist",
            "wood",
            "cereal"
    )
    
    val recipientWallet = Wallet.derive(recipientMnemonic, info)
    val recipientDid = Did(senderWallet.bech32Address)

    val metadata = CommercioDoc.Metadata(
        contentUri = "https://example.com/document/metadata",
        schema = CommercioDoc.Metadata.Schema(
            uri = "https://example.com/custom/metadata/schema",
            version = "1.0.0"
        )
    )

    val docRecipientDid = Did(recipientWallet.bech32Address)
    val docId = UUID.randomUUID().toString()
    
    try {
        // --- Share a document
        val response = DocsHelper.shareDocument(
            id = docId,
            contentUri = "https://example.com/document",
            metadata = metadata,
            recipients = listOf(recipientDid),
            fee = StdFee(gas = "200000", amount = listOf(StdCoin(denom = "ucommercio", amount = "10000"))),
            wallet = senderWallet
        )
        
        val txHash = (response as TxResponse.Successful).txHash

       // --- Send receipt
       DocsHelper.sendDocumentReceipt(
          recipient = recipientDid,
          txHash = txHash,
          documentId = docId,
          wallet = recipientWallet
       )
    } catch (e: Exception) {
       throw e
    }

// --- getSentDocuments
val sentDocs = DocsHelper.getSentDocuments(
   address = senderWallet.bech32Address,
   networkInfo = networkInfo
)
print("\nsentDocs: $sentDocs\n\n")

// --- getReceivedDocuments
val receivedDocs = DocsHelper.getReceivedDocuments(
   address = recipientWallet.bech32Address,
   networkInfo = networkInfo
)
print("\nreceivedDocs: $receivedDocs\n\n")

// --- getSentReceipts
val sentReceipts = DocsHelper.getSentReceipts(
   address = senderWallet.bech32Address,
   networkInfo = networkInfo
)
print("\nsentReceipts: $sentReceipts\n")


// --- getReceivedReceipts
val receivedReceipts = DocsHelper.getReceivedReceipts(
   address = recipientWallet.bech32Address,
   networkInfo = networkInfo
)
print("\nreceivedReceipts: $receivedReceipts\n")
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