Skip to content

Commit

Permalink
refactor: replace IAM and IAMBase with modules
Browse files Browse the repository at this point in the history
BREAKING CHANGE: initialize modules in order
  • Loading branch information
JGiter committed Sep 30, 2021
1 parent 191d849 commit aa037a0
Show file tree
Hide file tree
Showing 195 changed files with 9,946 additions and 10,791 deletions.
141 changes: 49 additions & 92 deletions README.md
@@ -1,6 +1,7 @@
<p align="center">
<img src="https://github.com/energywebfoundation/iam-client-lib/actions/workflows/deploy.yml/badge.svg" />
</p>

# Identity and Access Management (IAM) Client Library

TypeScript library to be used within decentralized applications for authentication and authorization using DIDs (Decentralized Identifiers) and VCs (Verifiable Credentials)
Expand Down Expand Up @@ -44,103 +45,59 @@ Prerelease version
npm i iam-client-lib@canary
```

### Sample Config for browsers (TypeScript)

```js
import {
IAM,
WalletProvider,
setCacheClientOptions,
setChainConfig,
setMessagingOptions,
MessagingMethod,
} from 'iam-client-lib'

export class App {
private _iam: IAM;

constructor() {
// IAM has builtin default settings for VOLTA CHAIN

// If you want to change default cache server config or add config for your network
setCacheClientOptions(1111, {
url: 'https://some-cache-server.com/',
cacheServerSupportsAuth: true,
})

// If you want to change default chain config or add config for your network
setChainConfig(1111, {
didContractAddress: '0x3e2fb24edc3536d655720280b427c91bcb55f3d6',
ensRegistryAddress: '0xa372d665f83197a63bbe633ebe19c7bfd4943003',
ensResolverAddress: '0xe878bdcf5148307378043bfd2b584909aa48a227',
rpcUrl: 'http://some-rpc.com',
})

// If you want to change default messaging config or add config for your network
setMessagingOptions(1111, {
messagingMethod: MessagingMethod.Nats,
natsServerUrl: 'https://some-exchange-server.com',
})

// create IAM instance
this._iam = new IAM();
}

async initializeIAM() {
// this will show connection modal and authenticate
const { did, connected } = await this._iam.initializeConnection({
walletProvider: WalletProvider.MetaMask,
});

// after successfully authentication you can retrieve the signer
const signer = this._iam.getSigner();
}
### Initialization

```
Because of dependencies between modules they should be initialized in right order.
This is achieved by accessing module initializer from initialization function of required module.

### Sample Config for Node.js (TypeScript)
1. Initializing signer service. It will initialize staking and messaging services and allow to connect to cache server

```js
import { IAM } from 'iam-client-lib'

export class App {
private _iam: IAM;

constructor() {
// IAM has builtin default settings for VOLTA CHAIN

// If you want to change default cache server config or add config for your network
setCacheClientOptions(1111, {
url: 'https://some-cache-server.com/',
cacheServerSupportsAuth: true,
})

// If you want to change default chain config or add config for your network
setChainConfig(1111, {
didContractAddress: '0x3e2fb24edc3536d655720280b427c91bcb55f3d6',
ensRegistryAddress: '0xa372d665f83197a63bbe633ebe19c7bfd4943003',
ensResolverAddress: '0xe878bdcf5148307378043bfd2b584909aa48a227',
rpcUrl: 'http://some-rpc.com',
})

// create IAM instance
this._iam = new IAM({
// only for Node.js env you need to pass rpcUrl in the constructor
rpcUrl: 'http://some-rpc.com',
privateKey: '9945c05be0b1b7b35b7cec937e78c6552ecedca764b53a772547d94a687db929'
});
}

const {
signerService,
stakingService,
messagingService,
connectToCacheServer,
isSessionActive,
storeSession
} = await initWithPrivateKeySigner(privateKey, rpcUrl)
```

2. Connecting to cache server. Depending on signer type signature might be requested

async initializeIAM() {
// this will authenticate
const { did, connected } = await this._iam.initializeConnection();
```js
// IAM has builtin default settings for VOLTA CHAIN, which can overriden
setChainConfig(1111, {
didContractAddress: '0x3e2fb24edc3536d655720280b427c91bcb55f3d6',
ensRegistryAddress: '0xa372d665f83197a63bbe633ebe19c7bfd4943003',
ensResolverAddress: '0xe878bdcf5148307378043bfd2b584909aa48a227',
rpcUrl: 'http://some-rpc.com',
})

setMessagingOptions(1111, {
messagingMethod: MessagingMethod.Nats,
natsServerUrl: 'https://some-exchange-server.com'
})

setCacheClientOptions(1111, {
url: 'https://some-cache-server.com/',
cacheServerSupportsAuth: true,
})

const {
cacheClient,
domainsService,
connectToDidRegistry
} = await connectToCacheServer()
```

// after successfully authentication you can retrieve the signer
const signer = this._iam.getSigner();
}
3. Connecting to DID registry. If the document does not exist yet, a transaction will have to be sent

```js
const {
didRegistry,
claimsService
} = await connectToDidRegistry()
```

## Development
Expand Down Expand Up @@ -173,9 +130,9 @@ npm run build

## Active Maintainers

- [Ahmed Ibrahim](https://github.com/ahmedolaibrahim)
- [John Henderson](https://github.com/jrhender)
- [Dmitry Fesenko](https://github.com/JGiter)
* [Ahmed Ibrahim](https://github.com/ahmedolaibrahim)
* [John Henderson](https://github.com/jrhender)
* [Dmitry Fesenko](https://github.com/JGiter)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion bili.config.ts
Expand Up @@ -10,7 +10,7 @@ const config: Config = {
},
// include tslib for clients using older version of tslib
bundleNodeModules: ["tslib"],
input: "src/iam-client-lib.ts",
input: "src/index.ts",
output: {
format: ["cjs", "esm"],
minify: false,
Expand Down
141 changes: 50 additions & 91 deletions docs/api/README.md
@@ -1,6 +1,7 @@
<p align="center">
<img src="https://github.com/energywebfoundation/iam-client-lib/actions/workflows/deploy.yml/badge.svg" />
</p>

# Identity and Access Management (IAM) Client Library

TypeScript library to be used within decentralized applications for authentication and authorization using DIDs (Decentralized Identifiers) and VCs (Verifiable Credentials)
Expand Down Expand Up @@ -44,101 +45,59 @@ Prerelease version
npm i iam-client-lib@canary
```

### Sample Config for browsers (TypeScript)
### Initialization

```js
import {
IAM,
WalletProvider,
setCacheClientOptions,
setChainConfig,
setMessagingOptions,
MessagingMethod,
} from 'iam-client-lib'

export class App {
private _iam: IAM;

constructor() {
// IAM has builtin default settings for VOLTA CHAIN

// If you want to change default cache server config or add config for your network
setCacheClientOptions(1111, {
url: 'https://some-cache-server.com/',
cacheServerSupportsAuth: true,
})

// If you want to change default chain config or add config for your network
setChainConfig(1111, {
didContractAddress: '0x3e2fb24edc3536d655720280b427c91bcb55f3d6',
ensRegistryAddress: '0xa372d665f83197a63bbe633ebe19c7bfd4943003',
ensResolverAddress: '0xe878bdcf5148307378043bfd2b584909aa48a227',
rpcUrl: 'http://some-rpc.com',
})

// If you want to change default messaging config or add config for your network
setMessagingOptions(1111, {
messagingMethod: MessagingMethod.Nats,
natsServerUrl: 'https://some-exchange-server.com',
})

// create IAM instance
this._iam = new IAM();
}

async initializeIAM() {
// this will show connection modal and authenticate
const { did, connected } = await this._iam.initializeConnection({
walletProvider: WalletProvider.MetaMask,
});

// after successfully authentication you can retrieve the signer
const signer = this._iam.getSigner();
}
Because of dependencies between modules they should be initialized in right order.
This is achieved by accessing module initializer from initialization function of required module.

1. Initializing signer service. It will initialize staking and messaging services and allow to connect to cache server

```js
const {
signerService,
stakingService,
messagingService,
connectToCacheServer,
isSessionActive,
storeSession
} = await initWithPrivateKeySigner(privateKey, rpcUrl)
```

### Sample Config for Node.js (TypeScript)
2. Connecting to cache server. Depending on signer type signature might be requested

```js
import { IAM } from 'iam-client-lib'

export class App {
private _iam: IAM;

constructor() {
// IAM has builtin default settings for VOLTA CHAIN

// If you want to change default cache server config or add config for your network
setCacheClientOptions(1111, {
url: 'https://some-cache-server.com/',
cacheServerSupportsAuth: true,
})

// If you want to change default chain config or add config for your network
setChainConfig(1111, {
didContractAddress: '0x3e2fb24edc3536d655720280b427c91bcb55f3d6',
ensRegistryAddress: '0xa372d665f83197a63bbe633ebe19c7bfd4943003',
ensResolverAddress: '0xe878bdcf5148307378043bfd2b584909aa48a227',
rpcUrl: 'http://some-rpc.com',
})

// create IAM instance
this._iam = new IAM({
// only for Node.js env you need to pass rpcUrl in the constructor
rpcUrl: 'http://some-rpc.com',
privateKey: '9945c05be0b1b7b35b7cec937e78c6552ecedca764b53a772547d94a687db929'
});
}

async initializeIAM() {
// this will authenticate
const { did, connected } = await this._iam.initializeConnection();

// after successfully authentication you can retrieve the signer
const signer = this._iam.getSigner();
}
// IAM has builtin default settings for VOLTA CHAIN, which can overriden
setChainConfig(1111, {
didContractAddress: '0x3e2fb24edc3536d655720280b427c91bcb55f3d6',
ensRegistryAddress: '0xa372d665f83197a63bbe633ebe19c7bfd4943003',
ensResolverAddress: '0xe878bdcf5148307378043bfd2b584909aa48a227',
rpcUrl: 'http://some-rpc.com',
})

setMessagingOptions(1111, {
messagingMethod: MessagingMethod.Nats,
natsServerUrl: 'https://some-exchange-server.com'
})

setCacheClientOptions(1111, {
url: 'https://some-cache-server.com/',
cacheServerSupportsAuth: true,
})

const {
cacheClient,
domainsService,
connectToDidRegistry
} = await connectToCacheServer()
```

3. Connecting to DID registry. If the document does not exist yet, a transaction will have to be sent

```js
const {
didRegistry,
claimsService
} = await connectToDidRegistry()
```

## Development
Expand Down Expand Up @@ -171,9 +130,9 @@ npm run build

## Active Maintainers

- [Ahmed Ibrahim](https://github.com/ahmedolaibrahim)
- [John Henderson](https://github.com/jrhender)
- [Dmitry Fesenko](https://github.com/JGiter)
* [Ahmed Ibrahim](https://github.com/ahmedolaibrahim)
* [John Henderson](https://github.com/jrhender)
* [Dmitry Fesenko](https://github.com/JGiter)

## Contributing

Expand Down

0 comments on commit aa037a0

Please sign in to comment.