@@ -11,7 +11,8 @@ import CashuDevKit, {
1111 CDKMeltQuote ,
1212 CDKMelted ,
1313 CDKToken ,
14- CDKSpendingConditions
14+ CDKSpendingConditions ,
15+ CDKP2PKCondition
1516} from '../cashu-cdk' ;
1617
1718import { LNURLWithdrawParams } from 'js-lnurl' ;
@@ -722,15 +723,19 @@ export default class CashuStore {
722723 }
723724
724725 let conditions : CDKSpendingConditions | undefined ;
725- if ( p2pkPubkey ) {
726+ if ( p2pkPubkey && p2pkPubkey . trim ( ) . length > 0 ) {
727+ const conditionData : CDKP2PKCondition = {
728+ pubkey : p2pkPubkey . trim ( )
729+ } ;
730+ if ( locktime !== undefined && locktime !== null ) {
731+ conditionData . locktime = locktime ;
732+ }
726733 conditions = {
727734 kind : 'P2PK' ,
728- data : {
729- pubkey : p2pkPubkey ,
730- locktime
731- }
735+ data : conditionData
732736 } ;
733737 }
738+ console . log ( 'conditions' , conditions ) ;
734739
735740 const token = await CashuDevKit . send ( mintUrl , amount , memo , conditions ) ;
736741 await this . syncCDKBalances ( ) ;
@@ -1117,47 +1122,6 @@ export default class CashuStore {
11171122 return this . cashuWallets ;
11181123 } ;
11191124
1120- private getSeed = ( ) => {
1121- if ( this . seed ) return this . seed ;
1122-
1123- if ( true || this . seedVersion === 'v2-bip39' ) {
1124- const lndSeedPhrase = this . settingsStore . seedPhrase ;
1125- const mnemonic = lndSeedPhrase . join ( ' ' ) ;
1126-
1127- const seedFromMnemonic = bip39scure . mnemonicToSeedSync ( mnemonic ) ;
1128- // only need 16 bytes for a 12 word phrase
1129- const entropy = seedFromMnemonic . slice ( 48 , 64 ) ;
1130-
1131- const cashuSeedPhrase = bip39scure . entropyToMnemonic (
1132- entropy ,
1133- BIP39_WORD_LIST
1134- ) ;
1135- const seedPhrase = cashuSeedPhrase . split ( ' ' ) ;
1136-
1137- Storage . setItem (
1138- `${ this . getLndDir ( ) } -cashu-seed-phrase` ,
1139- seedPhrase
1140- ) ;
1141- this . seedPhrase = seedPhrase ;
1142-
1143- const seed = bip39scure . mnemonicToSeedSync ( cashuSeedPhrase ) ;
1144-
1145- Storage . setItem (
1146- `${ this . getLndDir ( ) } -cashu-seed` ,
1147- Base64Utils . bytesToBase64 ( seed )
1148- ) ;
1149- this . seed = seed ;
1150- return this . seed ;
1151- }
1152-
1153- // v1
1154- const seedPhrase = this . settingsStore . seedPhrase ;
1155- const mnemonic = seedPhrase . join ( ' ' ) ;
1156- const seed = bip39 . mnemonicToSeedSync ( mnemonic ) ;
1157- this . seed = new Uint8Array ( seed . slice ( 32 , 64 ) ) ; // limit to 32 bytes
1158- return this . seed ;
1159- } ;
1160-
11611125 setTotalBalance = async ( newTotalBalanceSats : number ) => {
11621126 const previousBalance = this . totalBalanceSats || 0 ;
11631127
@@ -2119,9 +2083,12 @@ export default class CashuStore {
21192083 console . error ( 'Error checking token spent status:' , error ) ;
21202084 runInAction ( ( ) => {
21212085 this . error = true ;
2122- this . error_msg =
2123- error . message ||
2086+ // Safely extract error message
2087+ const errorMessage =
2088+ ( error && typeof error === 'object' && error . message ) ||
2089+ ( typeof error === 'string' ? error : String ( error ) ) ||
21242090 localeString ( 'stores.CashuStore.checkSpentError' ) ;
2091+ this . error_msg = errorMessage ;
21252092 } ) ;
21262093 return false ;
21272094 }
@@ -2189,18 +2156,12 @@ export default class CashuStore {
21892156 noLsp : true
21902157 } ;
21912158
2192- // First receive the token via CDK
2193- const isLocked = CashuUtils . isTokenP2PKLocked ( decoded ) ;
2194- let signingKey : string | undefined ;
2195- if ( isLocked ) {
2196- const bip39seed = this . getSeed ( ) ;
2197- if ( bip39seed ) {
2198- signingKey = Base64Utils . base64ToHex (
2199- Base64Utils . bytesToBase64 ( bip39seed . slice ( 0 , 32 ) )
2200- ) ;
2201- }
2202- }
2203- await this . receiveTokenCDK ( encodedToken , signingKey ) ;
2159+ // Providing our Cashu secret key; CDK will only use it if the token is P2PK-locked.
2160+ const signingKey = this . deriveCashuSecretKey ( ) ;
2161+ await this . receiveTokenCDK (
2162+ encodedToken ,
2163+ signingKey || undefined
2164+ ) ;
22042165 await this . syncCDKBalances ( ) ;
22052166
22062167 // Create invoice for sweeping
@@ -2244,19 +2205,12 @@ export default class CashuStore {
22442205 await this . syncCDKBalances ( true ) ; // Include transactions for activity
22452206 } else {
22462207 // Regular receive via CDK
2247- const isLocked = CashuUtils . isTokenP2PKLocked ( decoded ) ;
2248- let signingKey : string | undefined ;
2249-
2250- if ( isLocked ) {
2251- const bip39seed = this . getSeed ( ) ;
2252- if ( bip39seed ) {
2253- signingKey = Base64Utils . base64ToHex (
2254- Base64Utils . bytesToBase64 ( bip39seed . slice ( 0 , 32 ) )
2255- ) ;
2256- }
2257- }
2258-
2259- await this . receiveTokenCDK ( encodedToken , signingKey ) ;
2208+ // Providing our Cashu secret key; CDK will only use it if the token is P2PK-locked.
2209+ const signingKey = this . deriveCashuSecretKey ( ) ;
2210+ await this . receiveTokenCDK (
2211+ encodedToken ,
2212+ signingKey || undefined
2213+ ) ;
22602214
22612215 // Record received token activity
22622216 this . receivedTokens ?. push (
@@ -2289,6 +2243,20 @@ export default class CashuStore {
22892243 errorMessage : localeString ( 'stores.CashuStore.alreadySpent' )
22902244 } ;
22912245 }
2246+ if (
2247+ e ?. message ?. includes ( 'Witness is missing for p2pk signature' ) ||
2248+ ( typeof e ?. message === 'string' &&
2249+ e . message . toLowerCase ( ) . includes ( 'witness is missing' ) &&
2250+ e . message . toLowerCase ( ) . includes ( 'p2pk' ) )
2251+ ) {
2252+ this . loading = false ;
2253+ return {
2254+ success : false ,
2255+ errorMessage : localeString (
2256+ 'stores.CashuStore.claimError.lockedToWallet'
2257+ )
2258+ } ;
2259+ }
22922260 this . loading = false ;
22932261 return {
22942262 success : false ,
@@ -2463,10 +2431,12 @@ export default class CashuStore {
24632431 this . mintingTokenError = false ;
24642432 this . error_msg = undefined ;
24652433 } ) ;
2466-
2434+ console . log ( 'mintToken' , memo , value , pubkey , lockTime ) ;
24672435 const mintUrl = this . selectedMintUrl ;
24682436
24692437 try {
2438+ await this . syncCDKBalances ( ) ;
2439+
24702440 // Check balance via CDK
24712441 const balance = await CashuDevKit . getMintBalance ( mintUrl ) ;
24722442 if ( balance < Number ( value ) ) {
0 commit comments