Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Add username ENS methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chmanie committed Jan 20, 2023
1 parent a61e7d8 commit d75fa65
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [Network](enums/Network.md)
- [SupportedExtension](enums/SupportedExtension.md)
- [TeamColor](enums/TeamColor.md)
- [UserLabelSuffix](enums/UserLabelSuffix.md)
- [Vote](enums/Vote.md)

## Classes
Expand Down
68 changes: 68 additions & 0 deletions docs/api/classes/ColonyNetwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,74 @@ ___

___

### getUserAddress

**getUserAddress**(`username`): `Promise`<``null`` \| `string`\>

Get the user's addess by the username

Returns the user's address that belongs to the given username. Username has to be provided without any suffix, just like it's shown in the dapp.
Will return `null` if the given username was not registered.

#### Parameters

| Name | Type |
| :------ | :------ |
| `username` | `string` |

#### Returns

`Promise`<``null`` \| `string`\>

The user's address

___

### getUsername

**getUsername**(`address`): `Promise`<``null`` \| `string`\>

Get a user's username

Returns the user's username (the ENS label, just like it's shown in the dapp, without any suffixes)
Will return `null` if the user does not exist or if no label was assigned yet

#### Parameters

| Name | Type |
| :------ | :------ |
| `address` | `string` |

#### Returns

`Promise`<``null`` \| `string`\>

The user's username

___

### registerUsername

**registerUsername**(`username`): [`MetaTxCreator`](MetaTxCreator.md)<`ColonyNetworkClient`, ``"registerUserLabel"``, { `label?`: `string` ; `user?`: `string` }, [`MetadataType`](../enums/MetadataType.md)\>

Register a Colony-internal ENS username

Registers a username for the signing address. An address can only register one username. Usernames are globally unique. This method will check whether the username was registered before.

#### Parameters

| Name | Type |
| :------ | :------ |
| `username` | `string` |

#### Returns

[`MetaTxCreator`](MetaTxCreator.md)<`ColonyNetworkClient`, ``"registerUserLabel"``, { `label?`: `string` ; `user?`: `string` }, [`MetadataType`](../enums/MetadataType.md)\>

A transaction creator

___

### init

`Static` **init**(`signerOrProvider`, `options?`): `Promise`<[`ColonyNetwork`](ColonyNetwork.md)\>
Expand Down
37 changes: 37 additions & 0 deletions docs/api/enums/UserLabelSuffix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Enumeration: UserLabelSuffix

## Enumeration Members

### Custom

**Custom** = ``".user.joincolony.test"``

___

### Gnosis

**Gnosis** = ``".user.joincolony.colonyxdai"``

___

### Goerli

**Goerli** = ``".user.joincolony.test"``

___

### Mainnet

**Mainnet** = ``".user.joincolony.eth"``

___

### Xdai

**Xdai** = ``".user.joincolony.colonyxdai"``

___

### XdaiQa

**XdaiQa** = ``".user.joincolony.colonyxdai"``
75 changes: 71 additions & 4 deletions src/ColonyNetwork/ColonyNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ColonyAddedEventObject,
ColonyMetadataEventObject,
TokenDeployedEventObject,
UserLabelRegisteredEventObject,
} from '@colony/colony-js/extras';
import { ERC2612Token as ERC2612TokenType } from '@colony/colony-js/tokens';
import {
Expand All @@ -28,7 +29,11 @@ import {
import { IpfsMetadata, IpfsAdapter } from '../ipfs';
import { BaseContract, TxConfig, TxCreator, MetaTxCreator } from '../TxCreator';
import { Colony } from './Colony';
import { ColonyLabelSuffix, MetaTxBroadCasterEndpoint } from '../constants';
import {
ColonyLabelSuffix,
MetaTxBroadCasterEndpoint,
UserLabelSuffix,
} from '../constants';
import { Expand, Parameters } from '../types';
import { extractEvent } from '../utils';
import { EIP2612TxCreator } from '../TxCreator/EIP2612TxCreator';
Expand Down Expand Up @@ -363,10 +368,10 @@ export class ColonyNetwork {
metadata?: ColonyMetadata | string,
) {
const checkLabel = async () => {
const existingLabel = await this.getColonyAddress(label);
const existingAddress = await this.getColonyAddress(label);

if (existingLabel) {
throw new Error(`Colony label ${label} already exists`);
if (existingAddress) {
throw new Error(`Colony with label ${label} already exists`);
}
return [tokenAddress, Colony.getLatestSupportedVersion(), label] as [
string,
Expand Down Expand Up @@ -482,6 +487,68 @@ export class ColonyNetwork {
return null;
}

/**
* Get a user's username
*
* Returns the user's username (the ENS label, just like it's shown in the dapp, without any suffixes)
* Will return `null` if the user does not exist or if no label was assigned yet
*
* @returns The user's username
*/
async getUsername(address: string) {
const ensName = await this.networkClient.lookupRegisteredENSDomain(address);
if (ensName) {
return ensName.replace(UserLabelSuffix[this.network], '');
}
return null;
}

/**
* Get the user's addess by the username
*
* Returns the user's address that belongs to the given username. Username has to be provided without any suffix, just like it's shown in the dapp.
* Will return `null` if the given username was not registered.
*
* @returns The user's address
*/
async getUserAddress(username: string) {
const hash = namehash(`${username}${UserLabelSuffix[this.network]}`);
const address = await this.networkClient.addr(hash);
if (address !== AddressZero) {
return address;
}
return null;
}

/**
* Register a Colony-internal ENS username
*
* Registers a username for the signing address. An address can only register one username. Usernames are globally unique. This method will check whether the username was registered before.
*
* @returns A transaction creator
*/
registerUsername(username: string) {
const checkUsername = async () => {
const existingAddress = await this.getColonyAddress(username);

if (existingAddress) {
throw new Error(`Username ${username} is already taken`);
}
return [username, ''] as [string, string];
};
return this.createMetaTxCreator(
this.networkClient,
'registerUserLabel',
checkUsername,
async (receipt) => ({
...extractEvent<UserLabelRegisteredEventObject>(
'UserLabelRegistered',
receipt,
),
}),
);
}

/**
* Deploy a "special" colony ERC20 token
*
Expand Down
9 changes: 9 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export enum ColonyLabelSuffix {
Custom = '.colony.joincolony.test',
}

export enum UserLabelSuffix {
Mainnet = '.user.joincolony.eth',
Goerli = '.user.joincolony.test',
Gnosis = '.user.joincolony.colonyxdai',
Xdai = '.user.joincolony.colonyxdai',
XdaiQa = '.user.joincolony.colonyxdai',
Custom = '.user.joincolony.test',
}

/**
* Identifies a motion as a decision
*
Expand Down

0 comments on commit d75fa65

Please sign in to comment.