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.
const headlessWallet = require('headless-obyte');
const eventBus = require('ocore/event_bus.js');
function postData() {
let json_data = {
age: 78.90,
props: {
sets: [
'0bbb',
'zzz',
1/3
]
}
};
let objMessage = {
app: 'data',
payload_location: 'inline',
payload: json_data
};
let opts = {
messages: [objMessage]
};
​headlessWallet.issueChangeAddressAndSendMultiPayment(opts, (err, unit) => {
if (err){
/*
something went wrong,
maybe put this transaction on a retry queue
*/
return;
}
// handle successful payment
});
}
eventBus.on('headless_wallet_ready', postData);
Key-value data feed
const headlessWallet = require('headless-obyte');
const eventBus = require('ocore/event_bus.js');
function postDataFeed(first_address) {
let json_data = {
time: new Date().toString(),
timestamp: Date.now()
};
let objMessage = {
app: 'data_feed',
payload_location: 'inline',
payload: json_data
};
let opts = {
paying_addresses: [first_address],
messages: [objMessage]
};
​headlessWallet.issueChangeAddressAndSendMultiPayment(opts, (err, unit) => {
if (err){
/*
something went wrong,
maybe put this transaction on a retry queue
*/
return;
}
// handle successful payment
});
}
eventBus.once('headless_wallet_ready', () => {
headlessWallet.readFirstAddress(postDataFeed);
});
Profile about yourself
let json_data = {
name: 'a',
about: 'b',
location: 'c',
website: 'd',
email: 'e'
};
let objMessage = {
app: 'profile',
payload_location: 'inline',
payload: json_data
};
Attestation profile
Private/Public attestations
const conf = require('ocore/conf.js');
const objectHash = require('ocore/object_hash.js');
let bPublic = false; // ask user if they want public or private profile
function hideProfile(profile, bPublic = false){
const composer = require('ocore/composer.js');
let hidden_profile = {};
let src_profile = {};
for (let field in profile){
let value = profile[field];
let blinding = composer.generateBlinding();
let hidden_value = objectHash.getBase64Hash([value, blinding], true);
hidden_profile[field] = hidden_value;
src_profile[field] = [value, blinding];
}
let shortProfile = {
first_name: profile.first_name,
last_name: profile.last_name,
dob: profile.dob,
country: profile.country,
};
let public_profile;
if (bPublic) {
public_profile = profile;
public_profile.user_id = objectHash.getBase64Hash([shortProfile, conf.salt]);
return [public_profile, null];
}
else {
public_profile = {
profile_hash: objectHash.getBase64Hash(hidden_profile, true),
user_id: objectHash.getBase64Hash([shortProfile, conf.salt])
};
return [public_profile, src_profile];
}
}
let profile = {
first_name: "", // required
last_name: "", // required
dob: "", // required
country: "", // required
us_state: "", // optional
id_type: "" // optional
};
for (let field in profile){
if (typeof profile[field] === 'undefined' || profile[field] === null || profile[field] === '') {
// remove negative values, except number 0
delete profile[field];
}
else {
// convert everything else to string
profile[field] = String(profile[field]);
}
}
let [public_data, src_profile] = hideProfile(profile, bPublic);
let json_data = {
address: 'ABCDEF...',
profile: public_data
};
let objMessage = {
app: 'attestation',
payload_location: 'inline',
payload: json_data
};
Public only attestations
let json_data = {
address: 'ABCDEF...',
profile: {
username: ""
}
};
let objMessage = {
app: 'attestation',
payload_location: 'inline',
payload: json_data
};
Poll question and choices
let json_data = {
question: 'abc?',
choices: [
'a',
'b',
'c',
]
};
let objMessage = {
app: 'poll',
payload_location: 'inline',
payload: json_data
};
Plain text data
And last and not the least, it is possible to post plain text to DAG too.
let text_data = '';
let objMessage = {
app: 'text',
payload_location: 'inline',
payload: text_data
};
More examples
Last updated
Was this helpful?