forked from cerc-io/registry-sdk
See also: cerc-io/laconicd#113  ``` message ApplicationRecord { string type = 1 [(gogoproto.moretags) = "json:\"type\" yaml:\"type\""]; string name = 2 [(gogoproto.moretags) = "json:\"name\" yaml:\"name\""]; string description = 3 [(gogoproto.moretags) = "json:\"description\" yaml:\"description\""]; string version = 4 [(gogoproto.moretags) = "json:\"version\" yaml:\"version\""]; string homepage = 5 [(gogoproto.moretags) = "json:\"homepage\" yaml:\"homepage\""]; string license = 6 [(gogoproto.moretags) = "json:\"license\" yaml:\"license\""]; string author = 7 [(gogoproto.moretags) = "json:\"author\" yaml:\"author\""]; string repository = 8 [(gogoproto.moretags) = "json:\"repository\" yaml:\"repository\""]; string repository_tag = 9 [(gogoproto.moretags) = "json:\"repositoryTag\" yaml:\"repositoryTag\""]; string app_version = 10 [(gogoproto.moretags) = "json:\"appVersion\" yaml:\"appVersion\""]; string app_type = 11 [(gogoproto.moretags) = "json:\"appType\" yaml:\"appType\""]; string meta = 20 [(gogoproto.moretags) = "json:\"meta\" yaml:\"meta\""]; repeated string tags = 21 [(gogoproto.moretags) = "json:\"tags\" yaml:\"tags\""]; } message WebAppDeploymentRecord { string type = 1 [(gogoproto.moretags) = "json:\"type\" yaml:\"type\""]; string name = 2 [(gogoproto.moretags) = "json:\"name\" yaml:\"name\""]; string description = 3 [(gogoproto.moretags) = "json:\"description\" yaml:\"description\""]; string version = 4 [(gogoproto.moretags) = "json:\"version\" yaml:\"version\""]; string application = 5 [(gogoproto.moretags) = "json:\"application\" yaml:\"application\""]; string url = 6 [(gogoproto.moretags) = "json:\"\" yaml:\"name\""]; string meta = 20 [(gogoproto.moretags) = "json:\"meta\" yaml:\"meta\""]; repeated string tags = 21 [(gogoproto.moretags) = "json:\"tags\" yaml:\"tags\""]; } message GeneralRecord { string type = 1 [(gogoproto.moretags) = "json:\"type\" yaml:\"type\""]; string name = 2 [(gogoproto.moretags) = "json:\"name\" yaml:\"name\""]; string description = 3 [(gogoproto.moretags) = "json:\"description\" yaml:\"description\""]; string version = 4 [(gogoproto.moretags) = "json:\"version\" yaml:\"version\""]; string category = 5 [(gogoproto.moretags) = "json:\"category\" yaml:\"category\""]; string value = 6 [(gogoproto.moretags) = "json:\"value\" yaml:\"value\""]; string meta = 20 [(gogoproto.moretags) = "json:\"meta\" yaml:\"meta\""]; repeated string tags = 21 [(gogoproto.moretags) = "json:\"tags\" yaml:\"tags\""]; ``` ``` ❯ cat general.yml record: type: GeneralRecord name: my-generic-record version: 0.0.1 value: "anything-goes-here" category: filter-by-this tags: - a - b - c meta: foo: bar bar: baz: boz ❯ bin/laconic cns record publish --filename general.yml --bond-id ba774084e25af5b29be126dda0bb910d93dea3634713a438ac257da5bbdc631c { "id": "bafyreigt3he52eia5g4i5pnst4dfcuwwgjdoul6xmcke2obz5os4xwoo3q" } ❯ bin/laconic cns record get --id bafyreigt3he52eia5g4i5pnst4dfcuwwgjdoul6xmcke2obz5os4xwoo3q [ { "id": "bafyreigt3he52eia5g4i5pnst4dfcuwwgjdoul6xmcke2obz5os4xwoo3q", "names": null, "owners": [ "9A66500A9AA574CAAB4EDB26F7333590BF452CE0" ], "bondId": "ba774084e25af5b29be126dda0bb910d93dea3634713a438ac257da5bbdc631c", "createTime": "2023-11-17T23:11:27Z", "expiryTime": "2024-11-16T23:11:27Z", "attributes": { "meta": "{\"foo\":\"bar\",\"bar\":{\"baz\":\"boz\"}}", "tags": [ "a", "b", "c" ], "type": "GeneralRecord", "name": "my-generic-record", "version": "0.0.1", "category": "filter-by-this", "value": "anything-goes-here" } } ] ``` > Note: The repeated items are preserved as lists, but the object/map ends up encoded as a string. It would be nice to avoid this. Reviewed-on: cerc-io/laconic-sdk#44 Reviewed-by: David Boreham <dboreham@noreply.git.vdb.to> Co-authored-by: Thomas E Lackey <telackey@bozemanpass.com> Co-committed-by: Thomas E Lackey <telackey@bozemanpass.com>
148 lines
2.7 KiB
TypeScript
148 lines
2.7 KiB
TypeScript
import assert from 'assert';
|
|
import { Validator } from 'jsonschema';
|
|
|
|
import RecordSchema from './schema/record.json';
|
|
import { Util } from './util';
|
|
import * as attributes from './proto/vulcanize/registry/v1beta1/attributes';
|
|
import * as any from './proto/google/protobuf/any';
|
|
|
|
/**
|
|
* Record.
|
|
*/
|
|
export class Record {
|
|
_record: any
|
|
|
|
/**
|
|
* New Record.
|
|
*/
|
|
constructor(record: any) {
|
|
assert(record);
|
|
|
|
const validator = new Validator();
|
|
const result = validator.validate(record, RecordSchema);
|
|
if (!result.valid) {
|
|
result.errors.map(console.error);
|
|
throw new Error('Invalid record input.');
|
|
}
|
|
|
|
this._record = record;
|
|
}
|
|
|
|
get attributes() {
|
|
let a = new any.google.protobuf.Any();
|
|
|
|
if (this._record.type) {
|
|
const namespace: any = attributes.vulcanize.registry.v1beta1;
|
|
if (!namespace[this._record.type]) {
|
|
throw new Error(`Class not found: ${this._record.type}`);
|
|
}
|
|
|
|
const value = namespace[this._record.type].fromObject(this._record);
|
|
a = new any.google.protobuf.Any({
|
|
type_url: `/vulcanize.registry.v1beta1.${this._record.type}`,
|
|
value: value.serialize(),
|
|
});
|
|
}
|
|
|
|
return a;
|
|
}
|
|
|
|
/**
|
|
* Serialize record.
|
|
*/
|
|
serialize() {
|
|
return {
|
|
'id': '_',
|
|
'bond_id': '_',
|
|
'create_time': '_',
|
|
'expiry_time': '_',
|
|
// Setting deleted as false (zero value) throws error in EIP712 signature verification.
|
|
'deleted': true,
|
|
'attributes': this.attributes,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get message to calculate record signature.
|
|
*/
|
|
getMessageToSign() {
|
|
return Util.sortJSON(this._record);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Record Signature.
|
|
*/
|
|
export class Signature {
|
|
_pubKey: string
|
|
_sig: string
|
|
|
|
/**
|
|
* New Signature.
|
|
*/
|
|
constructor(pubKey: string, sig: string) {
|
|
assert(pubKey);
|
|
assert(sig);
|
|
|
|
this._pubKey = pubKey;
|
|
this._sig = sig;
|
|
}
|
|
|
|
/**
|
|
* Serialize Signature.
|
|
*/
|
|
serialize() {
|
|
return Util.sortJSON({
|
|
'pub_key': this._pubKey,
|
|
'sig': this._sig
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Message Payload.
|
|
*/
|
|
export class Payload {
|
|
_record: Record
|
|
_signatures: Signature[]
|
|
|
|
/**
|
|
* New Payload.
|
|
*/
|
|
constructor(record: Record, ...signatures: Signature[]) {
|
|
assert(record);
|
|
|
|
this._record = record;
|
|
this._signatures = signatures;
|
|
}
|
|
|
|
get record() {
|
|
return this._record;
|
|
}
|
|
|
|
get signatures() {
|
|
return this._signatures;
|
|
}
|
|
|
|
/**
|
|
* Add message signature to payload.
|
|
*/
|
|
addSignature(signature: any) {
|
|
assert(signature);
|
|
|
|
this._signatures.push(signature);
|
|
}
|
|
|
|
/**
|
|
* Serialize Payload.
|
|
*/
|
|
serialize() {
|
|
// return Util.sortJSON({
|
|
// });
|
|
return {
|
|
'record': this._record.serialize(),
|
|
'signatures': this._signatures.map(s => s.serialize())
|
|
}
|
|
}
|
|
}
|