11import 'should' ;
2+ import { generateKeyPairSync } from 'crypto' ;
23
34import { TestBitGo } from '@bitgo/sdk-test' ;
45import { BitGo } from '../../../../src/bitgo' ;
@@ -24,8 +25,39 @@ describe('OFC:', function () {
2425 ofcCoin . isValidMofNSetup ( { m : 1 , n : 1 } ) . should . be . true ( ) ;
2526 } ) ;
2627
27- it ( 'should validate pub key' , ( ) => {
28- const { pub } = ofcCoin . keychains ( ) . create ( ) ;
29- ofcCoin . isValidPub ( pub ) . should . equal ( true ) ;
28+ describe ( 'isValidPub' , ( ) => {
29+ it ( 'accepts a BIP-32 xpub (user key)' , ( ) => {
30+ const { pub } = ofcCoin . keychains ( ) . create ( ) ;
31+ ofcCoin . isValidPub ( pub ) . should . equal ( true ) ;
32+ } ) ;
33+
34+ it ( 'accepts a base64 SPKI secp256k1 public key (BitGo key)' , ( ) => {
35+ const { publicKey } = generateKeyPairSync ( 'ec' , { namedCurve : 'secp256k1' } ) ;
36+ const spkiBase64 = publicKey . export ( { type : 'spki' , format : 'der' } ) . toString ( 'base64' ) ;
37+ ofcCoin . isValidPub ( spkiBase64 ) . should . equal ( true ) ;
38+ } ) ;
39+
40+ it ( 'rejects a BIP-32 xprv (private key)' , ( ) => {
41+ const { prv } = ofcCoin . keychains ( ) . create ( ) ;
42+ ofcCoin . isValidPub ( prv ) . should . equal ( false ) ;
43+ } ) ;
44+
45+ it ( 'rejects a SPKI public key on the wrong curve (P-256)' , ( ) => {
46+ const { publicKey } = generateKeyPairSync ( 'ec' , { namedCurve : 'prime256v1' } ) ;
47+ const spkiBase64 = publicKey . export ( { type : 'spki' , format : 'der' } ) . toString ( 'base64' ) ;
48+ ofcCoin . isValidPub ( spkiBase64 ) . should . equal ( false ) ;
49+ } ) ;
50+
51+ it ( 'rejects garbage base64' , ( ) => {
52+ ofcCoin . isValidPub ( Buffer . from ( 'not a key' ) . toString ( 'base64' ) ) . should . equal ( false ) ;
53+ } ) ;
54+
55+ it ( 'rejects an empty string' , ( ) => {
56+ ofcCoin . isValidPub ( '' ) . should . equal ( false ) ;
57+ } ) ;
58+
59+ it ( 'rejects a raw (non-SPKI) secp256k1 hex public key' , ( ) => {
60+ ofcCoin . isValidPub ( '02' + '11' . repeat ( 32 ) ) . should . equal ( false ) ;
61+ } ) ;
3062 } ) ;
3163} ) ;
0 commit comments