Node.js
The Auditor is a cautious observer of the ledger that routinely checks the ledger's integrity.
For example, instead of requesting transaction-by-transaction (a transaction in PAD means either a data request or a Trustee/Validator response), an Auditor would request the ledger as raw blocks, which can be used to verify the integrity of the ledger. Trustee attestations allow the Auditor to confirm they have been provided with the same ledger used by the Trustees.

Computing digest of a ledger

The Auditor computes a succinct representation of the ledger, which can then be tested for consistency against the Trustee attestations. Since the ledger forms a blockchain, we define this succinct representation as the hash of the most recent block in the blockchain.
Occasionally, an Auditor may want to check on the transaction details. In the following sections, we will show how an Auditor can:
  • Compute hash of a block
  • Compute hash of a block's data section
  • Extract transactions from a block's data

Computing hash of a block

A block is composed of a header and a data section. The block header section contains the block number, previous block's hash, and the hash of the current block's data section.
Example
1
{
2
"header": {
3
"number": "1",
4
"previousHash": "0ede53fd38d632f3a7d849f8ba8f70c851d772b53cecbc9d858fe7c8af03a858",
5
"dataHash": "ee967360a911deb8f6bd77cbb49b334e1837c006c9b6cb2d59d8acd41964a6ac",
6
},
7
}
Copied!
When we say "hash" of a block, we mean the hash of its header section. Below is a Node.js example of how to compute the hash given a JSON representation of the header. The dependency cryptoUtil is defined here.
1
const asn1 = require('asn1.js');
2
3
const cryptoUtil = require('./cryptoUtil');
4
5
/**
6
* @typedef {Object} Header
7
* @property {string|number} [number]
8
* @property {string|Buffer} [previousHash]
9
* @property {string|Buffer} dataHash
10
*/
11
/**
12
* @param {Header} header
13
* @return {Buffer}
14
*/
15
function computeBlockHash(header) {
16
const body = function() {
17
this.seq().obj(
18
this.key('number').int(),
19
this.key('previousHash').octstr(),
20
this.key('dataHash').octstr(),
21
);
22
};
23
const headerAsn1 = asn1.define('headerAsn1', body);
24
let {number, previousHash, dataHash} = header;
25
if (previousHash == null) {
26
previousHash = '';
27
}
28
if (typeof(number) !== 'number') {
29
number = parseInt(number);
30
}
31
if (!Buffer.isBuffer(previousHash)) {
32
previousHash = Buffer.from(previousHash, 'hex');
33
}
34
if (!Buffer.isBuffer(dataHash)) {
35
dataHash = Buffer.from(dataHash, 'hex');
36
}
37
const encoded = headerAsn1.encode({
38
number,
39
previousHash,
40
dataHash,
41
});
42
const hash = cryptoUtil.hash(encoded);
43
return hash;
44
}
45
46
const header1 = {
47
dataHash: "af34032c92ef85b976db007fa339293253bc4e58f144cf648c6ffcd5a1150791",
48
};
49
console.log(computeBlockHash(header1).toString('hex'));
50
// 1c2cf6ed047ab1d35b2ed3bfbba376d99626db2213632dd4b955ceb4c05f3ba8
51
52
const header2 = {
53
number: 1,
54
previousHash: "1c2cf6ed047ab1d35b2ed3bfbba376d99626db2213632dd4b955ceb4c05f3ba8",
55
dataHash: "cf8289074798c7e8e1d267f0c0fb83acde339fb3007ff5246fb6745a94d55883",
56
};
57
console.log(computeBlockHash(header2).toString('hex'));
58
// 1af3275c9db7305fc85a3ded00a7829b5d6a99deacd35ed48cd31b79c8689275
Copied!

Computing data hash

When a user has utilised the data section of a block (for example, he/she extracted a data request and used it for some business logic), it is crucial for him/her to check its consistency against the dataHash entry in the block header.
The data section is an array of base64-encoded strings. Its hash is defined as the hash on the concatenation of these binary data. Below is a sample script for computing the data hash of a block:
1
const cryptoUtil = require('./cryptoUtil');
2
3
/**
4
* @param {string[]} data
5
* @return {Buffer}
6
*/
7
function computeDataHash(data) {
8
const dataArray = data.map((d) => {
9
return Buffer.from(d, 'base64');
10
});
11
return cryptoUtil.hash(...dataArray);
12
}
13
14
const block = {
15
header: {
16
dataHash: "4a148a98c3f5216c480bed070e267186751da47d3782a4282b2c383879097c80",
17
},
18
data: {
19
data: ["datum1==", "datum2=="], // fake data
20
},
21
};
22
23
const dataHashHex = block.header.dataHash;
24
const dataHash = Buffer.from(dataHashHex, 'hex');
25
26
const data = block.data.data;
27
console.log(computeDataHash(data).equals(dataHash)); // true
Copied!

Extracting transactions from block data

Functions and parameters

There are 4 smart contract functions that write on the ledger. Their parameters are as follows:
  • Init: writes metadata of the instance
    1. 1.
      stringified Trustee public keys
    2. 2.
      trustee threshold
    3. 3.
      stringified Validator public keys
    4. 4.
      validator threshold
  • PostDataRequest: posts a data request
    1. 1.
      a token
  • PostTrusteeResponse: posts a Trustee response
    1. 1.
      Trustee response payload
    2. 2.
      Trustee's digital signature on 1
  • PostValidatorResponse: posts a Validator response
    1. 1.
      Validator response payload
    2. 2.
      Validator's digital signature on 1
