11import * as t from 'io-ts' ;
2- import { Descriptor , DescriptorPkType } from '@bitgo/wasm-utxo' ;
3- import { BIP32Interface , networks } from '@bitgo/utxo-lib' ;
4- import { signMessage , verifyMessage } from '@bitgo/sdk-core' ;
2+ import { Descriptor , DescriptorPkType , bip32 , message } from '@bitgo/wasm-utxo' ;
53
64export const NamedDescriptor = t . intersection (
75 [
@@ -27,32 +25,36 @@ export type NamedDescriptorNative = NamedDescriptor<Descriptor>;
2725export function createNamedDescriptorWithSignature (
2826 name : string ,
2927 descriptor : string | Descriptor ,
30- signingKey : BIP32Interface
28+ signingKey : bip32 . BIP32Interface
3129) : NamedDescriptor {
3230 if ( typeof descriptor === 'string' ) {
3331 descriptor = Descriptor . fromString ( descriptor , 'derivable' ) ;
3432 }
3533 const value = descriptor . toString ( ) ;
36- const signature = signMessage ( value , signingKey , networks . bitcoin ) . toString ( 'hex' ) ;
34+ const signature = Buffer . from ( message . signMessage ( value , signingKey . privateKey ! ) ) . toString ( 'hex' ) ;
3735 return { name, value, signatures : [ signature ] } ;
3836}
3937
4038export function toNamedDescriptorNative ( e : NamedDescriptor , pkType : DescriptorPkType ) : NamedDescriptorNative {
4139 return { ...e , value : Descriptor . fromString ( e . value , pkType ) } ;
4240}
4341
44- export function hasValidSignature ( descriptor : string | Descriptor , key : BIP32Interface , signatures : string [ ] ) : boolean {
42+ export function hasValidSignature (
43+ descriptor : string | Descriptor ,
44+ key : bip32 . BIP32Interface ,
45+ signatures : string [ ]
46+ ) : boolean {
4547 if ( typeof descriptor === 'string' ) {
4648 descriptor = Descriptor . fromString ( descriptor , 'derivable' ) ;
4749 }
4850
49- const message = descriptor . toString ( ) ;
51+ const descriptorString = descriptor . toString ( ) ;
5052 return signatures . some ( ( signature ) => {
51- return verifyMessage ( message , key , Buffer . from ( signature , 'hex' ) , networks . bitcoin ) ;
53+ return message . verifyMessage ( descriptorString , key . publicKey , Buffer . from ( signature , 'hex' ) ) ;
5254 } ) ;
5355}
5456
55- export function assertHasValidSignature ( namedDescriptor : NamedDescriptor , key : BIP32Interface ) : void {
57+ export function assertHasValidSignature ( namedDescriptor : NamedDescriptor , key : bip32 . BIP32Interface ) : void {
5658 if ( ! hasValidSignature ( namedDescriptor . value , key , namedDescriptor . signatures ?? [ ] ) ) {
5759 throw new Error ( `Descriptor ${ namedDescriptor . name } does not have a valid signature (key=${ key . toBase58 ( ) } )` ) ;
5860 }
0 commit comments