Skip to content
This repository was archived by the owner on Apr 11, 2023. 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
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,18 @@ describe('BitcoinChainAdapter', () => {
it('should throw if called with invalid chainId', () => {
args.chainId = 'INVALID_CHAINID'
expect(() => new bitcoin.ChainAdapter(args)).toThrow(
/fromCAIP19: error parsing caip19, chain: (.+), network: undefined/
'Bitcoin chainId INVALID_CHAINID not supported'
)
})
it('should throw if called with non bitcoin chainId', () => {
args.chainId = 'eip155:1'
expect(() => new bitcoin.ChainAdapter(args)).toThrow(/chainId must be a bitcoin chain type/)
expect(() => new bitcoin.ChainAdapter(args)).toThrow('Bitcoin chainId eip155:1 not supported')
})
it('should throw if called with no chainId', () => {
it('should use default chainId if no arg chainId provided.', () => {
args.chainId = undefined
expect(() => new bitcoin.ChainAdapter(args)).toThrow(/chainId required/)
const adapter = new bitcoin.ChainAdapter(args)
const chainId = adapter.getChainId()
expect(chainId).toEqual('bip122:000000000019d6689c085ae165831e93')
})
})

Expand Down
18 changes: 13 additions & 5 deletions packages/chain-adapters/src/bitcoin/BitcoinChainAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssetNamespace, AssetReference, caip2, caip19 } from '@shapeshiftoss/caip'
import { AssetNamespace, AssetReference, CAIP2, caip2, caip19 } from '@shapeshiftoss/caip'
import {
bip32ToAddressNList,
BTCOutputAddressType,
Expand Down Expand Up @@ -33,17 +33,25 @@ export class ChainAdapter
coinType: 0,
accountNumber: 0
}
protected readonly supportedChainIds: CAIP2[] = [
'bip122:000000000019d6689c085ae165831e93',
'bip122:000000000933ea01ad0ee984209779ba'
]
chainId = this.supportedChainIds[0]

constructor(args: ChainAdapterArgs) {
super(args)
if (!args.chainId) {
throw new Error('chainId required')
if (args.chainId && !this.supportedChainIds.includes(args.chainId))
throw new Error(`Bitcoin chainId ${args.chainId} not supported`)
if (args.chainId) {
this.chainId = args.chainId
} else {
this.chainId = this.supportedChainIds[0]
}
const { chain, network } = caip2.fromCAIP2(args.chainId)
const { chain, network } = caip2.fromCAIP2(this.chainId)
if (chain !== ChainTypes.Bitcoin) {
throw new Error('chainId must be a bitcoin chain type')
}
this.chainId = args.chainId
this.coinName = args.coinName
this.assetId = caip19.toCAIP19({
chain,
Expand Down
1 change: 1 addition & 0 deletions packages/chain-adapters/src/utxo/UTXOBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export abstract class UTXOBaseAdapter<T extends UTXOChainTypes> implements IChai
protected chainId: CAIP2
protected assetId: CAIP19
protected coinName: string
protected readonly supportedChainIds: CAIP2[]
protected readonly providers: {
http: unchained.bitcoin.V1Api
ws: unchained.ws.Client<unchained.SequencedTx>
Expand Down