Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jellyfish-api-core): add addMultiSigAddress RPC #2058

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions docs/node/CATEGORIES/05-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,19 @@ interface wallet {
signMessage (address: string, message: string): Promise<string>
}
```

## addMultiSigAddress

Add an nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.

```ts title="client.wallet.addMultiSigAddress()"
interface wallet {
addMultiSigAddress (nRequired: number, keys: string, label?: string, addressType?: string): Promise<MultiSigAddressResult>
}

interface MultiSigAddressResult {
address: string
redeemScript: string
descriptor: string
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { RpcApiError } from '@defichain/jellyfish-api-core'

describe('Wallet with masternode', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
})

afterAll(async () => {
await container.stop()
})

it('should throw error when number of keys provided =/= nrequired', async () => {
const n = 3
const pubKeyA = await client.wallet.getNewAddress()
const pubKeyB = await client.wallet.getNewAddress()
const keys = [pubKeyA, pubKeyB]

const promise = await client.wallet.addMultiSigAddress(n, keys)
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toMatchObject({
payload: {
code: -8,
message: 'not enough keys supplied (got 2 keys, but need at least 3 to redeem)',
method: 'addmultisigaddress'
}
})
})

it('should throw error when public key provided is invalid', async () => {
const n = 3
const pubKey = ['invalid key']

const promise = await client.wallet.addMultiSigAddress(n, pubKey)
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toMatchObject({
payload: {
code: -5,
message: 'Invalid address: invalid key',
method: 'addmultisigaddress'
}
})
})
})
19 changes: 19 additions & 0 deletions packages/jellyfish-api-core/src/category/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,19 @@ export class Wallet {
async signMessage (address: string, message: string): Promise<string> {
return await this.client.call('signmessage', [address, message], 'number')
}

/**
* Add an nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.
*
* @param {number} nRequired The number of required signatures based on number of keys/addresses.
* @param {string[]} keys The DeFi addresses or hex-encoded public keys.
* @param {string} label optional, a label to assign the addresses to.
* @param {string} addressType optional, the address type to use. E.g: “legacy”, “p2sh-segwit”, and “bech32”.
* @return {Promise<MultiSigAddressResult>}
*/
async addMultiSigAddress (nRequired: number, keys: string[], label?: string, addressType?: string): Promise<MultiSigAddressResult> {
return await this.client.call('addmultisigaddress', [nRequired, keys, label, addressType], 'number')
}
}

export interface UTXO {
Expand Down Expand Up @@ -521,3 +534,9 @@ export interface WalletWatchOnlyBalances {
untrusted_pending: BigNumber
immature: BigNumber
}

export interface MultiSigAddressResult {
address: string
redeemScript: string
descriptor: string
}