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

Commit 970d10f

Browse files
authored
Merge pull request #234 from streamr-dev/fix-DU-withdraw-amount-as-string
Fix: DU withdraw amount as string
2 parents 6be2214 + 6e9c523 commit 970d10f

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/dataunion/DataUnion.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getAddress } from '@ethersproject/address'
22
import { BigNumber } from '@ethersproject/bignumber'
33
import { arrayify, hexZeroPad } from '@ethersproject/bytes'
44
import { Contract } from '@ethersproject/contracts'
5-
import { TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
5+
import { JsonRpcSigner, TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
66
import debug from 'debug'
77
import { Contracts } from './Contracts'
88
import { StreamrClient } from '../StreamrClient'
@@ -229,8 +229,20 @@ export class DataUnion {
229229
const memberData = await duSidechain.memberData(address)
230230
if (memberData[0] === '0') { throw new Error(`${address} is not a member in Data Union (sidechain address ${duSidechain.address})`) }
231231
const withdrawn = memberData[3]
232-
// @ts-expect-error
233-
const message = to + hexZeroPad(amountTokenWei, 32).slice(2) + duSidechain.address.slice(2) + hexZeroPad(withdrawn, 32).slice(2)
232+
return this._createWithdrawSignature(amountTokenWei, to, withdrawn, signer)
233+
}
234+
235+
/** @internal */
236+
async _createWithdrawSignature(
237+
amountTokenWei: BigNumber|number|string,
238+
to: EthereumAddress,
239+
withdrawn: BigNumber,
240+
signer: JsonRpcSigner
241+
) {
242+
const message = to
243+
+ hexZeroPad(BigNumber.from(amountTokenWei).toHexString(), 32).slice(2)
244+
+ this.getSidechainAddress().slice(2)
245+
+ hexZeroPad(withdrawn.toHexString(), 32).slice(2)
234246
const signature = await signer.signMessage(arrayify(message))
235247
return signature
236248
}

test/integration/dataunion/signature.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract, providers, Wallet } from 'ethers'
1+
import { BigNumber, Contract, providers, Wallet } from 'ethers'
22
import { parseEther } from 'ethers/lib/utils'
33
import debug from 'debug'
44

@@ -74,4 +74,21 @@ describe('DataUnion signature', () => {
7474
expect(isValid2).toBe(true)
7575
expect(isValid3).toBe(true)
7676
}, 100000)
77+
78+
it('create signature', async () => {
79+
const client = new StreamrClient({
80+
auth: {
81+
privateKey: '0x1111111111111111111111111111111111111111111111111111111111111111'
82+
}
83+
})
84+
const dataUnion = client.getDataUnion('0x2222222222222222222222222222222222222222')
85+
const to = '0x3333333333333333333333333333333333333333'
86+
const withdrawn = BigNumber.from('4000000000000000')
87+
const amounts = [5000000000000000, '5000000000000000', BigNumber.from('5000000000000000')]
88+
// eslint-disable-next-line no-underscore-dangle
89+
const signaturePromises = amounts.map((amount) => dataUnion._createWithdrawSignature(amount, to, withdrawn, client.ethereum.getSigner()))
90+
const actualSignatures = await Promise.all(signaturePromises)
91+
const expectedSignature = '0x5325ae62cdfd7d7c15101c611adcb159439217a48193c4e1d87ca5de698ec5233b1a68fd1302fdbd5450618d40739904295c88e88cf79d4241cf8736c2ec75731b' // eslint-disable-line max-len
92+
expect(actualSignatures.every((actual) => actual === expectedSignature))
93+
})
7794
})

0 commit comments

Comments
 (0)