Quick question. In some areas Fleming APIs return property link
which is of the form safe://hbyyyynjbyfza393dx7kzkih6q1rax8sffjiz9skcmethgtorddc5xxak9
and in other places they return property xorname
which is a byte list.
How do I convert the xorname
to the form for a link and vice versa (JavaSript)?
EDIT: I’ve noticed that I don’t need this for PublishedImmutableData because it is available as PublishedImmutableData.resolved_from.xorurl
but I think we’ll still need APIs to convert between the two.
bzee
October 6, 2019, 4:45pm
2
I don’t think the CLI APIs cover such conversion yet. It’s a good idea for @maidsafe though to be consistent with the return values.
Anyway, I’ve solved this some months ago by having little helper functions:
export function get_xor_url(xor: Buffer, tag?: number) {
const encodedHash = multihashes.encode(xor, consts.CID_HASH_FN);
const newCid = new CID(consts.CID_VERSION, consts.CID_DEFAULT_CODEC, encodedHash);
const cidStr = newCid.toBaseEncodedString(consts.CID_BASE_ENCODING);
return tag
? `safe://${cidStr}:${tag}`
: `safe://${cidStr}`;
}
/**
* Parse an XOR URL into its parts.
*
* @param safeUrl The XOR URL (e.g. safe://<cid>:<tag>).
* @returns The extracted components of the XOR URL.
*/
export function parse_xor_url(safeUrl: string) {
const urlObject = url.parse(safeUrl);
const cid = new CID(urlObject.hostname);
const encodedHash = multihashes.decode(cid.multihash);
In short, it uses the cids
package @maidsafe used in their JavaScript API. I’m not sure my function is up-to-date still.
2 Likes
Good points. Agreed, we should be consistent
I’m not sure we ever need to be returning xorname
(this can get gotten from a xorurl
eg).
At the moment @happybeing we don’t have the XorUrlEncoder
api exposed yet (which would handle this).I’ll get this in ASAP (expose `XorUrlEncoder` · Issue #1022 · maidsafe/sn_browser · GitHub )
3 Likes
Also, I’ve been working on some minor feature which includes the xorurl in the objects returned by fetch
.
1 Like
This topic was automatically closed after 60 days. New replies are no longer allowed.