-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(abstract-utxo): make descriptor validation more flexible
Use a validator interface that allows for more complex validation and composition of validation rules. Add tests. Issue: BTC-1708
- Loading branch information
1 parent
9680124
commit d5b0b60
Showing
3 changed files
with
105 additions
and
32 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
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
48 changes: 48 additions & 0 deletions
48
modules/abstract-utxo/test/transaction/descriptor/validatePolicy.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,48 @@ | ||
import assert from 'assert'; | ||
|
||
import { Descriptor } from '@bitgo/wasm-miniscript'; | ||
import { Triple } from '@bitgo/sdk-core'; | ||
import { BIP32Interface } from '@bitgo/utxo-lib'; | ||
|
||
import { | ||
assertDescriptorPolicy, | ||
DescriptorPolicyValidationError, | ||
DescriptorValidationPolicy, | ||
getPolicyForEnv, | ||
getValidatorDescriptorTemplate, | ||
} from '../../../src/descriptor/validatePolicy'; | ||
import { getDescriptor } from '../../core/descriptor/descriptor.utils'; | ||
import { getKeyTriple } from '../../core/key.utils'; | ||
|
||
function testAssertDescriptorPolicy( | ||
d: Descriptor, | ||
p: DescriptorValidationPolicy, | ||
k: Triple<BIP32Interface>, | ||
expectedError: DescriptorPolicyValidationError | null | ||
) { | ||
const f = () => assertDescriptorPolicy(d, p, k); | ||
if (expectedError) { | ||
assert.throws(f); | ||
} else { | ||
assert.doesNotThrow(f); | ||
} | ||
} | ||
|
||
describe('assertDescriptorPolicy', function () { | ||
it('has expected result', function () { | ||
const keys = getKeyTriple(); | ||
testAssertDescriptorPolicy(getDescriptor('Wsh2Of3', keys), getValidatorDescriptorTemplate('Wsh2Of3'), keys, null); | ||
|
||
// prod does only allow Wsh2Of3-ish descriptors | ||
testAssertDescriptorPolicy(getDescriptor('Wsh2Of3', keys), getPolicyForEnv('prod'), keys, null); | ||
testAssertDescriptorPolicy( | ||
getDescriptor('Wsh2Of2', keys), | ||
getPolicyForEnv('prod'), | ||
keys, | ||
new DescriptorPolicyValidationError(getDescriptor('Wsh2Of2', keys), getPolicyForEnv('prod')) | ||
); | ||
|
||
// test is very permissive by default | ||
testAssertDescriptorPolicy(getDescriptor('Wsh2Of2', keys), getPolicyForEnv('test'), keys, null); | ||
}); | ||
}); |