-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix(utxo): descriptor address validation
- Loading branch information
Showing
12 changed files
with
184 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
"@types/bluebird": "^3.5.25", | ||
"@types/lodash": "^4.14.121", | ||
"@types/superagent": "4.1.15", | ||
"io-ts": "2.1.3", | ||
"bignumber.js": "^9.0.2", | ||
"bitcoinjs-message": "npm:@bitgo-forks/[email protected]", | ||
"bluebird": "^3.5.3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as t from 'io-ts'; | ||
|
||
export const NamedDescriptor = t.type({ | ||
name: t.string, | ||
value: t.string, | ||
}); | ||
|
||
export type NamedDescriptor = t.TypeOf<typeof NamedDescriptor>; |
44 changes: 44 additions & 0 deletions
44
modules/abstract-utxo/src/descriptor/assertDescriptorWalletAddress.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as assert from 'assert'; | ||
import * as t from 'io-ts'; | ||
import * as utxolib from '@bitgo/utxo-lib'; | ||
import { Descriptor } from '@bitgo/wasm-miniscript'; | ||
|
||
import { UtxoCoinSpecific, VerifyAddressOptions } from '../abstractUtxoCoin'; | ||
import { NamedDescriptor } from './NamedDescriptor'; | ||
|
||
class DescriptorAddressMismatchError extends Error { | ||
constructor(descriptor: Descriptor, index: number, derivedAddress: string, expectedAddress: string) { | ||
super( | ||
`Address mismatch for descriptor ${descriptor.toString()} at index ${index}: ${derivedAddress} !== ${expectedAddress}` | ||
); | ||
} | ||
} | ||
|
||
export function assertDescriptorWalletAddress( | ||
network: utxolib.Network, | ||
params: VerifyAddressOptions<UtxoCoinSpecific>, | ||
descriptors: unknown | ||
): void { | ||
assert(params.coinSpecific); | ||
assert(t.array(NamedDescriptor).is(descriptors)); | ||
assert('descriptorName' in params.coinSpecific); | ||
assert('descriptorChecksum' in params.coinSpecific); | ||
const descriptorName = params.coinSpecific.descriptorName; | ||
const descriptorChecksum = params.coinSpecific.descriptorChecksum; | ||
const namedDescriptor = descriptors.find((d) => d.name === descriptorName); | ||
if (!namedDescriptor) { | ||
throw new Error(`Descriptor ${descriptorName} not found`); | ||
} | ||
const descriptor = Descriptor.fromString(namedDescriptor.value, 'derivable'); | ||
const checksum = descriptor.toString().slice(-8); | ||
if (checksum !== descriptorChecksum) { | ||
throw new Error( | ||
`Descriptor checksum mismatch for descriptor name=${descriptorName}: ${checksum} !== ${descriptorChecksum}` | ||
); | ||
} | ||
const derivedScript = Buffer.from(descriptor.atDerivationIndex(params.index).scriptPubkey()); | ||
const derivedAddress = utxolib.address.fromOutputScript(derivedScript, network); | ||
if (params.address !== derivedAddress) { | ||
throw new DescriptorAddressMismatchError(descriptor, params.index, derivedAddress, params.address); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { Miniscript, Descriptor } from '@bitgo/wasm-miniscript'; | ||
export { assertDescriptorWalletAddress } from './assertDescriptorWalletAddress'; | ||
export { NamedDescriptor } from './NamedDescriptor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.