Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ export default class Session extends EventEmitter {
if (typeof this.options.privateKey !== 'undefined') {
const wallet = new Wallet(this.options.privateKey)
this.loginFunction = async () => (
this._client.loginEndpoints.loginWithChallengeResponse((d: string) => wallet.signMessage(d), wallet.address)
this._client.loginWithChallengeResponse((d: string) => wallet.signMessage(d), wallet.address)
)
} else if (typeof this.options.ethereum !== 'undefined') {
const provider = new Web3Provider(this.options.ethereum)
const signer = provider.getSigner()
this.loginFunction = async () => (
this._client.loginEndpoints.loginWithChallengeResponse((d: string) => signer.signMessage(d), await signer.getAddress())
this._client.loginWithChallengeResponse((d: string) => signer.signMessage(d), await signer.getAddress())
)
} else if (typeof this.options.apiKey !== 'undefined') {
this.loginFunction = async () => (
this._client.loginEndpoints.loginWithApiKey(this.options.apiKey!)
this._client.loginWithApiKey(this.options.apiKey!)
)
} else if (typeof this.options.username !== 'undefined' && typeof this.options.password !== 'undefined') {
this.loginFunction = async () => (
this._client.loginEndpoints.loginWithUsernamePassword(this.options.username!, this.options.password!)
this._client.loginWithUsernamePassword(this.options.username!, this.options.password!)
)
} else {
if (!this.options.sessionToken) {
Expand Down Expand Up @@ -128,7 +128,7 @@ export default class Session extends EventEmitter {
}

this.updateState(State.LOGGING_OUT)
await this._client.loginEndpoints.logoutEndpoint()
await this._client.logoutEndpoint()
this.options.sessionToken = undefined
this.updateState(State.LOGGED_OUT)
}
Expand Down
35 changes: 25 additions & 10 deletions src/StreamrClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import StreamrEthereum from './Ethereum'
import Session from './Session'
import Connection, { ConnectionError } from './Connection'
import Publisher from './publish'
import { Subscriber } from './subscribe'
import { Subscriber, Subscription } from './subscribe'
import { getUserId } from './user'
import { Todo, MaybeAsync, EthereumAddress } from './types'
import { StreamEndpoints } from './rest/StreamEndpoints'
Expand Down Expand Up @@ -141,18 +141,24 @@ export interface StreamrClient extends StreamEndpoints, LoginEndpoints {}

// eslint-disable-next-line no-redeclare
export class StreamrClient extends EventEmitter {
/** @internal */
id: string
/** @internal */
debug: Debug.Debugger
/** @internal */
options: StrictStreamrClientOptions
/** @internal */
session: Session
/** @internal */
connection: StreamrConnection
/** @internal */
publisher: Todo
/** @internal */
subscriber: Subscriber
/** @internal */
cached: StreamrCached
/** @internal */
ethereum: StreamrEthereum
streamEndpoints: StreamEndpoints
loginEndpoints: LoginEndpoints

constructor(options: StreamrClientOptions = {}, connection?: StreamrConnection) {
super()
Expand Down Expand Up @@ -189,25 +195,29 @@ export class StreamrClient extends EventEmitter {
this.subscriber = new Subscriber(this)
this.ethereum = new StreamrEthereum(this)

this.streamEndpoints = Plugin(this, new StreamEndpoints(this))
this.loginEndpoints = Plugin(this, new LoginEndpoints(this))
Plugin(this, new StreamEndpoints(this))
Plugin(this, new LoginEndpoints(this))
this.cached = new StreamrCached(this)
}

/** @internal */
async onConnectionConnected() {
this.debug('Connected!')
this.emit('connected')
}

/** @internal */
async onConnectionDisconnected() {
this.debug('Disconnected.')
this.emit('disconnected')
}

/** @internal */
onConnectionError(err: Todo) {
this.emit('error', new ConnectionError(err))
}

/** @internal */
getErrorEmitter(source: Todo) {
return (err: Todo) => {
if (!(err instanceof ConnectionError || err.reason instanceof ConnectionError)) {
Expand All @@ -219,19 +229,20 @@ export class StreamrClient extends EventEmitter {
}
}

/** @internal */
_onError(err: Todo, ...args: Todo) {
// @ts-expect-error
this.onError(err, ...args)
}

/** @internal */
async send(request: Todo) {
return this.connection.send(request)
}

/**
* Override to control output
*/

* @internal */
onError(error: Todo) { // eslint-disable-line class-methods-use-this
console.error(error)
}
Expand All @@ -256,6 +267,7 @@ export class StreamrClient extends EventEmitter {
return this.connection.connect()
}

/** @internal */
async nextConnection() {
return this.connection.nextConnection()
}
Expand Down Expand Up @@ -297,10 +309,12 @@ export class StreamrClient extends EventEmitter {
return getUserId(this)
}

/** @internal */
setNextGroupKey(...args: Todo) {
return this.publisher.setNextGroupKey(...args)
}

/** @internal */
rotateGroupKey(...args: Todo) {
return this.publisher.rotateGroupKey(...args)
}
Expand Down Expand Up @@ -340,7 +354,7 @@ export class StreamrClient extends EventEmitter {
await this.subscriber.unsubscribe(opts)
}

async resend(opts: Todo, onMessage?: OnMessageCallback) {
async resend(opts: Todo, onMessage?: OnMessageCallback): Promise<Subscription> {
const task = this.subscriber.resend(opts)
if (typeof onMessage !== 'function') {
return task
Expand All @@ -367,11 +381,11 @@ export class StreamrClient extends EventEmitter {
return this.connection.enableAutoDisconnect(...args)
}

getAddress() {
getAddress(): EthereumAddress {
return this.ethereum.getAddress()
}

async getPublisherId() {
async getPublisherId(): Promise<EthereumAddress> {
return this.getAddress()
}

Expand Down Expand Up @@ -406,6 +420,7 @@ export class StreamrClient extends EventEmitter {
return DataUnion._deploy(options, this) // eslint-disable-line no-underscore-dangle
}

/** @internal */
_getDataUnionFromName({ dataUnionName, deployerAddress }: { dataUnionName: string, deployerAddress: EthereumAddress}) {
return DataUnion._fromName({ // eslint-disable-line no-underscore-dangle
dataUnionName,
Expand Down
11 changes: 8 additions & 3 deletions src/dataunion/DataUnion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ const log = debug('StreamrClient::DataUnion')

export class DataUnion {

contractAddress: EthereumAddress
sidechainAddress: EthereumAddress
client: StreamrClient
private contractAddress: EthereumAddress
private sidechainAddress: EthereumAddress
private client: StreamrClient

/** @internal */
constructor(contractAddress: EthereumAddress, sidechainAddress: EthereumAddress, client: StreamrClient) {
// validate and convert to checksum case
this.contractAddress = getAddress(contractAddress)
Expand Down Expand Up @@ -460,6 +461,7 @@ export class DataUnion {
* Create a new DataUnionMainnet contract to mainnet with DataUnionFactoryMainnet
* This triggers DataUnionSidechain contract creation in sidechain, over the bridge (AMB)
* @return that resolves when the new DU is deployed over the bridge to side-chain
* @internal
*/
static async _deploy(options: DataUnionDeployOptions = {}, client: StreamrClient): Promise<DataUnion> {
const deployerAddress = client.getAddress()
Expand Down Expand Up @@ -513,18 +515,21 @@ export class DataUnion {

// Internal functions

/** @internal */
static _fromContractAddress(contractAddress: string, client: StreamrClient) {
const contracts = new Contracts(client)
const sidechainAddress = contracts.getDataUnionSidechainAddress(getAddress(contractAddress)) // throws if bad address
return new DataUnion(contractAddress, sidechainAddress, client)
}

/** @internal */
static _fromName({ dataUnionName, deployerAddress }: { dataUnionName: string, deployerAddress: string}, client: StreamrClient) {
const contracts = new Contracts(client)
const contractAddress = contracts.getDataUnionMainnetAddress(dataUnionName, getAddress(deployerAddress)) // throws if bad address
return DataUnion._fromContractAddress(contractAddress, client) // eslint-disable-line no-underscore-dangle
}

/** @internal */
async _getContract() {
const ret = this.getContracts().getMainnetContract(this.contractAddress)
// @ts-expect-error
Expand Down
7 changes: 7 additions & 0 deletions src/rest/LoginEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ async function getSessionToken(url: string, props: any) {
/** TODO the class should be annotated with at-internal, but adding the annotation hides the methods */
export class LoginEndpoints {

/** @internal */
client: StreamrClient

constructor(client: StreamrClient) {
this.client = client
}

/** @internal */
async getChallenge(address: string) {
this.client.debug('getChallenge %o', {
address,
Expand All @@ -48,6 +50,7 @@ export class LoginEndpoints {
)
}

/** @internal */
async sendChallengeResponse(challenge: { challenge: string }, signature: string, address: string) {
this.client.debug('sendChallengeResponse %o', {
challenge,
Expand All @@ -63,6 +66,7 @@ export class LoginEndpoints {
return getSessionToken(url, props)
}

/** @internal */
async loginWithChallengeResponse(signingFunction: (challenge: string) => Promise<string>, address: string) {
this.client.debug('loginWithChallengeResponse %o', {
address,
Expand All @@ -72,6 +76,7 @@ export class LoginEndpoints {
return this.sendChallengeResponse(challenge, signature, address)
}

/** @internal */
async loginWithApiKey(apiKey: string) {
this.client.debug('loginWithApiKey %o', {
apiKey,
Expand All @@ -83,6 +88,7 @@ export class LoginEndpoints {
return getSessionToken(url, props)
}

/** @internal */
async loginWithUsernamePassword(username: string, password: string) {
this.client.debug('loginWithUsernamePassword %o', {
username,
Expand Down Expand Up @@ -110,6 +116,7 @@ export class LoginEndpoints {
return authFetch<UserDetails>(`${this.client.options.restUrl}/users/me`, this.client.session)
}

/** @internal */
async logoutEndpoint(): Promise<void> {
this.client.debug('logoutEndpoint')
await authFetch(`${this.client.options.restUrl}/logout`, this.client.session, {
Expand Down
5 changes: 3 additions & 2 deletions src/rest/StreamEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function getKeepAliveAgentForUrl(url: string) {
/** TODO the class should be annotated with at-internal, but adding the annotation hides the methods */
export class StreamEndpoints {

/** @internal */
client: StreamrClient

constructor(client: StreamrClient) {
Expand Down Expand Up @@ -171,7 +172,7 @@ export class StreamEndpoints {
return json.addresses.map((a: string) => a.toLowerCase())
}

async isStreamPublisher(streamId: string, ethAddress: string) {
async isStreamPublisher(streamId: string, ethAddress: EthereumAddress) {
this.client.debug('isStreamPublisher %o', {
streamId,
ethAddress,
Expand All @@ -198,7 +199,7 @@ export class StreamEndpoints {
return json.addresses.map((a: string) => a.toLowerCase())
}

async isStreamSubscriber(streamId: string, ethAddress: string) {
async isStreamSubscriber(streamId: string, ethAddress: EthereumAddress) {
this.client.debug('isStreamSubscriber %o', {
streamId,
ethAddress,
Expand Down
4 changes: 3 additions & 1 deletion src/stream/StorageNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { EthereumAddress } from '../types'

export default class StorageNode {
_address: EthereumAddress

private _address: EthereumAddress

constructor(address: EthereumAddress) {
this._address = address
}
Expand Down
7 changes: 6 additions & 1 deletion src/stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ function getFieldType(value: any): (Field['type'] | undefined) {
}

export class Stream {
// TODO add field definitions for all fields
// @ts-expect-error
id: string
// @ts-expect-error
name: string
description?: string
config: {
fields: Field[];
} = { fields: [] }
partitions?: number
/** @internal */
_client: StreamrClient
requireEncryptedData?: boolean
requireSignedData?: boolean
storageDays?: number
inactivityThresholdHours?: number

constructor(client: StreamrClient, props: StreamProperties) {
this._client = client
Expand All @@ -99,6 +103,7 @@ export class Stream {
return json ? new Stream(this._client, json) : undefined
}

/** @internal */
toObject() {
const result = {}
Object.keys(this).forEach((key) => {
Expand Down
Loading