@@ -2020,6 +2020,50 @@ class web3swiftTests: XCTestCase {
2020
2020
XCTAssert ( expectedAddress == signer, " Failed to sign personal message " )
2021
2021
}
2022
2022
2023
+ func testPersonalSignatureOnContract( ) {
2024
+ let web3 = Web3 . InfuraRinkebyWeb3 ( )
2025
+ let tempKeystore = try ! EthereumKeystoreV3 ( password: " " )
2026
+ let keystoreManager = KeystoreManager ( [ tempKeystore!] )
2027
+ web3. addKeystoreManager ( keystoreManager)
2028
+ let message = " Hello World "
2029
+ let expectedAddress = keystoreManager. addresses![ 0 ]
2030
+ print ( expectedAddress)
2031
+ let signRes = web3. personal. signPersonalMessage ( message: message. data ( using: . utf8) !, from: expectedAddress, password: " " )
2032
+ guard case . success( let signature) = signRes else { return XCTFail ( ) }
2033
+ let unmarshalledSignature = SECP256K1 . unmarshalSignature ( signatureData: signature) !
2034
+ print ( " V = " + String( unmarshalledSignature. v) )
2035
+ print ( " R = " + Data( unmarshalledSignature. r) . toHexString ( ) )
2036
+ print ( " S = " + Data( unmarshalledSignature. s) . toHexString ( ) )
2037
+ print ( " Personal hash = " + Web3. Utils. hashPersonalMessage ( message. data ( using: . utf8) !) !. toHexString ( ) )
2038
+ let jsonString = " [{ \" inputs \" :[], \" payable \" :false, \" stateMutability \" : \" nonpayable \" , \" type \" : \" constructor \" },{ \" constant \" :true, \" inputs \" :[{ \" name \" : \" _message \" , \" type \" : \" string \" }], \" name \" : \" hashPersonalMessage \" , \" outputs \" :[{ \" name \" : \" hash \" , \" type \" : \" bytes32 \" }], \" payable \" :false, \" stateMutability \" : \" pure \" , \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[{ \" name \" : \" _message \" , \" type \" : \" string \" },{ \" name \" : \" v \" , \" type \" : \" uint8 \" },{ \" name \" : \" r \" , \" type \" : \" bytes32 \" },{ \" name \" : \" s \" , \" type \" : \" bytes32 \" }], \" name \" : \" recoverSigner \" , \" outputs \" :[{ \" name \" : \" signer \" , \" type \" : \" address \" }], \" payable \" :false, \" stateMutability \" : \" pure \" , \" type \" : \" function \" }] "
2039
+ let contract = web3. contract ( jsonString, at: EthereumAddress ( " 0x6f1745a39059268e8e4572e97897b50e4aab62a8 " ) , abiVersion: 2 )
2040
+ var options = Web3Options . defaultOptions ( )
2041
+ options. from = expectedAddress
2042
+ var intermediate = contract? . method ( " hashPersonalMessage " , parameters: [ message as AnyObject ] , options: options)
2043
+ var result = intermediate!. call ( options: nil )
2044
+ switch result {
2045
+ case . success( let res) :
2046
+ guard let hash = res [ " hash " ] ! as? Data else { return XCTFail ( ) }
2047
+ XCTAssert ( Web3 . Utils. hashPersonalMessage ( message. data ( using: . utf8) !) ! == hash)
2048
+ case . failure( let error) :
2049
+ print ( error)
2050
+ XCTFail ( )
2051
+ }
2052
+
2053
+ intermediate = contract? . method ( " recoverSigner " , parameters: [ message, unmarshalledSignature. v, Data ( unmarshalledSignature. r) , Data ( unmarshalledSignature. s) ] as [ AnyObject ] , options: options)
2054
+ result = intermediate!. call ( options: nil )
2055
+ switch result {
2056
+ case . success( let res) :
2057
+ guard let signer = res [ " signer " ] ! as? EthereumAddress else { return XCTFail ( ) }
2058
+ XCTAssert ( signer == expectedAddress)
2059
+ case . failure( let error) :
2060
+ print ( error)
2061
+ XCTFail ( )
2062
+ }
2063
+ }
2064
+
2065
+
2066
+
2023
2067
func testPerformanceExample( ) {
2024
2068
// This is an example of a performance test case.
2025
2069
self . measure {
0 commit comments