Skip to content

Commit

Permalink
Merge pull request #175 from ChainSafe/sign_verify_pk_webwallet
Browse files Browse the repository at this point in the history
Sign Verify Private Key
  • Loading branch information
KBryan authored Jul 5, 2022
2 parents aa975ae + f43bf86 commit ab56650
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 19 deletions.
8 changes: 8 additions & 0 deletions Plugins/Android.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Plugins/AsyncAwaitUtil.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Plugins/BouncyCastle.Crypto.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Plugins/Jint.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Plugins/Nethereum.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Web3Unity/Prefabs/PrivateKey/SignVerifyPK.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &7939348423131755349
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1857326031979140900}
- component: {fileID: 5699726257888795520}
m_Layer: 0
m_Name: SignVerifyPK
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1857326031979140900
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7939348423131755349}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &5699726257888795520
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7939348423131755349}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f9259fdff3465734daf9219e118083ce, type: 3}
m_Name:
m_EditorClassIdentifier:
7 changes: 7 additions & 0 deletions Web3Unity/Prefabs/PrivateKey/SignVerifyPK.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Web3Unity/Prefabs/Wallet/SignVerifyWebWallet.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2774818338625932009
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2774818338625932010}
- component: {fileID: 3278632383507343372}
m_Layer: 0
m_Name: SignVerifyWebWallet
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2774818338625932010
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2774818338625932009}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3278632383507343372
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2774818338625932009}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 22b7ebb5b91e09b4fa5c2911cefd95c5, type: 3}
m_Name:
m_EditorClassIdentifier:
7 changes: 7 additions & 0 deletions Web3Unity/Prefabs/Wallet/SignVerifyWebWallet.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Web3Unity/Prefabs/WebGL/SignVerifyExample.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 23, y: -48}
m_AnchoredPosition: {x: 23, y: -13}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &2799717858554657643
Expand Down Expand Up @@ -225,7 +225,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3c19034a790b61441adfd1dffc88b51c, type: 3}
m_Name:
m_EditorClassIdentifier:
message: hello
message: hello1
textHashedMessage: {fileID: 7523017095230044065}
textSignedHash: {fileID: 7303377407210195607}
verifyAddress: {fileID: 6576852011596053091}
Expand Down Expand Up @@ -295,7 +295,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 39.013, y: -153}
m_AnchoredPosition: {x: 39.013, y: -70}
m_SizeDelta: {x: 514.96, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &1190075119736674779
Expand Down
3 changes: 1 addition & 2 deletions Web3Unity/Scripts/Library/Web3PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public static string Address(string _privateKey)
public static string Sign(string _privateKey, string _message)
{
var signer = new EthereumMessageSigner();
EthECKey key = new EthECKey(_privateKey);
string signature = signer.EncodeUTF8AndSign(_message, new EthECKey(_privateKey));
string signature = signer.HashAndSign(_message, _privateKey);
return signature;
}
}
11 changes: 7 additions & 4 deletions Web3Unity/Scripts/Library/Web3Wallet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Signer;
using UnityEngine;
using Nethereum.Util;

Expand Down Expand Up @@ -62,9 +65,9 @@ public static async Task<string> Sign(string _message)

public static string Sha3(string _message)
{
Sha3Keccack hasher = new Sha3Keccack();
string hash = hasher.CalculateHash(_message);
// 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
return "0x" + hash;
var signer = new EthereumMessageSigner();
var hash = new Sha3Keccack().CalculateHash(_message).EnsureHexPrefix();
// 0x06b3dfaec148fb1bb2b066f10ec285e7c9bf402ab32aa78a5d38e34566810cd2
return hash;
}
}
1 change: 0 additions & 1 deletion Web3Unity/Scripts/Library/WebGL/Web3GL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,4 @@ public static int Network()
}

}

#endif
11 changes: 8 additions & 3 deletions Web3Unity/Scripts/Prefabs/Minter/MintWeb3Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ public class MintWeb3Wallet : MonoBehaviour

public string chain = "ethereum";
public string network = "rinkeby"; // mainnet ropsten kovan rinkeby goerli
public string account = PlayerPrefs.GetString("Account");
public string account;
public string to = "0x7259E32e35cf880aEACfbD412E7F4Baa8606e04c";
public string cid = "QmXjWjjMU8r39UCEZ8483aNedwNRFRLvvV9kwq1GpCgthj";
public string chainId = "4";


public void Start()
{
account = PlayerPrefs.GetString("Account");
}

