Sending data to DAG
You can store arbitrary data (any sequence of bytes) in Obyte public DAG. Some special types of data also supported, like 'text', 'profile', 'poll', etc.

Data with any structure

To send data to DAG database, code below is a minimal that you can use. Since the opts.messages is array, it means that all the examples on this page can be changed to multi-message transactions.
1
const headlessWallet = require('headless-obyte');
2
const eventBus = require('ocore/event_bus.js');
3
4
function postData() {
5
let json_data = {
6
age: 78.90,
7
props: {
8
sets: [
9
'0bbb',
10
'zzz',
11
1/3
12
]
13
}
14
};
15
let objMessage = {
16
app: 'data',
17
payload_location: 'inline',
18
payload: json_data
19
};
20
let opts = {
21
messages: [objMessage]
22
};
23
24
​headlessWallet.issueChangeAddressAndSendMultiPayment(opts, (err, unit) => {
25
if (err){
26
/*
27
something went wrong,
28
maybe put this transaction on a retry queue
29
*/
30
return;
31
}
32
// handle successful payment
33
});
34
}
35
36
eventBus.on('headless_wallet_ready', postData);
Copied!
AAs can be triggered with data by manually entering the key/value pairs to GUI wallet, prompting users to submit pre-filled Send screen, making the AA to trigger another AA or with the script from headless wallet.

Key-value data feed

Previous example shows how to send any data to DAG, but if you would want to store data that is searchable and can be used in smart-contracts or autonomous agents then you would need to post it as key-value pairs and with app type data_feed. If we also send it always from the same first address in our wallet then we have basically become an oracle.
1
const headlessWallet = require('headless-obyte');
2
const eventBus = require('ocore/event_bus.js');
3
4
function postDataFeed(first_address) {
5
let json_data = {
6
time: new Date().toString(),
7
timestamp: Date.now()
8
};
9
let objMessage = {
10
app: 'data_feed',
11
payload_location: 'inline',
12
payload: json_data
13
};
14
let opts = {
15
paying_addresses: [first_address],
16
messages: [objMessage]
17
};
18
19
​headlessWallet.issueChangeAddressAndSendMultiPayment(opts, (err, unit) => {
20
if (err){
21
/*
22
something went wrong,
23
maybe put this transaction on a retry queue
24
*/
25
return;
26
}
27
// handle successful payment
28
});
29
}
30
31
eventBus.once('headless_wallet_ready', () => {
32
headlessWallet.readFirstAddress(postDataFeed);
33
});
Copied!

Profile about yourself

Anybody on Obyte network can post some data about themselves, which will be linked to their address. This app type is called profile. Payload for that message can contain any keys. For example, obyte.io web service lets you post a profile with keys like this:
1
let json_data = {
2
name: 'a',
3
about: 'b',
4
location: 'c',
5
website: 'd',
6
email: 'e'
7
};
8
let objMessage = {
9
app: 'profile',
10
payload_location: 'inline',
11
payload: json_data
12
};
Copied!

Attestation profile

Attestation profile is similar to previous one with one difference that attestation type message are posted by somebody and they describe somebody else. These profiles are used to do KYC by asking user to provide data about themselves from trusted attestor.

Private/Public attestations

Real Name Attestation bot creates an attestation profile, which keeps all the data private on user device while makes it possible for the others to recognize unique users by user_id (it's a salted hash of first_name, last_name, dob and country) and also makes it possible to validate with profile_hash if user provides them a private profile.
1
const conf = require('ocore/conf.js');
2
const objectHash = require('ocore/object_hash.js');
3
4
let bPublic = false; // ask user if they want public or private profile
5
6
function hideProfile(profile, bPublic = false){
7
const composer = require('ocore/composer.js');
8
let hidden_profile = {};
9
let src_profile = {};
10
for (let field in profile){
11
let value = profile[field];
12
let blinding = composer.generateBlinding();
13
let hidden_value = objectHash.getBase64Hash([value, blinding], true);
14
hidden_profile[field] = hidden_value;
15
src_profile[field] = [value, blinding];
16
}
17
let shortProfile = {
18
first_name: profile.first_name,
19
last_name: profile.last_name,
20
dob: profile.dob,
21
country: profile.country,
22
};
23
let public_profile;
24
if (bPublic) {
25
public_profile = profile;
26
public_profile.user_id = objectHash.getBase64Hash([shortProfile, conf.salt]);
27
return [public_profile, null];
28
}
29
else {
30
public_profile = {
31
profile_hash: objectHash.getBase64Hash(hidden_profile, true),
32
user_id: objectHash.getBase64Hash([shortProfile, conf.salt])
33
};
34
return [public_profile, src_profile];
35
}
36
}
37
38
let profile = {
39
first_name: "", // required
40
last_name: "", // required
41
dob: "", // required
42
country: "", // required
43
us_state: "", // optional
44
id_type: "" // optional
45
};
46
for (let field in profile){
47
if (typeof profile[field] === 'undefined' || profile[field] === null || profile[field] === '') {
48
// remove negative values, except number 0
49
delete profile[field];
50
}
51
else {
52
// convert everything else to string
53
profile[field] = String(profile[field]);
54
}
55
}
56
let [public_data, src_profile] = hideProfile(profile, bPublic);
57
let json_data = {
58
address: 'ABCDEF...',
59
profile: public_data
60
};
61
let objMessage = {
62
app: 'attestation',
63
payload_location: 'inline',
64
payload: json_data
65
};
Copied!
In case user would want to make their profile public like with Email Attestation and Steem Attestation, true could be sent as a second parameter of hideProfile() function. To avoid people doxxing themselves, the possibility to make public attestation with Real Name Attestation has not been included. This choice is only on Email Attestation and Steem Attestation.
In order for the user to share private profile with others, your bot should also send the private profile to user's wallet.

Public only attestations

If there is no plan to provide private attestations and all the attestation will always be public then there is no need for user_id. Then the attestation profile can be as simple as this, but the attestator needs to make sure that the same username attestation is not done to multiple users. One example of that kind is Username Attestation bot.
1
let json_data = {
2
address: 'ABCDEF...',
3
profile: {
4
username: ""
5
}
6
};
7
let objMessage = {
8
app: 'attestation',
9
payload_location: 'inline',
10
payload: json_data
11
};
Copied!
For public attestation, there is not private profile to send to user's wallet because the bots who need information about the user could simply just retrieve the attestation profile from the DAG.

Poll question and choices

Anybody can post a poll and let others vote on it, this can be done like this data and it will also appear on obyte.io web service from where it will direct users to Poll chat bot.
1
let json_data = {
2
question: 'abc?',
3
choices: [
4
'a',
5
'b',
6
'c',
7
]
8
};
9
let objMessage = {
10
app: 'poll',
11
payload_location: 'inline',
12
payload: json_data
13
};
Copied!

Plain text data

And last and not the least, it is possible to post plain text to DAG too.
1
let text_data = '';
2
let objMessage = {
3
app: 'text',
4
payload_location: 'inline',
5
payload: text_data
6
};
Copied!

More examples

Shorter code examples using composer library are available in "/tools" folder of headless-wallet.
If you wish to send data to DAG database periodically then full code examples for price oracle, bitcoin oracle or sports oracle can be found on Github.
Last modified 1yr ago