@ -7,6 +7,8 @@
// / h t t p s : / / g i t h u b . c o m / e t h e r e u m / E I P s / b l o b / m a s t e r / E I P S / e i p - 7 1 2 . m d
import Foundation
import AlphaWalletABI
import AlphaWalletAddress
import AlphaWalletCore
import BigInt
// / A s t r u c t r e p r e s e n t s E I P 7 1 2 t y p e t u p l e
@ -81,8 +83,8 @@ extension EIP712TypedData {
let primaryType = primaryType . dropTrailingSquareBrackets
var found = dependencies
guard ! found . contains ( primaryType ) ,
let primaryTypes = types [ primaryType ] else {
return found
let primaryTypes = types [ primaryType ] else {
return found
}
found . insert ( primaryType )
for type in primaryTypes {
@ -98,10 +100,10 @@ extension EIP712TypedData {
depSet . remove ( primaryType )
let sorted = [ primaryType ] + Array ( depSet ) . sorted ( )
let encoded = sorted . compactMap { type in
guard let values = types [ type ] else { return nil }
let param = values . map { " \( $0 . type ) \( $0 . name ) " } . joined ( separator : " , " )
return " \( type ) ( \( param ) ) "
} . joined ( )
guard let values = types [ type ] else { return nil }
let param = values . map { " \( $0 . type ) \( $0 . name ) " } . joined ( separator : " , " )
return " \( type ) ( \( param ) ) "
} . joined ( )
return encoded . data ( using : . utf8 ) ? ? Data ( )
}
@ -210,7 +212,7 @@ extension EIP712TypedData {
// / H e l p e r f u n c f o r e n c o d i n g u i n t / i n t t y p e s
private func parseIntSize ( type : String , prefix : String ) -> Int {
guard type . starts ( with : prefix ) ,
let size = Int ( type . dropFirst ( prefix . count ) ) else {
let size = Int ( type . dropFirst ( prefix . count ) ) else {
return - 1
}
@ -458,3 +460,34 @@ extension EIP712TypedData.JSON {
return nil
}
}
fileprivate extension Data {
init ? ( hexString : String ) {
let string : Substring
if hexString . hasPrefix ( " 0x " ) {
string = hexString . dropFirst ( 2 )
} else {
string = Substring ( hexString )
}
self . init ( capacity : string . count / 2 )
for offset in stride ( from : 0 , to : string . count , by : 2 ) {
let start = string . index ( string . startIndex , offsetBy : offset )
guard string . distance ( from : start , to : string . endIndex ) >= 2 else {
let byte = string [ start . . . ]
guard let number = UInt8 ( byte , radix : 16 ) else {
return nil
}
append ( number )
break
}
let end = string . index ( string . startIndex , offsetBy : offset + 2 )
let byte = string [ start . . < end ]
guard let number = UInt8 ( byte , radix : 16 ) else {
return nil
}
append ( number )
}
}
}