A piece of block data may contain one of these functions' invocations and their parameters.

Extracting the transactions

It is possible to extract the invoked smart contract functions and parameters from block data. The resulting value is an array of arrays of strings where the first strings in the inner arrays are the functions and the succeeding strings are the parameters. Since the blocks are protocol-buffer-encoded, we need to use a library protobufjs for decoding.
1
const {Type, Field} = require('protobufjs');
2
3
const protoChaincodeInput = new Type('ChaincodeInput')
4
.add(new Field('args', 1, 'string', 'repeated'));
5
6
const protoChaincodeSpec = new Type('ChaincodeSpec')
7
.add(new Field('input', 3, 'ChaincodeInput')).add(protoChaincodeInput);
8
9
const protoChaincodeProposalPayloadInput = new Type('ChaincodeProposalPayloadInput')
10
.add(new Field('chaincodeSpec', 1, 'ChaincodeSpec')).add(protoChaincodeSpec);
11
12
const protoChaincodeProposalPayload = new Type('ChaincodeProposalPayload')
13
.add(new Field('input', 1, 'ChaincodeProposalPayloadInput')).add(protoChaincodeProposalPayloadInput);
14
15
const protoChaincodeActionPayload = new Type('ChaincodeActionPayload')
16
.add(new Field('chaincodeProposalPayload', 1, 'ChaincodeProposalPayload')).add(protoChaincodeProposalPayload);
17
18
const protoTransactionAction = new Type('TransactionAction')
19
.add(new Field('payload', 2, 'ChaincodeActionPayload')).add(protoChaincodeActionPayload);
20
21
const protoTransaction = new Type('Transaction')
22
.add(new Field('actions', 1, 'TransactionAction', 'repeated')).add(protoTransactionAction);
23
24
const protoPayload = new Type('Payload')
25
.add(new Field('data', 2, 'Transaction')).add(protoTransaction);
26
27
const protoEnvelope = new Type('Envelope')
28
.add(new Field('payload', 1, 'Payload')).add(protoPayload);
29
30
function getTransactions(bytes) {
31
const txs = [];
32
const envelope = protoEnvelope.decode(bytes);
33
const actions = envelope.toJSON().payload.data.actions;
34
for (const action of actions) {
35
txs.push(action.payload.chaincodeProposalPayload
36
.input.chaincodeSpec.input.args);
37
}
38
return txs;
39
}
40
41
const block = {
42
header: {
43
// ...
44
},
45
data: {
46
data: [
47
'CpsDEpgDCpUDEpIDCo8DCowDCokDGoYDChNQb3N0VHJ1c3RlZVJlc3BvbnNlCrIBeyJ0eXBlIjoidHJ1c3RlZV9yZXNwb25zZSIsInBhZE5hbWUiOiJteS1wYWQtMS4wIiwidHJ1c3RlZUlkIjoidHJ1c3RlZTEiLCJ0b2tlbiI6ImYxMzQxZmM5MDU5Zjg1N2MyMThmNTEwNzcxZTJjODlmIiwidHJ1c3RlZVNoYXJlIjoiQWtGdVdiQUcxU2NjbDY5UlduMnhIU2lRUGRsaFlvbVpuRnF5SE81bFFEZGwifQq5AXsic2lnbmVyTWV0YWRhdGEiOnsiaWQiOiJ0cnVzdGVlMSIsImZ1bGxOYW1lIjoiVHJ1c3RlZS0xIiwicm9sZSI6IlRydXN0ZWUifSwicGF5bG9hZCI6Ik1FWUNJUUNCMHJFa2FvZGR6VWQ0eXVJUmFuTlZ5ZVlDK21JZmxPSm5Mdm82RnF0QXRBSWhBSys3cWxtenJnZ1diRTEzVDJvcUErNS9wSUdiZmluZFl3OEFTbU1xV21vNSJ9',
48
'CkESPwo9EjsKOQo3CjUaMwoPUG9zdERhdGFSZXF1ZXN0CiAyMWJmNTdkZDUyYTNiODEzMjhmZTEzOGFjNGU4ZWQxYg==',
49
],
50
},
51
}
52
const blockData = block.data.data;
53
54
const data0B64 = blockData[0];
55
const data0 = Buffer.from(data0B64, 'base64');
56
57
console.log(getTransactions(data0));
58
// [
59
// [
60
// 'PostTrusteeResponse',
61
// '{"type":"trustee_response","padName":"my-pad-1.0","trusteeId":"trustee1","token":"f1341fc9059f857c218f510771e2c89f","trusteeShare":"AkFuWbAG1Sccl69RWn2xHSiQPdlhYomZnFqyHO5lQDdl"}',
62
// '{"signerMetadata":{"id":"trustee1","fullName":"Trustee-1","role":"Trustee"},"payload":"MEYCIQCB0rEkaoddzUd4yuIRanNVyeYC+mIflOJnLvo6FqtAtAIhAK+7qlmzrggWbE13T2oqA+5/pIGbfindYw8ASmMqWmo5"}'
63
// ]
64
// ]
65
66
const data1B64 = blockData[1];
67
const data1 = Buffer.from(base1B64, 'base64');
68
69
console.log(getTransactions(data1));
70
// [
71
// [
72
// 'PostTrusteeResponse',
73
// '21bf57dd52a3b81328fe138ac4e8ed1b'
74
// ]
75
// ]
Copied!

Examples of transactions and parameters

Coming soon!