11import React , { useCallback , useState , useEffect , useMemo } from 'react'
2- import { isSuccessResponse , getMultisigAddress , DefaultLockInfo , addressToScript , scriptToAddress } from 'utils'
2+ import {
3+ isSuccessResponse ,
4+ getMultisigAddress ,
5+ DefaultLockInfo ,
6+ addressToScript ,
7+ scriptToAddress ,
8+ LegacyMultiSigLockInfo ,
9+ MultiSigLockInfo ,
10+ } from 'utils'
311import { MultisigOutputUpdate } from 'services/subjects'
412import {
513 MultisigConfig ,
@@ -11,11 +19,13 @@ import {
1119 exportMultisigConfig ,
1220 deleteMultisigConfig ,
1321 getMultisigBalances ,
22+ getMultisigDAOBalances ,
1423 loadMultisigTxJson ,
1524 OfflineSignJSON ,
1625 getMultisigSyncProgress ,
1726} from 'services/remote'
1827import { computeScriptHash } from '@ckb-lumos/lumos/utils'
28+ import { remaindRegenerateMultisigAddress } from 'services/localCache'
1929
2030export const useSearch = ( clearSelected : ( ) => void , onFilterConfig : ( searchKey : string ) => void ) => {
2131 const [ keywords , setKeywords ] = useState ( '' )
@@ -55,13 +65,45 @@ export const useSearch = (clearSelected: () => void, onFilterConfig: (searchKey:
5565export const useConfigManage = ( { walletId, isMainnet } : { walletId : string ; isMainnet : boolean } ) => {
5666 const [ entities , setEntities ] = useState < MultisigEntity [ ] > ( [ ] )
5767 const saveConfig = useCallback (
58- ( { m, n, r, addresses } : { m : number ; n : number ; r : number ; addresses : string [ ] } ) => {
68+ ( {
69+ m,
70+ n,
71+ r,
72+ addresses,
73+ lockCodeHash = MultiSigLockInfo . CodeHash ,
74+ } : {
75+ m : number
76+ n : number
77+ r : number
78+ addresses : string [ ]
79+ lockCodeHash ?: string
80+ } ) => {
5981 return saveMultisigConfig ( {
6082 m,
6183 n,
6284 r,
6385 blake160s : addresses . map ( v => addressToScript ( v ) . args ) ,
6486 walletId,
87+ lockCodeHash,
88+ } ) . then ( res => {
89+ if ( isSuccessResponse ( res ) ) {
90+ setEntities ( v => ( res . result ? [ res . result , ...v ] : v ) )
91+ } else {
92+ throw new Error ( typeof res . message === 'string' ? res . message : res . message . content )
93+ }
94+ } )
95+ } ,
96+ [ walletId , setEntities ]
97+ )
98+ const regenerateConfig = useCallback (
99+ ( config : MultisigConfig ) => {
100+ return saveMultisigConfig ( {
101+ m : config . m ,
102+ n : config . n ,
103+ r : config . r ,
104+ blake160s : config . blake160s ,
105+ walletId,
106+ lockCodeHash : MultiSigLockInfo . CodeHash ,
65107 } ) . then ( res => {
66108 if ( isSuccessResponse ( res ) ) {
67109 setEntities ( v => ( res . result ? [ res . result , ...v ] : v ) )
@@ -132,7 +174,8 @@ export const useConfigManage = ({ walletId, isMainnet }: { walletId: string; isM
132174 { isMainnet }
133175 )
134176 ) ,
135- fullPayload : getMultisigAddress ( entity . blake160s , entity . r , entity . m , entity . n , isMainnet ) ,
177+ fullPayload : getMultisigAddress ( entity . blake160s , entity . r , entity . m , entity . n , isMainnet , entity . lockCodeHash ) ,
178+ isLegacy : entity . lockCodeHash === LegacyMultiSigLockInfo . CodeHash ,
136179 } ) ) ,
137180 [ entities , isMainnet ]
138181 )
@@ -145,6 +188,7 @@ export const useConfigManage = ({ walletId, isMainnet }: { walletId: string; isM
145188 )
146189 return {
147190 saveConfig,
191+ regenerateConfig,
148192 allConfigs,
149193 onUpdateConfigAlias,
150194 onUpdateConfig,
@@ -267,6 +311,47 @@ const useDeleteAction = (deleteConfigById: (id: number) => void) => {
267311 }
268312}
269313
314+ const useRegenerateAction = ( regenerateConfig : ( config : MultisigConfig ) => Promise < void > ) => {
315+ const [ isDialogOpen , setIsDialogOpen ] = useState ( false )
316+ const [ config , setConfig ] = useState < MultisigConfig > ( )
317+ const [ isNoRemind , setIsNoRemind ] = useState ( false )
318+ const [ regenerateErrorMessage , setRegenerateErrorMessage ] = useState < string | undefined > ( )
319+
320+ const handleCheckbox = useCallback (
321+ ( e : React . ChangeEvent < HTMLInputElement > ) => {
322+ const { checked } = e . target
323+ setIsNoRemind ( checked )
324+ remaindRegenerateMultisigAddress . save ( checked )
325+ } ,
326+ [ setIsNoRemind ]
327+ )
328+
329+ const regenerate = useCallback (
330+ async ( multisigConfig : MultisigConfig ) => {
331+ try {
332+ await regenerateConfig ( multisigConfig )
333+ } catch ( error ) {
334+ setIsDialogOpen ( true )
335+ setRegenerateErrorMessage ( error instanceof Error ? error . message : '' )
336+ }
337+ } ,
338+ [ setIsDialogOpen , config , isNoRemind ]
339+ )
340+ const closeDialog = useCallback ( ( ) => {
341+ setIsDialogOpen ( false )
342+ } , [ setIsDialogOpen ] )
343+ return {
344+ action : regenerate ,
345+ closeDialog,
346+ config,
347+ setConfig,
348+ isDialogOpen,
349+ isNoRemind,
350+ handleCheckbox,
351+ regenerateErrorMessage,
352+ }
353+ }
354+
270355const useApproveAction = ( ) => {
271356 const [ isDialogOpen , setIsDialogOpen ] = useState ( false )
272357 const [ multisigConfig , setMultisigConfig ] = useState < MultisigConfig | undefined > ( )
@@ -342,14 +427,21 @@ const useDaoWithdrawAction = () => {
342427 }
343428}
344429
345- export const useActions = ( { deleteConfigById } : { deleteConfigById : ( id : number ) => void } ) => {
430+ export const useActions = ( {
431+ deleteConfigById,
432+ regenerateConfig,
433+ } : {
434+ deleteConfigById : ( id : number ) => void
435+ regenerateConfig : ( config : MultisigConfig ) => Promise < void >
436+ } ) => {
346437 return {
347438 deleteAction : useDeleteAction ( deleteConfigById ) ,
348439 infoAction : useInfoAction ( ) ,
349440 sendAction : useSendAction ( ) ,
350441 approveAction : useApproveAction ( ) ,
351442 daoDepositAction : useDaoDepositAction ( ) ,
352443 daoWithdrawAction : useDaoWithdrawAction ( ) ,
444+ regenerateAction : useRegenerateAction ( regenerateConfig ) ,
353445 }
354446}
355447
@@ -365,14 +457,20 @@ export const useSubscription = ({
365457 isLightClient : boolean
366458} ) => {
367459 const [ multisigBanlances , setMultisigBanlances ] = useState < Record < string , string > > ( { } )
460+ const [ multisigDaoBalances , setMultisigDaoBalances ] = useState < Record < string , string > > ( { } )
368461 const [ multisigSyncProgress , setMultisigSyncProgress ] = useState < Record < string , number > > ( { } )
369462 const getAndSaveMultisigBalances = useCallback ( ( ) => {
370463 getMultisigBalances ( { isMainnet, multisigAddresses : configs . map ( v => v . fullPayload ) } ) . then ( res => {
371464 if ( isSuccessResponse ( res ) && res . result ) {
372465 setMultisigBanlances ( res . result )
373466 }
374467 } )
375- } , [ setMultisigBanlances , isMainnet , configs ] )
468+ getMultisigDAOBalances ( { isMainnet, multisigAddresses : configs . map ( v => v . fullPayload ) } ) . then ( res => {
469+ if ( isSuccessResponse ( res ) && res . result ) {
470+ setMultisigDaoBalances ( res . result )
471+ }
472+ } )
473+ } , [ setMultisigBanlances , setMultisigDaoBalances , isMainnet , configs ] )
376474 const hashToPayload = useMemo (
377475 ( ) =>
378476 configs . reduce < Record < string , string > > (
@@ -415,7 +513,7 @@ export const useSubscription = ({
415513 clearInterval ( interval )
416514 }
417515 } , [ isLightClient , getAndSaveMultisigSyncProgress ] )
418- return { multisigBanlances, multisigSyncProgress }
516+ return { multisigBanlances, multisigDaoBalances , multisigSyncProgress }
419517}
420518
421519export const useCancelWithLightClient = ( ) => {
0 commit comments