@@ -2,17 +2,32 @@ import { MessageLayer, Utils } from 'streamr-client-protocol'
22import { Web3Provider } from '@ethersproject/providers'
33
44import { pLimitFn , sleep } from '../utils'
5+ import type { EthereumConfig } from '../Config'
56
67const { StreamMessage } = MessageLayer
78const { SigningUtil } = Utils
89const { 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