File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed
Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * as descriptor from './descriptor';
33export * as testutil from './testutil' ;
44export * from './dustThreshold' ;
55export * from './Output' ;
6+ export * from './xOnlyPubkey' ;
Original file line number Diff line number Diff line change 1+ export function toXOnlyPublicKey ( b : Buffer ) : Buffer {
2+ if ( b . length === 33 ) {
3+ if ( b [ 0 ] === 0x02 || b [ 0 ] === 0x03 ) {
4+ return b . subarray ( 1 ) ;
5+ } else {
6+ throw new Error ( `invalid pubkey leading byte ${ b . subarray ( 0 , 1 ) . toString ( 'hex' ) } ` ) ;
7+ }
8+ }
9+
10+ if ( b . length === 32 ) {
11+ return b ;
12+ }
13+
14+ throw new Error ( `invalid pubkey buffer length ${ b . length } ` ) ;
15+ }
Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
2+
3+ import { toXOnlyPublicKey } from '../src' ;
4+
5+ describe ( 'xOnlyPubkey' , function ( ) {
6+ it ( 'converts to X-Only pubkey' , function ( ) {
7+ const buf32 = Buffer . alloc ( 32 , 0 ) ;
8+ assert . deepStrictEqual ( toXOnlyPublicKey ( Buffer . concat ( [ Buffer . from ( [ 0x02 ] ) , buf32 ] ) ) , buf32 ) ;
9+ assert . deepStrictEqual ( toXOnlyPublicKey ( Buffer . concat ( [ Buffer . from ( [ 0x03 ] ) , buf32 ] ) ) , buf32 ) ;
10+ assert . deepStrictEqual ( toXOnlyPublicKey ( buf32 ) , buf32 ) ;
11+ assert . throws ( ( ) => toXOnlyPublicKey ( Buffer . concat ( [ Buffer . from ( [ 0x04 ] ) , buf32 ] ) ) ) ;
12+ assert . throws ( ( ) => toXOnlyPublicKey ( Buffer . alloc ( 31 ) ) ) ;
13+ } ) ;
14+ } ) ;
You can’t perform that action at this time.
0 commit comments