Skip to content

Commit

Permalink
feat: export EthrDidController helper class (decentralized-identity…
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Apr 14, 2021
1 parent d6d2dc1 commit 745100d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function configureNetworksWithInfura(projectId?: string): ConfiguredNetworks {
return configureNetworks({ networks })
}

function getContractForNetwork(conf: ProviderConfiguration): Contract {
export function getContractForNetwork(conf: ProviderConfiguration): Contract {
let provider: Provider = conf.provider || conf.web3?.currentProvider
if (!provider) {
if (conf.rpcUrl) {
Expand Down
49 changes: 44 additions & 5 deletions src/controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
import { Signer } from '@ethersproject/abstract-signer'
import { CallOverrides, Contract } from '@ethersproject/contracts'
import { BlockTag, JsonRpcProvider, TransactionReceipt } from '@ethersproject/providers'
import { address, interpretIdentifier, stringToBytes32 } from './helpers'
import { BlockTag, JsonRpcProvider, Provider, TransactionReceipt } from '@ethersproject/providers'
import { getContractForNetwork } from './configuration'
import { address, DEFAULT_REGISTRY_ADDRESS, interpretIdentifier, stringToBytes32 } from './helpers'

/**
* A class that can be used to interact with the ERC1056 contract on behalf of a local controller key-pair
*/
export class EthrDidController {
private contract: Contract
private signer?: Signer
private address: string
public did: string

constructor(identifier: string | address, contract: Contract, signer?: Signer) {
this.contract = contract
/**
* Creates an EthrDidController instance.
*
* @param identifier - required - a `did:ethr` string or a publicKeyHex or an ethereum address
* @param signer - optional - a Signer that represents the current controller key (owner) of the identifier. If a 'signer' is not provided, then a 'contract' with an attached signer can be used.
* @param contract - optional - a Contract instance representing a ERC1056 contract. At least one of `contract`, `provider`, or `rpcUrl` is required
* @param chainNameOrId - optional - the network name or chainID, defaults to 'mainnet'
* @param provider - optional - a web3 Provider. At least one of `contract`, `provider`, or `rpcUrl` is required
* @param rpcUrl - optional - a JSON-RPC URL that can be used to connect to an ethereum network. At least one of `contract`, `provider`, or `rpcUrl` is required
* @param registry - optional - The ERC1056 registry address. Defaults to '0xdca7ef03e98e0dc2b855be647c39abe984fcf21b'. Only used with 'provider' or 'rpcUrl'
*/
constructor(
identifier: string | address,
contract?: Contract,
signer?: Signer,
chainNameOrId = 'mainnet',
provider?: Provider,
rpcUrl?: string,
registry: string = DEFAULT_REGISTRY_ADDRESS
) {
// initialize identifier
const { address, publicKey, network } = interpretIdentifier(identifier)
const net = network || chainNameOrId
// initialize contract connection
if (contract) {
this.contract = contract
} else if (provider || signer?.provider) {
const prov = provider || signer?.provider
this.contract = getContractForNetwork({ name: net, provider: prov, registry, rpcUrl })
} else {
throw new Error(' either a contract instance or a provider or rpcUrl is required to initialize')
}
this.signer = signer
const { address } = interpretIdentifier(identifier)
this.address = address
let networkString = net ? `${net}:` : ''
if (networkString in ['mainnet:', '0x1:']) {
networkString = ''
}
this.did = publicKey ? `did:ethr:${networkString}${publicKey}` : `did:ethr:${networkString}${address}`
}

async getOwner(address: address, blockTag?: BlockTag): Promise<string> {
Expand Down
17 changes: 13 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,20 @@ export function stringToBytes32(str: string): string {
return buffStr + '0'.repeat(66 - buffStr.length)
}

export function interpretIdentifier(identifier: string): { address: string; publicKey?: string } {
if (identifier.length > 42) {
return { address: computeAddress(identifier), publicKey: identifier }
export function interpretIdentifier(identifier: string): { address: string; publicKey?: string; network?: string } {
let input = identifier
let network = undefined
if (input.startsWith('did:ethr')) {
const components = input.split(':')
input = components[components.length - 1]
if (components.length === 4) {
network = components[2]
}
}
if (input.length > 42) {
return { address: computeAddress(input), publicKey: input, network }
} else {
return { address: getAddress(identifier) } // checksum address
return { address: getAddress(input), network } // checksum address
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getResolver } from './resolver'
import { EthrDidController } from './controller'
import {
bytes32toString,
DEFAULT_REGISTRY_ADDRESS,
Expand All @@ -15,6 +16,7 @@ export {
getResolver,
bytes32toString,
stringToBytes32,
EthrDidController,
/**@deprecated */
legacyAlgoMap as delegateTypes,
/**@deprecated */
Expand Down

0 comments on commit 745100d

Please sign in to comment.