diff --git a/packages/huma-sdk/src/services/v2/HumaReceivableFactory.ts b/packages/huma-sdk/src/services/v2/HumaReceivableFactory.ts index 1c02d19c..3ac1ed52 100644 --- a/packages/huma-sdk/src/services/v2/HumaReceivableFactory.ts +++ b/packages/huma-sdk/src/services/v2/HumaReceivableFactory.ts @@ -45,6 +45,17 @@ export class HumaReceivableFactory { throw new Error('Input must be a JSON object.') } + // Check that the private key is different from the signer passed in the context + const signerAddress = await this.#humaContext.signer.getAddress() + const arweavePaymentAddress = ethers.utils.computeAddress( + arweavePaymentPrivateKey, + ) + if (signerAddress === arweavePaymentAddress) { + throw new Error( + 'The ARWeave payment private key must be different from the signer address', + ) + } + await this.throwIfReferenceIdExists(referenceId) const metadataURI = await this.uploadMetadata( diff --git a/packages/huma-sdk/tests/services/v2/HumaReceivableFactory.test.ts b/packages/huma-sdk/tests/services/v2/HumaReceivableFactory.test.ts new file mode 100644 index 00000000..9e7558ca --- /dev/null +++ b/packages/huma-sdk/tests/services/v2/HumaReceivableFactory.test.ts @@ -0,0 +1,41 @@ +import { ChainEnum, POOL_NAME, POOL_TYPE } from '@huma-finance/shared' +import { BigNumber, ethers } from 'ethers' +import { HumaReceivableFactory, HumaContext } from '../../../src/services' + +describe('HumaReceivableFactory', () => { + beforeEach(() => { + jest.resetAllMocks() + }) + + it('should throw if the signer and arweave uploader wallet are the same', async () => { + const mnemonic = + 'test test test test test test test test test test test junk' + const mnemonicWallet = ethers.Wallet.fromMnemonic(mnemonic) + const provider = new ethers.providers.JsonRpcProvider( + `http://localhost:8545`, + ) + + const humaContext = new HumaContext({ + signer: mnemonicWallet, + provider, + chainId: ChainEnum.Localhost, + poolName: POOL_NAME.ArfCreditPoolV2, + poolType: POOL_TYPE.ReceivableBackedCreditLine, + }) + const receivableFactory = new HumaReceivableFactory({ + humaContext, + }) + + await expect( + receivableFactory.createReceivableWithMetadata( + mnemonicWallet.privateKey, // privateKey + 840, // currencyCode for USD + BigNumber.from(1000), // receivableAmount + 1684517656, // maturityDate + JSON.parse('{"test": "test"}'), + ), + ).rejects.toThrow( + 'The ARWeave payment private key must be different from the signer address', + ) + }) +})