Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generic extensions to Id #13

Open
CMCDragonkai opened this issue Feb 6, 2022 · 1 comment
Open

Allow generic extensions to Id #13

CMCDragonkai opened this issue Feb 6, 2022 · 1 comment
Assignees
Labels
development Standard development r&d:polykey:supporting activity Supporting core activity

Comments

@CMCDragonkai
Copy link
Member

Specification

It would be nice for users to be able to extend IdInternal which can allow us to specialise the encoding functions into the child classes.

class NodeIdInternal extends IdInternal {
  pubic toMultibase(): string {
    return super.toMultibase('base32hex');
  }
}

const nodeId = NodeIdInternal.create(/* ... */);
nodeId.toMultibase();
// instead of nodesUtils.encodeNodeId();

However this is not really possible easily due to the fact that Id type is a union of IdInternal and number.

There's a potential way to do this though with "polymorphic static this" MatrixAI/Polykey#321 (comment)

type Id<T = IdInternal> = T & number;

class IdInternal extends Uint8Array {
  static create<T extends typeof IdInternal>(this: T): Id<InstanceType<T>> {
    return (new this() as Id<InstanceType<T>>);
  }
  baseMethod () {
  }
}

type NodeId = Id<NodeIdInternal>;

class NodeIdInternal extends IdInternal {
  derivedMethod () { }
}

class ClaimIdInternal extends IdInternal {
  anotherMethod () {}
}

class SomeOtherIdOfId extends ClaimIdInternal {
}

const x = IdInternal.create();
const y = NodeIdInternal.create();
const z = SomeOtherIdOfId.create();

// console.log(x.baseMethod());

function doSomething(x: NodeId) {
  const pojo = { };
  pojo[x] = 123;
}

doSomething(x); // this is a type error
doSomething(y);

This makes Id itself a generic type, and it allows all downstream classes to override instance encoding methods like toMultibase or otherwise, and also mean that NodeIdInternal.create would create Id<NodeIdInternal>, which you can match with type NodeId = Id<NodeIdInternal>.

One tradeoff is that's complex for child classes to extend any static methods of IdInternal, but that's unlikely to be needed at all. The only methods that child classes may want to extend are all the encoding methods, and potentially adding new features to their ID objects.

Note that this means right now where alot of functions hand off their decoding to idUtils, they would need to be changed to refer to the current constructor. So like fromBuffer cannot just call idUtils.fromBuffer since that's hardcoded to call IdInternal, but instead refer to this.

Additional context

Tasks

  1. ...
  2. ...
  3. ...
@CMCDragonkai
Copy link
Member Author

CMCDragonkai commented Feb 14, 2022

This could be applied to GRPCClient and its child classes as well. Right now we had to separate the creation functions with GRPCClient.createClient needing to be called by the child classes. If GRPCClient could have a static method that constructs all child classes as well, this would be nice.

However as I said it's difficult to override the parent static method with this technique, and child classes of grpc client may need to do it differently.

If MatrixAI/Polykey#333 is resolved though, there won't be a need for this issue to be applied to the grpc domain.

@CMCDragonkai CMCDragonkai added r&d:polykey:core activity 4 End to End Networking behind Consumer NAT Devices r&d:polykey:core activity 3 Peer to Peer Federated Hierarchy r&d:polykey:core activity 2 Cross Platform Cryptography for JavaScript Platforms r&d:polykey:core activity 1 Secret Vault Sharing and Secret History Management and removed r&d:polykey:core activity 4 End to End Networking behind Consumer NAT Devices r&d:polykey:core activity 3 Peer to Peer Federated Hierarchy r&d:polykey:core activity 2 Cross Platform Cryptography for JavaScript Platforms r&d:polykey:core activity 1 Secret Vault Sharing and Secret History Management labels Jul 23, 2022
@CMCDragonkai CMCDragonkai self-assigned this Jul 10, 2023
@CMCDragonkai CMCDragonkai added the r&d:polykey:supporting activity Supporting core activity label Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Standard development r&d:polykey:supporting activity Supporting core activity
Development

No branches or pull requests

1 participant