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
18 changes: 15 additions & 3 deletions src/dataunion/DataUnion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getAddress } from '@ethersproject/address'
import { BigNumber } from '@ethersproject/bignumber'
import { arrayify, hexZeroPad } from '@ethersproject/bytes'
import { Contract } from '@ethersproject/contracts'
import { TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
import { JsonRpcSigner, TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
import debug from 'debug'
import { Contracts } from './Contracts'
import { StreamrClient } from '../StreamrClient'
Expand Down Expand Up @@ -229,8 +229,20 @@ export class DataUnion {
const memberData = await duSidechain.memberData(address)
if (memberData[0] === '0') { throw new Error(`${address} is not a member in Data Union (sidechain address ${duSidechain.address})`) }
const withdrawn = memberData[3]
// @ts-expect-error
const message = to + hexZeroPad(amountTokenWei, 32).slice(2) + duSidechain.address.slice(2) + hexZeroPad(withdrawn, 32).slice(2)
return this._createWithdrawSignature(amountTokenWei, to, withdrawn, signer)
}

/** @internal */
async _createWithdrawSignature(
amountTokenWei: BigNumber|number|string,
to: EthereumAddress,
withdrawn: BigNumber,
signer: JsonRpcSigner
) {
const message = to
+ hexZeroPad(BigNumber.from(amountTokenWei).toHexString(), 32).slice(2)
+ this.getSidechainAddress().slice(2)
+ hexZeroPad(withdrawn.toHexString(), 32).slice(2)
const signature = await signer.signMessage(arrayify(message))
return signature
}
Expand Down
19 changes: 18 additions & 1 deletion test/integration/dataunion/signature.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, providers, Wallet } from 'ethers'
import { BigNumber, Contract, providers, Wallet } from 'ethers'
import { parseEther } from 'ethers/lib/utils'
import debug from 'debug'

Expand Down Expand Up @@ -74,4 +74,21 @@ describe('DataUnion signature', () => {
expect(isValid2).toBe(true)
expect(isValid3).toBe(true)
}, 100000)

it('create signature', async () => {
const client = new StreamrClient({
auth: {
privateKey: '0x1111111111111111111111111111111111111111111111111111111111111111'
}
})
const dataUnion = client.getDataUnion('0x2222222222222222222222222222222222222222')
const to = '0x3333333333333333333333333333333333333333'
const withdrawn = BigNumber.from('4000000000000000')
const amounts = [5000000000000000, '5000000000000000', BigNumber.from('5000000000000000')]
// eslint-disable-next-line no-underscore-dangle
const signaturePromises = amounts.map((amount) => dataUnion._createWithdrawSignature(amount, to, withdrawn, client.ethereum.getSigner()))
const actualSignatures = await Promise.all(signaturePromises)
const expectedSignature = '0x5325ae62cdfd7d7c15101c611adcb159439217a48193c4e1d87ca5de698ec5233b1a68fd1302fdbd5450618d40739904295c88e88cf79d4241cf8736c2ec75731b' // eslint-disable-line max-len
expect(actualSignatures.every((actual) => actual === expectedSignature))
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should have a test around signWithdrawAmountTo rather than/in addition to _createWithdrawSignature, since _createWithdrawSignature is just implementation detail for signWithdrawAmountTo.

})