0.1.4 • Published 4 years ago
identity-client-ts v0.1.4
identity-client-ts
WIP 使用方法
Get
アカウントごとにノードに登録されているキーと値の一覧を取得します。キーの指定はできません。
import { CosmosSDK } from "cosmos-client-ts";
import { Identity } from "identity-client-ts/lib/x/identity";
const url: string = "ノードのRESTサーバのURL:1317"; // ポート番号 1317 はデフォルト
const chainId: string = "チェーンID";
const sdk = new CosmosSDK(url, chainId);
const address: string = "アカウントのアドレス";
async function getIdentity() {
try {
let result = await Identity.get(sdk, address);
console.log(result);
} catch (error) {
console.error("*** Error:", error);
}
}
getIdentity();
// 次の記述でもOK(set、import、removeでも同様)
Identity.get(sdk, address)
.then(result => {
console.log(result);
})
.catch(e => {
console.error("エラー内容:", e);
});
{ height: '0', result: { color: 'red' } }
Set
import { CosmosSDK } from "cosmos-client-ts";
import { Auth } from "cosmos-client-ts/lib/x/auth/index";
import { BaseReq } from "cosmos-client-ts/lib/types/cosmos-sdk/rest";
import { Coin } from "cosmos-client-ts/lib/types/cosmos-sdk/coin";
import { DecCoin } from "cosmos-client-ts/lib/types/cosmos-sdk/deccoin";
import { Identity } from "identity-client-ts/lib/x/identity";
import { SetReq } from "identity-client-ts/lib/x/identity/types/set-req";
const url: string = "ノードのRESTサーバのURL:1317"; // ポート番号 1317 はデフォルト
const chainId: string = "チェーンID";
const sdk = new CosmosSDK(url, chainId);
const address: string = "アカウントのアドレス";
class MyCoin implements Coin {
denom = "stake";
amount = "200000";
}
class MyDecCoin implements DecCoin {
denom = "";
amount = "0";
}
let defaultValues = (): BaseReq => ({
from: address,
memo: "",
chain_id: chainId,
account_number: "0",
sequence: "0",
fees: [new MyCoin()],
gas_prices: [new MyDecCoin()],
gas: "0",
gas_adjustment: "",
simulate: false
});
async function setIdentity() {
let params: SetReq = {
key: "somekey",
value: "somevalue",
base_req: defaultValues()
};
try {
Auth.init();
let tx = await Identity.set(sdk, address, params);
console.log("fee", tx.fee);
console.log("msg", tx.msg);
console.log("signature", tx.signatures);
console.log("memo", tx.memo);
} catch (error) {
console.error("*** Error:", error);
}
}
setIdentity();
結果例
fee { amount: [ { denom: 'stake', amount: '200000' } ], gas: '0' }
msg [ { type: 'identity/MsgSet',
value:
{ address: 'cosmos15g0309kcs0nfed829cwyc07s6ydpaalel6676h',
key: 'somekey',
value: 'somevalue' } } ]
signature null
memo
Import
import { CosmosSDK } from "cosmos-client-ts";
import { Auth } from "cosmos-client-ts/lib/x/auth/index";
import { BaseReq } from "cosmos-client-ts/lib/types/cosmos-sdk/rest";
import { Coin } from "cosmos-client-ts/lib/types/cosmos-sdk/coin";
import { DecCoin } from "cosmos-client-ts/lib/types/cosmos-sdk/deccoin";
import { Identity } from "identity-client-ts/lib/x/identity";
import { ImportReq } from "identity-client-ts/lib/x/identity/types/import-req";
/*
bob's address
cosmos15g0309kcs0nfed829cwyc07s6ydpaalel6676h
alice's address
cosmos16jzfjpqfnpvx6m25tjnujwngfy5qxhd56rgh3v
*/
const url: string = "http://133.130.77.80:1317";
const chainId: string = "t";
const sdk = new CosmosSDK(url, chainId);
const bob_address: string = "cosmos15g0309kcs0nfed829cwyc07s6ydpaalel6676h";
class MyCoin implements Coin {
denom = "stake";
amount = "200000";
}
class MyDecCoin implements DecCoin {
denom = "";
amount = "0";
}
let defaultValues = (): BaseReq => ({
from: bob_address,
memo: "",
chain_id: chainId,
account_number: "0",
sequence: "0",
fees: [new MyCoin()],
gas_prices: [new MyDecCoin()],
gas: "0",
gas_adjustment: "",
simulate: false
});
async function importIdentity() {
const alice_address = "cosmos16jzfjpqfnpvx6m25tjnujwngfy5qxhd56rgh3v";
const params: ImportReq = {
from_address: alice_address,
base_req: defaultValues()
};
try {
Auth.init();
const tx = await Identity.post(sdk, alice_address, params);
console.log("fee", tx.fee);
console.log("msg", tx.msg);
console.log("signature", tx.signatures);
console.log("memo", tx.memo);
} catch (error) {
console.error("*** Error:", error);
}
}
importIdentity();
結果例
fee { amount: [ { denom: 'stake', amount: '200000' } ], gas: '0' }
msg [ { type: 'identity/MsgImport',
value:
{ from_address: 'cosmos16jzfjpqfnpvx6m25tjnujwngfy5qxhd56rgh3v',
to_address: 'cosmos16jzfjpqfnpvx6m25tjnujwngfy5qxhd56rgh3v' } } ]
signature null
memo
Delete
import { CosmosSDK } from "cosmos-client-ts";
import { Auth } from "cosmos-client-ts/lib/x/auth/index";
import { BaseReq } from "cosmos-client-ts/lib/types/cosmos-sdk/rest";
import { Coin } from "cosmos-client-ts/lib/types/cosmos-sdk/coin";
import { DecCoin } from "cosmos-client-ts/lib/types/cosmos-sdk/deccoin";
import { Identity } from "identity-client-ts/lib/x/identity";
import { DeleteReq } from "identity-client-ts/lib/x/identity/types/delete-req";
const url: string = "ノードのRESTサーバのURL:1317"; // ポート番号 1317 はデフォルト
const chainId: string = "チェーンID";
const sdk = new CosmosSDK(url, chainId);
const address: string = "アカウントのアドレス";
class MyCoin implements Coin {
denom = "stake";
amount = "200000";
}
class MyDecCoin implements DecCoin {
denom = "";
amount = "0";
}
let defaultValues = (): BaseReq => ({
from: address,
memo: "",
chain_id: chainId,
account_number: "0",
sequence: "0",
fees: [new MyCoin()],
gas_prices: [new MyDecCoin()],
gas: "0",
gas_adjustment: "",
simulate: false
});
async function importIdentity() {
let params: DeleteReq = {
base_req: defaultValues()
};
try {
Auth.init();
let tx = await Identity.remove(sdk, address, params);
console.log("fee", tx.fee);
console.log("msg", tx.msg);
console.log("signature", tx.signatures);
console.log("memo", tx.memo);
} catch (error) {
console.error("*** Error:", error);
}
}
importIdentity();
結果例
fee { amount: [ { denom: 'stake', amount: '200000' } ], gas: '0' }
msg [ { type: 'identity/MsgDelete',
value: { address: 'cosmos15g0309kcs0nfed829cwyc07s6ydpaalel6676h' } } ]
signature null
memo