Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit f1a6d3b

Browse files
committed
Convert publish/Signer.js -> publish/Signer.ts.
1 parent 6ce6ffe commit f1a6d3b

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/Config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { O } from 'ts-toolbelt'
77
import { getVersionString, counterId } from './utils'
88
import { Todo } from './types'
99

10+
export type EthereumConfig = ExternalProvider|JsonRpcFetchFunc
11+
1012
export type StreamrClientOptions = {
1113
id?: string
1214
debug?: Debug.Debugger,
1315
auth?: {
1416
privateKey?: string
15-
ethereum?: ExternalProvider|JsonRpcFetchFunc,
17+
ethereum?: EthereumConfig
1618
apiKey?: string
1719
username?: string
1820
password?: string

src/publish/Signer.js renamed to src/publish/Signer.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@ import { MessageLayer, Utils } from 'streamr-client-protocol'
22
import { Web3Provider } from '@ethersproject/providers'
33

44
import { pLimitFn, sleep } from '../utils'
5+
import type { EthereumConfig } from '../Config'
56

67
const { StreamMessage } = MessageLayer
78
const { SigningUtil } = Utils
89
const { SIGNATURE_TYPES } = StreamMessage
910

10-
function getSigningFunction({ privateKey, ethereum } = {}) {
11+
type AuthOption = {
12+
ethereum: undefined
13+
privateKey: string | Uint8Array
14+
} | {
15+
privateKey: undefined
16+
ethereum: EthereumConfig
17+
} | {
18+
ethereum: undefined
19+
privateKey: undefined
20+
}
21+
22+
function getSigningFunction({
23+
privateKey,
24+
ethereum,
25+
}: AuthOption) {
1126
if (privateKey) {
1227
const key = (typeof privateKey === 'string' && privateKey.startsWith('0x'))
1328
? privateKey.slice(2) // strip leading 0x
1429
: privateKey
15-
return async (d) => SigningUtil.sign(d, key)
30+
return async (d: string) => SigningUtil.sign(d, key.toString())
1631
}
1732

1833
if (ethereum) {
@@ -30,15 +45,16 @@ function getSigningFunction({ privateKey, ethereum } = {}) {
3045
throw new Error('Need either "privateKey" or "ethereum".')
3146
}
3247

33-
export default function Signer(options = {}, publishWithSignature = 'auto') {
48+
export default function Signer(options: AuthOption, publishWithSignature = 'auto') {
3449
const { privateKey, ethereum } = options
50+
const noSignStreamMessage = (streamMessage: MessageLayer.StreamMessage) => streamMessage
3551

3652
if (publishWithSignature === 'never') {
37-
return (v) => v
53+
return noSignStreamMessage
3854
}
3955

4056
if (publishWithSignature === 'auto' && !privateKey && !ethereum) {
41-
return (v) => v
57+
return noSignStreamMessage
4258
}
4359

4460
if (publishWithSignature !== 'auto' && publishWithSignature !== 'always') {
@@ -47,7 +63,10 @@ export default function Signer(options = {}, publishWithSignature = 'auto') {
4763

4864
const sign = getSigningFunction(options)
4965

50-
async function signStreamMessage(streamMessage, signatureType = SIGNATURE_TYPES.ETH) {
66+
async function signStreamMessage(
67+
streamMessage: MessageLayer.StreamMessage,
68+
signatureType: MessageLayer.StreamMessage['signatureType'] = SIGNATURE_TYPES.ETH
69+
) {
5170
if (!streamMessage) {
5271
throw new Error('streamMessage required as part of the data to sign.')
5372
}

0 commit comments

Comments
 (0)