@@ -15,14 +15,20 @@ import { bytes } from '@ckb-lumos/codec'
1515import { Allocation , Balances } from '../utils/perun-wallet-wrapper/wire'
1616// import PerunPersistorService from '../services/perun/persistor'
1717// import PerunChannelEntity from '../database/chain/entities/perun-channel'
18- import { mol } from "@ckb-ccc/core"
18+ import { ccc , mol } from "@ckb-ccc/core"
19+ import participant from 'src/services/perun/tools/participant'
1920
2021const defaultAddressEncoder : AddressEncoder = ( add : Uint8Array | string ) => {
2122 if ( typeof add === 'string' ) {
2223 return bytes . bytify ( add )
2324 }
2425 return add
2526}
27+ const equalNumPaddedHex = ( num : bigint ) => {
28+ const hex = num . toString ( 16 )
29+ const res = hex . length % 2 === 0 ? hex : `0${ hex } `
30+ return `0x${ res } `
31+ }
2632
2733export default class PerunController {
2834 static emiter = new EventEmitter ( )
@@ -137,21 +143,34 @@ export default class PerunController {
137143 } as Controller . Response
138144 }
139145
140- async openChannel ( params : Controller . Params . OpenChannelParams ) : Promise < Controller . Response > {
146+ async openChannel ( params : PerunAPI . OpenChannelParams ) : Promise < Controller . Response > {
141147 logger . info ( 'PerunController: openChannel----------' )
148+ const { me, peer, balances, challengeDuration } = params ;
149+ const meRequestId = await participant . encode ( me . publicKey , me . address ) ;
150+ const peerRequestId = await participant . encode ( peer . publicKey , peer . address ) ;
151+ const { assets, balances : iBalances } = balances . reduce ( ( obj , item ) => {
152+ obj . assets . push ( item . type ? ccc . Script . from ( item . type ) . toBytes ( ) : new Uint8Array ( 0 ) )
153+ obj . balances . push (
154+ item . balances . map ( balance => bytes . bytify ( equalNumPaddedHex ( BigInt ( balance ) ) ) ) ,
155+ )
156+ return obj ;
157+ } , { assets : [ ] as Uint8Array [ ] , balances : [ ] as Uint8Array [ ] [ ] } )
142158 const alloc = Allocation . create ( {
143- assets : [ new Uint8Array ( 1 ) ] ,
159+ assets : assets ,
144160 balances : Balances . create ( {
145- balances : [
146- {
147- balance : params . balances ,
148- } ,
149- ] ,
161+ balances : iBalances . map ( balance => ( {
162+ balance
163+ } ) )
164+ // balances: [
165+ // {
166+ // balance: params.balances,
167+ // },
168+ // ],
150169 } ) ,
151170 } )
152171
153172 const res = await PerunController . serviceClient
154- . openChannel ( params . me , params . peer , alloc , params . challengeDuration , mol . Uint32 . encode ( 123 ) )
173+ . openChannel ( meRequestId , peerRequestId , alloc , challengeDuration , mol . Uint32 . encode ( 123 ) )
155174 . catch ( e => {
156175 logger . info ( 'PerunController: openChannel-----error-----' , e )
157176 return {
@@ -169,8 +188,8 @@ export default class PerunController {
169188 }
170189 }
171190 const channelId = channelIdToString ( new Uint8Array ( res . channelId ! ) )
172- logger . log ( 'Controler Buffer channelId' , res . channelId ! )
173- logger . log ( 'Controler channelID' , channelId )
191+ // logger.log('Controler Buffer channelId', res.channelId!)
192+ // logger.log('Controler channelID', channelId)
174193 return {
175194 status : ResponseCode . Success ,
176195 result : {
0 commit comments