Closed tegefaulkes closed 2 years ago
I can't quite get the encode/decode feature to work nicely so we're stuck with to/fromMultibase and wrapping them.
But I have added this neat feature were we can create an Opaque type directly when converting by doing
// where
type NodeId = Opaque<'nodeId', Id>;
// we can do
const nodeId = internalId.fromBuffer<NodeId>(buffer);
// or
const nodeid2: NodeId = InternalId.fromBuffer(buffer);
Any method that returns an Id
type can support this. so the generators like IdRandom
can return our opaque type directly aswell. This just saves us having to use id as OpaqueId
.
This is still a WIP. I'm still making changes.
It may be possible to extend InternalId
and set a Multibase format with;
type NodeId = InternalNodeid & number;
class internalNodeId extends InternalId {
public readonly base: MultibaseFormats = 'base58btc';
}
The encode and decode functions can be built into InternalNodeId
that way. Unfortunately I can't use the class generic types for the static methods so we'd still have to do InternalNodeId.fromBuffer<NodeId>
to get it to return a NodeId
. or we'd have to override all of the static methods to return a NodeId
.
The alternative is to just have the utility functions for each domain to encode/decode.
It's not necessary, it would just be a nice to have. We can proceed to merge this with just the relevant instance and static methods added in here.
We keep using nodesUtils
and vaultsUtils
for encoding/decoding relevant to the domains.
@tegefaulkes Changed PR title to be more clearer. You should try to be more descriptive on those PR names.
Examples of new features.
All of the type coverters are in one place now in the IdInternal class.
IdInternal.fromBuffer();
IdInternal.fromString();
IdInternal.fromUUID();
IdInternal.fromMultibase();
const id = IdInternal.create();
id.toBuffer();
id.toString();
id.toUUID();
id.toMultibase('someRandomBase');
Any constructors for a Id can create an opaque type directly now by using
type OpaqueId<'opaqueId', Id>
const opaqueId = IdInternal.create<OpaqueId>();
const opaqueId: OpaqueId = idInternal.create();
// Same for conversions from other types
const opaqueId = IdInternal.fromBuffer<OpaqueId>();
const opaqueId: OpaqueId = IdInternal.fromString();
// The original way still works
const id = IdInternal.create() as OpaqueId;
The id generators can do the same thing.
type OpaqueId = Opaque<'opaque', Id>;
const idGen = new IdRandom<OpaqueId>();
// ids has type OpaqueId[]
const ids = [...utils.take(idGen, 10)];
All review comments are resolved. should be good to merge.
Description
This adds some QOL features to the
Id
andIdInternal
.Issues Fixed
Tasks
InternalId
Id
Final checklist