2024-05-06 20:22:43 +00:00
. import StatusQ . Core . Utils 0.1 as SQUtils
2024-05-31 09:58:47 +00:00
function chainIdFromEip155 ( chain ) {
return parseInt ( chain . split ( ':' ) . pop ( ) . trim ( ) , 10 )
}
2024-06-07 16:54:19 +00:00
function isHex ( str ) {
return str . startsWith ( '0x' ) && str . length % 2 === 0 && /^[0-9a-fA-F]*$/ . test ( str . slice ( 2 ) )
}
2024-05-31 09:58:47 +00:00
function hexToString ( hex ) {
if ( hex . startsWith ( "0x" ) ) {
hex = hex . substring ( 2 ) ;
}
var str = '' ;
for ( var i = 0 ; i < hex . length ; i += 2 ) {
str += String . fromCharCode ( parseInt ( hex . substr ( i , 2 ) , 16 ) ) ;
}
return str ;
}
function strToHex ( str ) {
var hex = '' ;
for ( var i = 0 ; i < str . length ; i ++ ) {
var byte = str . charCodeAt ( i ) . toString ( 16 ) ;
hex += ( byte . length < 2 ? '0' : '' ) + byte ;
}
return '0x' + hex ;
}
2024-05-06 20:22:43 +00:00
function extractChainsAndAccountsFromApprovedNamespaces ( approvedNamespaces ) {
const eip155Data = approvedNamespaces . eip155 ;
2024-05-31 09:58:47 +00:00
const chains = eip155Data . chains . map ( chainIdFromEip155 ) ;
2024-05-06 20:22:43 +00:00
const accountSet = new Set (
eip155Data . accounts . map ( account => account . split ( ':' ) . pop ( ) . trim ( ) )
) ;
const uniqueAccounts = Array . from ( accountSet ) ;
return { chains , accounts : uniqueAccounts } ;
}
2024-05-31 09:58:47 +00:00
function buildSupportedNamespacesFromModels ( chainsModel , accountsModel , methods ) {
2024-05-06 20:22:43 +00:00
var chainIds = [ ]
var addresses = [ ]
for ( let i = 0 ; i < chainsModel . count ; i ++ ) {
let entry = SQUtils . ModelUtils . get ( chainsModel , i )
chainIds . push ( parseInt ( entry . chainId ) )
}
for ( let i = 0 ; i < accountsModel . count ; i ++ ) {
let entry = SQUtils . ModelUtils . get ( accountsModel , i )
addresses . push ( entry . address )
}
2024-05-31 09:58:47 +00:00
return buildSupportedNamespaces ( chainIds , addresses , methods )
2024-05-06 20:22:43 +00:00
}
2024-05-31 09:58:47 +00:00
function buildSupportedNamespaces ( chainIds , addresses , methods ) {
2024-07-29 13:50:08 +00:00
let eipChainIds = [ ]
let eipAddresses = [ ]
2024-05-06 20:22:43 +00:00
for ( let i = 0 ; i < chainIds . length ; i ++ ) {
let chainId = chainIds [ i ]
eipChainIds . push ( ` "eip155: ${ chainId } " ` )
for ( let i = 0 ; i < addresses . length ; i ++ ) {
eipAddresses . push ( ` "eip155: ${ chainId } : ${ addresses [ i ] } " ` )
}
}
2024-05-31 09:58:47 +00:00
let methodsStr = methods . map ( method => ` " ${ method } " ` ) . join ( ',' )
2024-05-06 20:22:43 +00:00
return ` {
2024-07-29 13:50:08 +00:00
"eip155" : {
"chains" : [ $ { eipChainIds . join ( ',' ) } ] ,
"methods" : [ $ { methodsStr } ] ,
2024-08-21 05:42:25 +00:00
"events" : [ "chainChanged" , "accountsChanged" , "message" , "disconnect" , "connect" ] ,
2024-07-29 13:50:08 +00:00
"accounts" : [ $ { eipAddresses . join ( ',' ) } ]
}
} `
2024-07-05 09:32:31 +00:00
}
function validURI ( uri ) {
2024-07-24 18:25:15 +00:00
const regex = /^wc:[0-9a-fA-F-]*@([1-9][0-9]*)(\?([a-zA-Z-]+=[^&]+)(&[a-zA-Z-]+=[^&]+)*)?$/
2024-07-05 09:32:31 +00:00
return regex . test ( uri )
}
function extractInfoFromPairUri ( uri ) {
2024-07-24 18:25:15 +00:00
let topic = ""
let expiry = NaN
2024-07-05 09:32:31 +00:00
// Extract topic and expiry from wc:99fdcac5cc081ac8c1181b4c38c5dc49fb5eb212706d5c94c445be549765e7f0@2?expiryTimestamp=1720090818&relay-protocol=irn&symKey=c6b67d94174bd42d16ff288220ce9b8966e5b56a2d3570a30d5b0a760f1953f0
const regex = /wc:([0-9a-fA-F]*)/
const match = uri . match ( regex )
if ( match ) {
topic = match [ 1 ]
}
2024-07-24 18:25:15 +00:00
let parts = uri . split ( '?' )
2024-07-05 09:32:31 +00:00
if ( parts . length > 1 ) {
2024-07-24 18:25:15 +00:00
const params = parts [ 1 ] . split ( '&' )
2024-07-05 09:32:31 +00:00
for ( let i = 0 ; i < params . length ; i ++ ) {
2024-07-24 18:25:15 +00:00
const keyVal = params [ i ] . split ( '=' )
2024-07-05 09:32:31 +00:00
if ( keyVal [ 0 ] === 'expiryTimestamp' ) {
expiry = parseInt ( keyVal [ 1 ] )
}
}
}
return { topic , expiry }
2024-07-17 10:46:43 +00:00
}
2024-07-23 14:26:55 +00:00
function filterActiveSessionsForKnownAccounts ( sessions , accountsModel ) {
let knownSessions = ( { } )
Object . keys ( sessions ) . forEach ( ( topic ) => {
const session = sessions [ topic ]
const eip155Addresses = session . namespaces . eip155 . accounts
const accountSet = new Set (
eip155Addresses . map ( eip155Address => eip155Address . split ( ':' ) . pop ( ) . trim ( ) )
) ;
const uniqueAddresses = Array . from ( accountSet ) ;
const firstAccount = SQUtils . ModelUtils . getFirstModelEntryIf ( accountsModel , ( account ) => {
return uniqueAddresses . includes ( account . address )
} )
if ( ! firstAccount ) {
return
}
knownSessions [ topic ] = session
} )
return knownSessions
2024-08-05 13:41:20 +00:00
}
function getAccountsInSession ( session ) {
const eip155Addresses = session . namespaces . eip155 . accounts
const accountSet = new Set (
eip155Addresses . map ( eip155Address => eip155Address . split ( ':' ) . pop ( ) . trim ( ) )
) ;
const uniqueAddresses = Array . from ( accountSet ) ;
return uniqueAddresses
}