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 ## Development
[README](./DEVELOPMENT.md) [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 // MsgSetRecordResponse
message MsgSetRecordResponse{ message MsgSetRecordResponse{
string id = 1;
} }
// Payload // Payload

View File

@ -35,7 +35,8 @@ import {
MessageMsgSetAuthorityBond, MessageMsgSetAuthorityBond,
MessageMsgSetName, MessageMsgSetName,
MessageMsgSetRecord, MessageMsgSetRecord,
NAMESERVICE_ERRORS NAMESERVICE_ERRORS,
parseMsgSetRecordResponse
} from './messages/nameservice'; } from './messages/nameservice';
import { import {
createTxMsgCommitBid, createTxMsgCommitBid,
@ -47,9 +48,12 @@ import {
const DEFAULT_WRITE_ERROR = 'Unable to write to chiba-clonk.'; const DEFAULT_WRITE_ERROR = 'Unable to write to chiba-clonk.';
// Parse Tx response from cosmos-sdk. // 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; 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) => { txResponse.events.forEach((event:any) => {
event.attributes = event.attributes.map(({ key, value }: { key: string, value: string }) => ({ event.attributes = event.attributes.map(({ key, value }: { key: string, value: string }) => ({
@ -64,7 +68,7 @@ export const parseTxResponse = (result: any) => {
/** /**
* Create an auction bid. * 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) { if (!noise) {
noise = Account.generateMnemonic(); noise = Account.generateMnemonic();
} }
@ -153,7 +157,7 @@ export class Registry {
let result; let result;
result = await this._submitRecordTx(params, transactionPrivateKey, fee); 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 = [ export const NAMESERVICE_ERRORS = [
'Name already reserved.', 'Name already reserved.',
'Authority bond not found.', 'Authority bond not found.',

View File

@ -33,7 +33,7 @@ const namingTests = () => {
// Create watcher. // Create watcher.
watcher = await ensureUpdatedConfig(WATCHER_YML_PATH); watcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
await registry.setRecord( const result = await registry.setRecord(
{ {
privateKey, privateKey,
bondId, bondId,
@ -43,8 +43,7 @@ const namingTests = () => {
fee fee
) )
const [record] = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }, true); watcherId = result.data.id;
watcherId = record.id;
}); });
test('Reserve authority.', async () => { test('Reserve authority.', async () => {
@ -149,7 +148,7 @@ const namingTests = () => {
test('Lookup name with history', async () => { test('Lookup name with history', async () => {
const updatedWatcher = await ensureUpdatedConfig(WATCHER_YML_PATH); const updatedWatcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
await registry.setRecord( const result = await registry.setRecord(
{ {
privateKey, privateKey,
bondId, bondId,
@ -159,8 +158,7 @@ const namingTests = () => {
fee fee
) )
const [record] = await registry.queryRecords({ type: 'watcher', version: updatedWatcher.record.version }, true); const updatedWatcherId = result.data.id;
const updatedWatcherId = record.id;
await registry.setName({ crn: crn, cid: updatedWatcherId }, privateKey, fee); await registry.setName({ crn: crn, cid: updatedWatcherId }, privateKey, fee);
const records = await registry.lookupNames([crn], true); 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 { export class MsgSetRecordResponse extends pb_1.Message {
constructor(data?: any[] | {}) { constructor(data?: any[] | {
id?: string;
}) {
super(); super();
pb_1.Message.initialize(this, Array.isArray(data) ? data : [], 0, -1, [], []); 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({}); const message = new MsgSetRecordResponse({});
if (data.id != null) {
message.id = data.id;
}
return message; return message;
} }
toObject() { toObject() {
const data: {} = {}; const data: {
id?: string;
} = {};
if (this.id != null) {
data.id = this.id;
}
return data; return data;
} }
serialize(): Uint8Array; serialize(): Uint8Array;
serialize(w: pb_1.BinaryWriter): void; serialize(w: pb_1.BinaryWriter): void;
serialize(w?: pb_1.BinaryWriter): Uint8Array | void { serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter(); const writer = w || new pb_1.BinaryWriter();
if (typeof this.id === "string" && this.id.length)
writer.writeString(1, this.id);
if (!w) if (!w)
return writer.getResultBuffer(); return writer.getResultBuffer();
} }
@ -148,6 +172,9 @@ export namespace vulcanize.nameservice.v1beta1 {
if (reader.isEndGroup()) if (reader.isEndGroup())
break; break;
switch (reader.getFieldNumber()) { switch (reader.getFieldNumber()) {
case 1:
message.id = reader.readString();
break;
default: reader.skipField(); default: reader.skipField();
} }
} }

View File

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