From 28c8099b1629d7add04f848571d87ef8e1429ec1 Mon Sep 17 00:00:00 2001 From: nabarun Date: Wed, 20 Apr 2022 17:24:37 +0530 Subject: [PATCH] Get record id from setRecord message response --- README.md | 11 ++++++ proto/vulcanize/nameservice/v1beta1/tx.proto | 1 + src/index.ts | 14 +++++--- src/messages/nameservice.ts | 13 +++++++ src/naming.test.ts | 10 +++--- src/proto/vulcanize/nameservice/v1beta1/tx.ts | 35 ++++++++++++++++--- src/util.test.ts | 5 ++- 7 files changed, 71 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c176f41..37032ed 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/proto/vulcanize/nameservice/v1beta1/tx.proto b/proto/vulcanize/nameservice/v1beta1/tx.proto index 1961ead..f66d3a1 100644 --- a/proto/vulcanize/nameservice/v1beta1/tx.proto +++ b/proto/vulcanize/nameservice/v1beta1/tx.proto @@ -43,6 +43,7 @@ message MsgSetRecord{ // MsgSetRecordResponse message MsgSetRecordResponse{ + string id = 1; } // Payload diff --git a/src/index.ts b/src/index.ts index 994561c..38fa588 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); } /** diff --git a/src/messages/nameservice.ts b/src/messages/nameservice.ts index 46984d0..ce24773 100644 --- a/src/messages/nameservice.ts +++ b/src/messages/nameservice.ts @@ -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.', diff --git a/src/naming.test.ts b/src/naming.test.ts index 3c9611b..1a52278 100644 --- a/src/naming.test.ts +++ b/src/naming.test.ts @@ -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); diff --git a/src/proto/vulcanize/nameservice/v1beta1/tx.ts b/src/proto/vulcanize/nameservice/v1beta1/tx.ts index 1affb0b..ce4ad77 100644 --- a/src/proto/vulcanize/nameservice/v1beta1/tx.ts +++ b/src/proto/vulcanize/nameservice/v1beta1/tx.ts @@ -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(); } } diff --git a/src/util.test.ts b/src/util.test.ts index d586b0e..fd711c8 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -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 () => {