-
Notifications
You must be signed in to change notification settings - Fork 20
/
blob.js
120 lines (100 loc) · 3.12 KB
/
blob.js
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
import { DockAPI } from '@docknetwork/dock-blockchain-api';
import { DockCoreModules } from '@docknetwork/dock-blockchain-modules';
// The following can be tweaked depending on where the node is running and what
// account is to be used for sending the transaction.
import {
DidKey,
VerificationRelationship,
DockBlobId,
DockDid,
DIDDocument,
} from '@docknetwork/credential-sdk/types';
import {
Ed25519Keypair,
DidKeypair,
} from '@docknetwork/credential-sdk/keypairs';
async function writeAndReadBlob(blobModule, blobValue, did, pair) {
const blobId = DockBlobId.random();
console.log('Writing blob with id ', blobId, 'and value', blobValue);
const blob = {
id: blobId,
blob: blobValue,
};
await blobModule.new(blob, pair);
console.log('Blob written, reading from chain...');
return await blobModule.get(blobId);
}
async function createAuthorDID(didModule, pair) {
// Generate a DID to be used as author
const dockDID = DockDid.random();
console.log('Creating new author DID', dockDID);
// Create an author DID to write with
const publicKey = pair.publicKey();
const didKey = new DidKey(publicKey, new VerificationRelationship());
await didModule.createDocument(DIDDocument.create(dockDID, [didKey]));
return dockDID;
}
async function connectToNode() {
console.log('Connecting to the node...');
const dock = new DockAPI();
await dock.init({
address: process.env.FullNodeEndpoint || 'ws://127.0.0.1:9944',
});
console.log('Setting sdk account...');
const account = dock.keyring.addFromUri(
process.env.TestAccountURI || '//Alice',
);
dock.setAccount(account);
return dock;
}
async function main() {
// Connect to the node
const dock = await connectToNode();
const modules = new DockCoreModules(dock);
const keyPair = Ed25519Keypair.random();
// Generate a DID to be used as author
const dockDID = await createAuthorDID(modules.did, keyPair);
// Generate keypair for DID
const pair = new DidKeypair([dockDID, 1], keyPair);
// Write blob as json
const blobValueJSON = { jsonStorage: true };
const chainBlobJSON = await writeAndReadBlob(
modules.blob,
blobValueJSON,
dockDID,
pair,
);
const blobJSONFromChain = chainBlobJSON[1];
console.log('Resulting blob JSON from chain:', blobJSONFromChain);
// Write blob as string
const blobValue = 'hello blob storage!';
const chainBlob = await writeAndReadBlob(
modules.blob,
blobValue,
dockDID,
pair,
);
const blobStrFromChain = chainBlob[1].toString();
console.log('Resulting blob string from chain:', blobStrFromChain);
// Write blob as array
const blobValueArray = new Uint8Array([1, 2, 3]);
const chainBlobArray = await writeAndReadBlob(
modules.blob,
blobValueArray,
dockDID,
pair,
);
const blobArrayFromChain = chainBlobArray[1];
console.log('Resulting blob array from chain:', blobArrayFromChain);
// Finalize
console.log('All done, disconnecting...');
await dock.disconnect();
}
main()
.then(() => {
process.exit(0);
})
.catch((error) => {
console.error('Error occurred somewhere, it was caught!', error);
process.exit(1);
});