@stasho/vf-records 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/records.d.ts +8 -0
- package/dist/records.js +25 -0
- package/package.json +1 -1
package/dist/records.d.ts
CHANGED
|
@@ -53,4 +53,12 @@ export type VfRecord = {
|
|
|
53
53
|
ts: string;
|
|
54
54
|
};
|
|
55
55
|
export declare function parsePayload(payload: string): VfRecord | null;
|
|
56
|
+
/**
|
|
57
|
+
* Parses a record the way the backend index STORES it: the decoded
|
|
58
|
+
* `VfRecord` as JSON (`{"kind":"pub","domain":…}`), not the raw memo text.
|
|
59
|
+
* Validation is delegated to `parsePayload` by re-encoding the candidate, so
|
|
60
|
+
* there is exactly one validator; every field must be a string for the
|
|
61
|
+
* re-encode to be faithful.
|
|
62
|
+
*/
|
|
63
|
+
export declare function parseStoredRecord(json: string): VfRecord | null;
|
|
56
64
|
export declare function encodeRecord(record: VfRecord): string;
|
package/dist/records.js
CHANGED
|
@@ -105,6 +105,31 @@ export function parsePayload(payload) {
|
|
|
105
105
|
}
|
|
106
106
|
return null;
|
|
107
107
|
}
|
|
108
|
+
const RECORD_KINDS = new Set(["pub", "transfer", "bind"]);
|
|
109
|
+
/**
|
|
110
|
+
* Parses a record the way the backend index STORES it: the decoded
|
|
111
|
+
* `VfRecord` as JSON (`{"kind":"pub","domain":…}`), not the raw memo text.
|
|
112
|
+
* Validation is delegated to `parsePayload` by re-encoding the candidate, so
|
|
113
|
+
* there is exactly one validator; every field must be a string for the
|
|
114
|
+
* re-encode to be faithful.
|
|
115
|
+
*/
|
|
116
|
+
export function parseStoredRecord(json) {
|
|
117
|
+
let candidate;
|
|
118
|
+
try {
|
|
119
|
+
candidate = JSON.parse(json);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
if (typeof candidate !== "object" || candidate === null)
|
|
125
|
+
return null;
|
|
126
|
+
const fields = candidate;
|
|
127
|
+
if (!RECORD_KINDS.has(fields["kind"]))
|
|
128
|
+
return null;
|
|
129
|
+
if (!Object.values(fields).every((v) => typeof v === "string"))
|
|
130
|
+
return null;
|
|
131
|
+
return parsePayload(encodeRecord(fields));
|
|
132
|
+
}
|
|
108
133
|
export function encodeRecord(record) {
|
|
109
134
|
if (record.kind === "pub") {
|
|
110
135
|
return `${VF_PREFIX}pub:${JSON.stringify({
|
package/package.json
CHANGED