2022-04-01 12:32:56 +00:00
|
|
|
import isUrl from 'is-url';
|
|
|
|
import { sha256 } from 'js-sha256';
|
2022-04-05 14:11:06 +00:00
|
|
|
import { generatePostBodyBroadcast, BroadcastMode } from '@tharsis/provider';
|
2022-03-30 05:21:03 +00:00
|
|
|
import {
|
|
|
|
Chain,
|
2022-03-31 10:00:02 +00:00
|
|
|
Sender,
|
2022-04-01 12:32:56 +00:00
|
|
|
Fee,
|
2022-04-05 09:20:12 +00:00
|
|
|
createMessageSend,
|
|
|
|
MessageSendParams
|
2022-03-30 05:21:03 +00:00
|
|
|
} from '@tharsis/transactions'
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
import { RegistryClient } from "./registry-client";
|
|
|
|
import { Account } from "./account";
|
|
|
|
import { createTransaction } from "./txbuilder";
|
2022-04-08 05:29:03 +00:00
|
|
|
import { Payload, Record } from './types';
|
2022-04-12 06:08:16 +00:00
|
|
|
import { Util } from './util';
|
|
|
|
import {
|
|
|
|
createTxMsgCancelBond,
|
|
|
|
createTxMsgCreateBond,
|
|
|
|
createTxMsgRefillBond,
|
|
|
|
createTxMsgWithdrawBond,
|
|
|
|
MessageMsgCancelBond,
|
|
|
|
MessageMsgCreateBond,
|
|
|
|
MessageMsgRefillBond,
|
|
|
|
MessageMsgWithdrawBond
|
|
|
|
} from "./messages/bond";
|
|
|
|
import {
|
|
|
|
createTxMsgDeleteName,
|
|
|
|
createTxMsgReserveAuthority,
|
|
|
|
createTxMsgSetAuthorityBond,
|
|
|
|
createTxMsgSetName,
|
|
|
|
createTxMsgSetRecord,
|
|
|
|
MessageMsgDeleteName,
|
|
|
|
MessageMsgReserveAuthority,
|
|
|
|
MessageMsgSetAuthorityBond,
|
|
|
|
MessageMsgSetName,
|
|
|
|
MessageMsgSetRecord,
|
|
|
|
NAMESERVICE_ERRORS
|
|
|
|
} from './messages/nameservice';
|
|
|
|
import {
|
|
|
|
createTxMsgCommitBid,
|
|
|
|
createTxMsgRevealBid,
|
|
|
|
MessageMsgCommitBid,
|
|
|
|
MessageMsgRevealBid
|
|
|
|
} from './messages/auction';
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
const DEFAULT_WRITE_ERROR = 'Unable to write to chiba-clonk.';
|
|
|
|
|
2022-04-06 04:46:38 +00:00
|
|
|
export const DEFAULT_CHAIN_ID = 'chibaclonk_9000-1';
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
// Parse Tx response from cosmos-sdk.
|
|
|
|
export const parseTxResponse = (result: any) => {
|
|
|
|
const { txhash: hash, height, ...txResponse } = result;
|
|
|
|
txResponse.data = txResponse.data && Buffer.from(txResponse.data, 'base64').toString('utf8');
|
2022-03-29 11:01:23 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
txResponse.events.forEach((event:any) => {
|
|
|
|
event.attributes = event.attributes.map(({ key, value }: { key: string, value: string }) => ({
|
|
|
|
key: Buffer.from(key, 'base64').toString('utf8'),
|
|
|
|
value: Buffer.from(value, 'base64').toString('utf8')
|
|
|
|
}));
|
|
|
|
});
|
2022-03-29 11:01:23 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
return { hash, height, ...txResponse };
|
|
|
|
};
|
2022-03-29 11:01:23 +00:00
|
|
|
|
2022-04-12 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Create an auction bid.
|
|
|
|
*/
|
|
|
|
export const createBid = async (chainId: string, auctionId: string, bidderAddress: string, bidAmount: string, noise?: string) => {
|
|
|
|
if (!noise) {
|
|
|
|
noise = Account.generateMnemonic();
|
|
|
|
}
|
|
|
|
|
|
|
|
const reveal = {
|
|
|
|
chainId,
|
|
|
|
auctionId,
|
|
|
|
bidderAddress,
|
|
|
|
bidAmount,
|
|
|
|
noise
|
|
|
|
};
|
|
|
|
|
|
|
|
const commitHash = await Util.getContentId(reveal);
|
|
|
|
const revealString = Buffer.from(JSON.stringify(reveal)).toString('hex');
|
|
|
|
|
|
|
|
return {
|
|
|
|
commitHash,
|
|
|
|
reveal,
|
|
|
|
revealString
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
export const isKeyValid = (key: string) => key && key.match(/^[0-9a-fA-F]{64}$/);
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
export class Registry {
|
|
|
|
_endpoint: string
|
|
|
|
_chain: Chain
|
|
|
|
_client: RegistryClient
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
static processWriteError(error: string) {
|
|
|
|
// error string a stacktrace containing the message.
|
|
|
|
// https://gist.github.com/nikugogoi/de55d390574ded3466abad8bffd81952#file-txresponse-js-L7
|
|
|
|
const errorMessage = NAMESERVICE_ERRORS.find(message => error.includes(message))
|
|
|
|
|
|
|
|
return errorMessage || DEFAULT_WRITE_ERROR;
|
2022-03-29 11:01:23 +00:00
|
|
|
}
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
constructor(restUrl: string, gqlUrl: string, cosmosChainId = DEFAULT_CHAIN_ID) {
|
|
|
|
if (!isUrl(restUrl)) {
|
|
|
|
throw new Error('Path to a REST endpoint should be provided.');
|
2022-04-01 12:32:56 +00:00
|
|
|
}
|
2022-03-30 05:21:03 +00:00
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
if (!isUrl(gqlUrl)) {
|
|
|
|
throw new Error('Path to a GQL endpoint should be provided.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._endpoint = restUrl;
|
|
|
|
this._client = new RegistryClient(restUrl, gqlUrl);
|
2022-03-30 05:21:03 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
this._chain = {
|
|
|
|
chainId: 9000,
|
|
|
|
cosmosChainId
|
|
|
|
}
|
2022-03-30 05:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Get account by addresses.
|
|
|
|
*/
|
2022-04-12 06:08:16 +00:00
|
|
|
async getAccount(address: string) {
|
2022-04-01 12:32:56 +00:00
|
|
|
return this._client.getAccount(address);
|
2022-03-30 05:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Get records by attributes.
|
|
|
|
*/
|
|
|
|
async queryRecords(attributes: {[key: string]: any}, all = false, refs = false) {
|
|
|
|
return this._client.queryRecords(attributes, all, refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve names to records.
|
|
|
|
*/
|
|
|
|
async resolveNames(names: string[], refs = false) {
|
|
|
|
return this._client.resolveNames(names, refs);
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
|
|
|
* Publish record.
|
|
|
|
* @param transactionPrivateKey - private key in HEX to sign transaction.
|
|
|
|
*/
|
|
|
|
async setRecord(
|
|
|
|
params: { privateKey: string, record: any, bondId: string },
|
|
|
|
transactionPrivateKey: string,
|
|
|
|
fee: Fee
|
|
|
|
) {
|
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
result = await this._submitRecordTx(params, transactionPrivateKey, fee);
|
2022-04-08 05:29:03 +00:00
|
|
|
|
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-05 09:20:12 +00:00
|
|
|
/**
|
|
|
|
* Send coins.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async sendCoins(params: MessageSendParams, privateKey: string, fee: Fee) {
|
2022-04-05 09:20:12 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-05 09:20:12 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-05 09:20:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createMessageSend(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-05 09:20:12 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Computes the next bondId for the given account private key.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async getNextBondId(privateKey: string) {
|
2022-04-01 12:32:56 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountObj } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const nextSeq = parseInt(accountObj.sequence, 10) + 1;
|
|
|
|
result = sha256(`${accountObj.address}:${accountObj.account_number}:${nextSeq}`);
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
return result;
|
2022-03-31 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
/**
|
|
|
|
* Get bonds by ids.
|
|
|
|
*/
|
2022-04-12 06:08:16 +00:00
|
|
|
async getBondsByIds(ids: string[]) {
|
2022-04-04 07:05:16 +00:00
|
|
|
return this._client.getBondsByIds(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query bonds by attributes.
|
|
|
|
*/
|
2022-04-12 06:08:16 +00:00
|
|
|
async queryBonds(attributes = {}) {
|
2022-04-04 07:05:16 +00:00
|
|
|
return this._client.queryBonds(attributes);
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Create bond.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async createBond(params: MessageMsgCreateBond, privateKey: string, fee: Fee) {
|
2022-04-01 12:32:56 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-01 12:32:56 +00:00
|
|
|
}
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgCreateBond(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
return parseTxResponse(result);
|
2022-03-31 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:20:20 +00:00
|
|
|
/**
|
|
|
|
* Refill bond.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async refillBond(params: MessageMsgRefillBond, privateKey: string, fee: Fee) {
|
2022-04-04 10:20:20 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-04 10:20:20 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-04 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgRefillBond(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-04 10:20:20 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Withdraw (from) bond.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async withdrawBond(params: MessageMsgWithdrawBond, privateKey: string, fee: Fee) {
|
2022-04-04 10:20:20 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-04 10:20:20 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-04 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgWithdrawBond(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-04 10:20:20 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel bond.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async cancelBond(params: MessageMsgCancelBond, privateKey: string, fee: Fee) {
|
2022-04-04 10:20:20 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-04 10:20:20 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-04 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgCancelBond(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-04 10:20:20 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
/**
|
|
|
|
* Reserve authority.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async reserveAuthority(params: { name: string, owner?: string }, privateKey: string, fee: Fee) {
|
2022-04-05 05:31:10 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-05 05:31:10 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-05 05:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 10:54:26 +00:00
|
|
|
const msgParams = {
|
|
|
|
name: params.name,
|
|
|
|
// TODO: Pass empty string as owner.
|
|
|
|
owner: params.owner || sender.accountAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
const msg = createTxMsgReserveAuthority(this._chain, sender, fee, '', msgParams)
|
2022-04-11 06:58:14 +00:00
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
|
|
|
* Set authority bond.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async setAuthorityBond(params: MessageMsgSetAuthorityBond, privateKey: string, fee: Fee) {
|
2022-04-08 05:29:03 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-08 05:29:03 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgSetAuthorityBond(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-12 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Commit auction bid.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async commitBid(params: MessageMsgCommitBid, privateKey: string, fee: Fee) {
|
2022-04-12 06:08:16 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-12 06:08:16 +00:00
|
|
|
|
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-12 06:08:16 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-12 06:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const msg = createTxMsgCommitBid(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reveal auction bid.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async revealBid(params: MessageMsgRevealBid, privateKey: string, fee: Fee) {
|
2022-04-12 06:08:16 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-12 06:08:16 +00:00
|
|
|
|
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-12 06:08:16 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-12 06:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const msg = createTxMsgRevealBid(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get records by ids.
|
|
|
|
*/
|
|
|
|
async getAuctionsByIds(ids: string[]) {
|
|
|
|
return this._client.getAuctionsByIds(ids);
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
/**
|
|
|
|
* Lookup authorities by names.
|
|
|
|
*/
|
2022-04-08 05:29:03 +00:00
|
|
|
async lookupAuthorities(names: string[], auction = false) {
|
2022-04-05 05:31:10 +00:00
|
|
|
return this._client.lookupAuthorities(names, auction);
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
|
|
|
* Set name (WRN) to record ID (CID).
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async setName(params: MessageMsgSetName, privateKey: string, fee: Fee) {
|
2022-04-08 05:29:03 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-08 05:29:03 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgSetName(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Lookup naming information.
|
|
|
|
*/
|
|
|
|
async lookupNames(names: string[], history = false) {
|
|
|
|
return this._client.lookupNames(names, history);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete name (WRN) mapping.
|
|
|
|
*/
|
2022-04-12 10:54:26 +00:00
|
|
|
async deleteName(params: MessageMsgDeleteName, privateKey: string, fee: Fee) {
|
2022-04-08 10:32:24 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-11 06:58:14 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-08 10:32:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
const msg = createTxMsgDeleteName(this._chain, sender, fee, '', params)
|
|
|
|
result = await this._submitTx(msg, privateKey, sender);
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
return parseTxResponse(result);
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
|
|
|
* Submit record transaction.
|
|
|
|
* @param privateKey - private key in HEX to sign message.
|
|
|
|
* @param txPrivateKey - private key in HEX to sign transaction.
|
|
|
|
*/
|
|
|
|
async _submitRecordTx(
|
|
|
|
{ privateKey, record, bondId }: { privateKey: string, record: any, bondId: string },
|
|
|
|
txPrivateKey: string,
|
|
|
|
fee: Fee
|
|
|
|
) {
|
|
|
|
if (!isKeyValid(privateKey)) {
|
|
|
|
throw new Error('Registry privateKey should be a hex string.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isKeyValid(bondId)) {
|
|
|
|
throw new Error(`Invalid bondId: ${bondId}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign record.
|
|
|
|
const recordSignerAccount = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const registryRecord = new Record(record);
|
|
|
|
const payload = new Payload(registryRecord);
|
|
|
|
await recordSignerAccount.signPayload(payload);
|
|
|
|
|
|
|
|
// Send record payload Tx.
|
2022-04-12 10:54:26 +00:00
|
|
|
return this._submitRecordPayloadTx({ payload, bondId }, txPrivateKey, fee);
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 10:54:26 +00:00
|
|
|
async _submitRecordPayloadTx(params: MessageMsgSetRecord, privateKey: string, fee: Fee) {
|
2022-04-08 05:29:03 +00:00
|
|
|
if (!isKeyValid(privateKey)) {
|
|
|
|
throw new Error('Registry privateKey should be a hex string.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isKeyValid(params.bondId)) {
|
|
|
|
throw new Error(`Invalid bondId: ${params.bondId}.`);
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress);
|
2022-04-08 05:29:03 +00:00
|
|
|
|
|
|
|
const sender = {
|
2022-04-12 10:54:26 +00:00
|
|
|
accountAddress: account.formattedCosmosAddress,
|
2022-04-08 05:29:03 +00:00
|
|
|
sequence: accountInfo.sequence,
|
|
|
|
accountNumber: accountInfo.account_number,
|
2022-04-12 10:54:26 +00:00
|
|
|
pubkey: account.encodedPubkey,
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const msg = createTxMsgSetRecord(this._chain, sender, fee, '', params)
|
|
|
|
return this._submitTx(msg, privateKey, sender);
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Submit a generic Tx to the chain.
|
|
|
|
*/
|
|
|
|
async _submitTx(message: any, privateKey: string, sender: Sender) {
|
|
|
|
// Check private key.
|
|
|
|
if (!isKeyValid(privateKey)) {
|
|
|
|
throw new Error('Registry privateKey should be a hex string.');
|
|
|
|
}
|
2022-04-01 03:31:00 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
// Check that the account exists on-chain.
|
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2022-04-01 03:31:00 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
// Generate signed Tx.
|
|
|
|
const transaction = createTransaction(message, account, sender, this._chain);
|
2022-04-01 03:31:00 +00:00
|
|
|
|
2022-04-05 14:11:06 +00:00
|
|
|
const tx = generatePostBodyBroadcast(transaction, BroadcastMode.Block)
|
2022-04-01 03:31:00 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
// Submit Tx to chain.
|
|
|
|
const { tx_response: response } = await this._client.submit(tx);
|
2022-04-11 06:58:14 +00:00
|
|
|
|
|
|
|
if (response.code !== 0) {
|
|
|
|
// Throw error when transaction is not successful.
|
|
|
|
// https://docs.starport.com/guide/nameservice/05-play.html#buy-name-transaction-details
|
|
|
|
throw new Error(Registry.processWriteError(response.raw_log))
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
return response;
|
2022-04-01 03:31:00 +00:00
|
|
|
}
|
2022-03-24 06:40:03 +00:00
|
|
|
}
|
2022-04-05 05:31:10 +00:00
|
|
|
|
|
|
|
export { Account }
|