Skip to main content

Developer Guide

Integrate GID in DApp

GID developers provide a complete in-app integration method, as detailed below:

// Register gid
const { GotaBit } = GotaBitJS;

const mnemonic =
"dinner proud piano mention silk plunge forest fold trial duck electric define";

const gotabit = await GotaBit.init('test', mnemonic);

const [from_wallet] = await gotabit.wallet.getAccounts();

const client = await gotabit.wasmClient(true);

const controller_address =
"gio1ghd753shjuwexxywmgs4xz7x2q732vcnkm6h2pyv9s6ah3hylvrqhhpzlg";
const resolver_address =
"gio1wr6vc3g4caz9aclgjacxewr0pjlre9wl2uhq73rp8mawwmqaczsql8xyyu";

console.info("controller_address: ", controller_address);

const name = "alice124";
const denom = "ugtb";
const owner = from_wallet.address;
const secret = "s12345678";
const duration = 63072000;

const rent_price = await client.queryContractSmart(controller_address, {
rent_price: {
name: name,
duration: duration,
},
});

console.info("rent_price: ", rent_price);

const gid_coin = [
{
amount: rent_price.price,
denom: denom,
},
];

async function register() {
let get_commitment,
commit_commitment,
register_result,
tx_fee = "auto";
try {
get_commitment = await client.queryContractSmart(controller_address, {
get_commitment: {
owner: owner,
name: name,
secret: secret,
resolver: resolver_address,
},
});
console.log("get_commitment: ", get_commitment);
} catch (e) {
console.info("get_commitment_error");
return;
}

try {
commit_commitment = await client.execute(
from_wallet.address,
controller_address,
{ commit: { commitment: get_commitment.commitment } },
tx_fee
);

console.info("commit_commitment: ", commit_commitment);
} catch (e) {
console.info("commit_commitment_error");
return;
}

try {
register_result = await client.execute(
from_wallet.address,
controller_address,
{
register: {
owner: owner,
name: name,
duration: duration,
secret: secret,
resolver: resolver_address,
},
},
tx_fee,
"register gid",
gid_coin
);
console.info("register_info", register_result);
} catch (e) {
console.info("register_error");
}
}

register();

GID Universal SDK

GID SDK pseudo-code

const mnemonic = "midnight donkey dose rebel chunk bullet shed library aspect holiday control clump";

const gid = GID.init(mnemonic, Config);

const res = gid.query("bob.gid");

const record = {
address: 'address',
key: 'key',
info: 'info',
}

// Query bob's gotabit address.
console.log(res.get(record.address));

// Query bob's public encrypt key.
console.log(res.get(record.key).encrypt);

// Query bob's nickname.
console.log(res.get(record.info).nickname);

// gid set address
const gotabit = {
name: "gotabit",
address: "gio12jh7jf396jvqhjckp5lygk8am3255u73m3qm5e",
};

gid.set(record.address, gotabit);

// gid set key
const key = {
name: 'encrypt',
value: [
{
name: 'default',
key: '02fa2e854d757c810a4b9f805d68c532c3b0068b15afee44a5424aef88b96a234f',
type: 'ecies'
},
{
name: 'chat',
key: '02fa2e854d757c810a4b9f805d68c532c3b0068b15afee44a5424aef88b96a234f',
type: 'ecies'
}
]
};
gid.set(record.key, key);

// gid set info
const info = {
name: 'nickname',
value: 'Bob'
}
gid.set(record.info, info);

// gid set txt
gid.set('txt', 'gio1t3538w4e689r5me07rjuunpgcrrw8de7zvzfnl');

API access

registered name

// Register gid
const { GotaBit } = GotaBitJS;

const mnemonic =
"dinner proud piano mention silk plunge forest fold trial duck electric define";

const gotabit = await GotaBit.init('test', mnemonic);

const [from_wallet] = await gotabit.wallet.getAccounts();

const client = await gotabit.wasmClient(true);

const controller_address =
"gio1ghd753shjuwexxywmgs4xz7x2q732vcnkm6h2pyv9s6ah3hylvrqhhpzlg";
const resolver_address =
"gio1wr6vc3g4caz9aclgjacxewr0pjlre9wl2uhq73rp8mawwmqaczsql8xyyu";

console.info("controller_address: ", controller_address);

const name = "alice124";
const denom = "ugtb";
const owner = from_wallet.address;
const secret = "s12345678";
const duration = 63072000;

const rent_price = await client.queryContractSmart(controller_address, {
rent_price: {
name: name,
duration: duration,
},
});

console.info("rent_price: ", rent_price);

const gid_coin = [
{
amount: rent_price.price,
denom: denom,
},
];

async function register() {
let get_commitment,
commit_commitment,
register_result,
tx_fee = "auto";
try {
get_commitment = await client.queryContractSmart(controller_address, {
get_commitment: {
owner: owner,
name: name,
secret: secret,
resolver: resolver_address,
},
});
console.log("get_commitment: ", get_commitment);
} catch (e) {
console.info("get_commitment_error");
return;
}

try {
commit_commitment = await client.execute(
from_wallet.address,
controller_address,
{ commit: { commitment: get_commitment.commitment } },
tx_fee
);

console.info("commit_commitment: ", commit_commitment);
} catch (e) {
console.info("commit_commitment_error");
return;
}

try {
register_result = await client.execute(
from_wallet.address,
controller_address,
{
register: {
owner: owner,
name: name,
duration: duration,
secret: secret,
resolver: resolver_address,
},
},
tx_fee,
"register gid",
gid_coin
);
console.info("register_info", register_result);
} catch (e) {
console.info("register_error");
}
}

register();

resolve name

const { GotaBit } = GotaBitJS;

const mnemonic =
"dinner proud piano mention silk plunge forest fold trial duck electric define";
const gotabit = await GotaBit.init("test", mnemonic);

const [from_wallet] = await gotabit.wallet.getAccounts();

const client = await gotabit.client(true, true);
const tx_fee = "auto";

const key = "name";
const gid = "alice101";
const value = "alice";

const resolver_address =
"gio1eyfccmjm6732k7wp4p6gdjwhxjwsvje44j0hfx8nkgrm8fs7vqfsvq7yce";
const controller_address =
"gio1ghd753shjuwexxywmgs4xz7x2q732vcnkm6h2pyv9s6ah3hylvrqhhpzlg";

async function set_text() {
let nodehash;
try {
nodehash = await client.queryContractSmart(controller_address, {
get_nodehash: {
name: gid,
},
});
console.info("nodehash: ", nodehash);
} catch (e) {
console.error("failed to get nodehash");
return;
}

try {
const set_text_result = await client.execute(
from_wallet.address,
resolver_address,
{
set_text_data: {
node: nodehash.node,
key: key,
value: value,
},
},
tx_fee
);
console.info("set_text_result: ", set_text_result);
} catch (e) {
console.error("failed to set text");
console.error(e);
}
}

set_text();