// Start is called before the first frame update
public async void MintNFT()
{
CreateMintModel.Response nftResponse = await EVM.CreateMint(chain, network, account, to, cid);
Debug.Log("NFT Response: " + nftResponse);
account = PlayerPrefs.GetString("Account");
// connects to user's browser wallet (metamask) to send a transaction
try
{
string response = await Web3Wallet.SendTransaction(chainId, nftResponse.tx.to, nftResponse.tx.value, nftResponse.tx.data, nftResponse.tx.gasLimit, nftResponse.tx.gasPrice);
print(response);
Debug.Log(response);
} catch (Exception e) {
Debug.LogException(e, this);
}

}
}
13 changes: 9 additions & 4 deletions Web3Unity/Scripts/Prefabs/Minter/MintWebGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ public class MintWebGL : MonoBehaviour
// Start is called before the first frame update
public string chain = "ethereum";
public string network = "rinkeby"; // mainnet ropsten kovan rinkeby goerli
public string account = "0x148dC439Ffe10DF915f1DA14AA780A47A577709E";
public string account;
public string to = "0x148dC439Ffe10DF915f1DA14AA780A47A577709E";
public string cid = "QmXjWjjMU8r39UCEZ8483aNedwNRFRLvvV9kwq1GpCgthj";




public void Start()
{
account = PlayerPrefs.GetString("Account");
}

public async void MintNFT()
{
CreateMintModel.Response nftResponse = await EVM.CreateMint(chain, network, account, to, cid);
// connects to user's browser wallet (metamask) to send a transaction
try
{
{
string response = await Web3GL.SendTransactionData(nftResponse.tx.to, nftResponse.tx.value, nftResponse.tx.gasPrice,nftResponse.tx.gasLimit, nftResponse.tx.data);
print("Response: " + response);
} catch (Exception e) {
Expand Down
36 changes: 36 additions & 0 deletions Web3Unity/Scripts/Prefabs/PrivateKey/SignVerifyPK.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using Nethereum.Hex.HexConvertors.Extensions;
using UnityEngine;

public class SignVerifyPK : MonoBehaviour
{
// Start is called before the first frame update
async void Start()
{
var privateKey = "ADD_PRIVATE_KEY";
string message = "hello";
var hashedMessage = Web3Wallet.Sha3(message);
Debug.Log("Hashed Message PK: " + hashedMessage);
string signature = Web3PrivateKey.Sign(privateKey, message);
print("Signature PK: " + signature);
// get account from private key
string account = Web3PrivateKey.Address(privateKey);
print("Account from PK: " + account);
string address = await EVM.Verify(hashedMessage, signature);
print("Address From Verify PK: " + address);

ParseSignatureFunction(signature);
}
// Update is called once per frame
public void ParseSignatureFunction(string sig)
{
string signature = sig;
string r = signature.Substring(0, 66).EnsureHexPrefix();
Debug.Log("PK R:" + r);
string s = signature.Substring(66, 64).EnsureHexPrefix();
Debug.Log("PK S: " + s);
int v = int.Parse(signature.Substring(130, 2), System.Globalization.NumberStyles.HexNumber);
Debug.Log("PK V: " + v);
}
}
11 changes: 11 additions & 0 deletions Web3Unity/Scripts/Prefabs/PrivateKey/SignVerifyPK.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Web3Unity/Scripts/Prefabs/Wallet/SignVerifyWebWallet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text;
using Nethereum.Signer;
using Nethereum.Util;
using UnityEngine;

public class SignVerifyWebWallet : MonoBehaviour
{
// Start is called before the first frame update
async void Start()
{
string message = "hello";
string signature = await Web3Wallet.Sign(message);
//verification
SignVerifySignature(signature, message);
}

public void SignVerifySignature(string signatureString, string originalMessage)
{
string msg = "\x19" + "Ethereum Signed Message:\n" + originalMessage.Length + originalMessage;
byte[] msgHash = new Sha3Keccack().CalculateHash(Encoding.UTF8.GetBytes(msg));
EthECDSASignature signature = MessageSigner.ExtractEcdsaSignature(signatureString);
EthECKey key = EthECKey.RecoverFromSignature(signature, msgHash);

bool isValid = key.Verify(msgHash, signature);
Debug.Log("Address Returned: " + key.GetPublicAddress());
Debug.Log("Is Valid: " + isValid);
}
}
11 changes: 11 additions & 0 deletions Web3Unity/Scripts/Prefabs/Wallet/SignVerifyWebWallet.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ab56650

Please sign in to comment.