Get record id from setRecord message response

This commit is contained in:
nabarun 2022-04-20 17:24:37 +05:30 committed by Ashwin Phatak
parent 79b55b73da
commit 28c8099b16
7 changed files with 71 additions and 18 deletions

View File

@ -73,3 +73,14 @@ Follow these steps to run the tests:
## Development
[README](./DEVELOPMENT.md)
## Known Issues
- [Util](./src/util.ts) `getContentId` method does not generate same CID compared to that in chiba-clonk.
- Passing a float type value in [watcher attributes](./src/testing/data/watcher.yml) throws error when sending setRecord message.
```
failed to execute message; message index: 0: Invalid signature.: unauthorized
```
- When sending setRecord message, an integer value passed in watcher attributes is parsed as float type in chiba-clonk while [unmarshalling json](https://pkg.go.dev/encoding/json#Unmarshal).

View File

@ -43,6 +43,7 @@ message MsgSetRecord{
// MsgSetRecordResponse
message MsgSetRecordResponse{
string id = 1;
}
// Payload

View File

@ -35,7 +35,8 @@ import {
MessageMsgSetAuthorityBond,
MessageMsgSetName,
MessageMsgSetRecord,
NAMESERVICE_ERRORS
NAMESERVICE_ERRORS,
parseMsgSetRecordResponse
} from './messages/nameservice';
import {
createTxMsgCommitBid,
@ -47,9 +48,12 @@ import {
const DEFAULT_WRITE_ERROR = 'Unable to write to chiba-clonk.';
// Parse Tx response from cosmos-sdk.
export const parseTxResponse = (result: any) => {
export const parseTxResponse = (result: any, parseResponse?: (data: string) => any) => {
const { txhash: hash, height, ...txResponse } = result;
txResponse.data = txResponse.data && Buffer.from(txResponse.data, 'base64').toString('utf8');
if (parseResponse) {
txResponse.data = parseResponse(txResponse.data)
}
txResponse.events.forEach((event:any) => {
event.attributes = event.attributes.map(({ key, value }: { key: string, value: string }) => ({
@ -64,7 +68,7 @@ export const parseTxResponse = (result: any) => {
/**
* Create an auction bid.
*/
export const createBid = async (chainId: string, auctionId: string, bidderAddress: string, bidAmount: string, noise?: string) => {
export const createBid = async (chainId: string, auctionId: string, bidderAddress: string, bidAmount: string, noise?: string) => {
if (!noise) {
noise = Account.generateMnemonic();
}
@ -153,7 +157,7 @@ export class Registry {
let result;
result = await this._submitRecordTx(params, transactionPrivateKey, fee);
return parseTxResponse(result);
return parseTxResponse(result, parseMsgSetRecordResponse);
}
/**

View File

@ -67,6 +67,19 @@ const MSG_DELETE_NAME_TYPES = {
],
}
export const parseMsgSetRecordResponse = (data: string) => {
const responseBytes = Buffer.from(data, 'hex')
// TODO: Decode response using protobuf.
// const msgSetRecordResponse = nameserviceTx.vulcanize.nameservice.v1beta1.MsgSetRecordResponse.deserialize(responseBytes);
// return msgSetRecordResponse.toObject();
// Workaround as proto based decoding is not working.
const [_, id] = responseBytes.toString().split(';')
return { id }
}
export const NAMESERVICE_ERRORS = [
'Name already reserved.',
'Authority bond not found.',

View File

@ -33,7 +33,7 @@ const namingTests = () => {
// Create watcher.
watcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
await registry.setRecord(
const result = await registry.setRecord(
{
privateKey,
bondId,
@ -43,8 +43,7 @@ const namingTests = () => {
fee
)
const [record] = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }, true);
watcherId = record.id;
watcherId = result.data.id;
});
test('Reserve authority.', async () => {
@ -149,7 +148,7 @@ const namingTests = () => {
test('Lookup name with history', async () => {
const updatedWatcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
await registry.setRecord(
const result = await registry.setRecord(
{
privateKey,
bondId,
@ -159,8 +158,7 @@ const namingTests = () => {
fee
)
const [record] = await registry.queryRecords({ type: 'watcher', version: updatedWatcher.record.version }, true);
const updatedWatcherId = record.id;
const updatedWatcherId = result.data.id;
await registry.setName({ crn: crn, cid: updatedWatcherId }, privateKey, fee);
const records = await registry.lookupNames([crn], true);

View File

@ -122,23 +122,47 @@ export namespace vulcanize.nameservice.v1beta1 {
}
}
export class MsgSetRecordResponse extends pb_1.Message {
constructor(data?: any[] | {}) {
constructor(data?: any[] | {
id?: string;
}) {
super();
pb_1.Message.initialize(this, Array.isArray(data) ? data : [], 0, -1, [], []);
if (!Array.isArray(data) && typeof data == "object") { }
if (!Array.isArray(data) && typeof data == "object") {
if ("id" in data && data.id != undefined) {
this.id = data.id;
}
}
}
static fromObject(data: {}) {
get id() {
return pb_1.Message.getField(this, 1) as string;
}
set id(value: string) {
pb_1.Message.setField(this, 1, value);
}
static fromObject(data: {
id?: string;
}) {
const message = new MsgSetRecordResponse({});
if (data.id != null) {
message.id = data.id;
}
return message;
}
toObject() {
const data: {} = {};
const data: {
id?: string;
} = {};
if (this.id != null) {
data.id = this.id;
}
return data;
}
serialize(): Uint8Array;
serialize(w: pb_1.BinaryWriter): void;
serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter();
if (typeof this.id === "string" && this.id.length)
writer.writeString(1, this.id);
if (!w)
return writer.getResultBuffer();
}
@ -148,6 +172,9 @@ export namespace vulcanize.nameservice.v1beta1 {
if (reader.isEndGroup())
break;
switch (reader.getFieldNumber()) {
case 1:
message.id = reader.readString();
break;
default: reader.skipField();
}
}

View File

@ -26,7 +26,7 @@ const utilTests = () => {
// Create watcher.
watcher = await getBaseConfig(WATCHER_YML_PATH);
await registry.setRecord(
const result = await registry.setRecord(
{
privateKey,
bondId,
@ -36,8 +36,7 @@ const utilTests = () => {
fee
)
const [record] = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }, true);
watcherId = record.id;
watcherId = result.data.id;
});
xtest('generate content id.', async () => {