我的Solidity程序运行良好..我只是将Environment设置为web3 provider。然后我将abi和地址复制到index.html中。我还将默认帐户设置为 web3.eth.defaultAccount = web3.eth.accounts [0]; 但连接无法正常工作,表示未定义帐户 这是我的团结计划
pragma solidity ^0.4.11;
合同HDataAccessManager {
mapping (string => string) dataLink;
mapping (string => string) dataHash;
mapping (string => bytes32) private passwords;
string[] users;
function _isValid(string _user_id, string _encryption_key_hash) returns (bool) {
for(uint i = 0;i<users.length;i++){
if(keccak256(users[i]) == keccak256(_user_id) &&
keccak256(_encryption_key_hash) == passwords[_user_id]) {
return true;
}
}
return false;
}
function addUser(string _user_id, string _encryption_key_hash) {
users.push(_user_id);
passwords[_user_id] = keccak256(_encryption_key_hash);
}
function totalUsers() returns (uint) {
return users.length;
}
// link is alias of generated_id
function storeLink(string _user_id, string _link){
// check if user is present in blockchain
//require(_isValid(_user_id, _encryption_key_hash));
dataLink[_user_id] = _link;
}
function storeHash(string _user_id, string _hash){
// check if user is present in blockchain
//require(_isValid(_user_id, _encryption_key_hash));
dataHash[_user_id] = _hash;
}
function retrieveLink(string _user_id, string _encryption_key_hash) returns (string, string) {
//check if user is present in blockchain
require(_isValid(_user_id, _encryption_key_hash));
string link = dataLink[_user_id];
string hash = dataHash[_user_id];
return (link, hash);
}
}
这是索引.html
{% extends 'website/templates/base.html' %}
{% load widget_tweaks %}
{% load static %}
{% block title %}Profile{% endblock %}
{% block content%}
<link rel="stylesheet" href="{% static 'website/css/login.css' %}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/sha1.js"></script>
<script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
if (typeof web3 !== 'undefined') {
web3 =new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
web3.eth.defaultAccount = web3.eth.accounts[0];
json_string='[
{
"constant": false,
"inputs": [
{
"name": "_user_id",
"type": "string"
},
{
"name": "_encryption_key_hash",
"type": "string"
}
],
"name": "_isValid",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function",
"stateMutability": "nonpayable"
},
{
"constant": false,
"inputs": [
{
"name": "_user_id",
"type": "string"
},
{
"name": "_encryption_key_hash",
"type": "string"
}
],
"name": "addUser",
"outputs": [],
"payable": false,
"type": "function",
"stateMutability": "nonpayable"
},
{
"constant": false,
"inputs": [
{
"name": "_user_id",
"type": "string"
},
{
"name": "_encryption_key_hash",
"type": "string"
}
],
"name": "retrieveLink",
"outputs": [
{
"name": "",
"type": "string"
},
{
"name": "",
"type": "string"
}
],
"payable": false,
"type": "function",
"stateMutability": "nonpayable"
},
{
"constant": false,
"inputs": [
{
"name": "_user_id",
"type": "string"
},
{
"name": "_hash",
"type": "string"
}
],
"name": "storeHash",
"outputs": [],
"payable": false,
"type": "function",
"stateMutability": "nonpayable"
},
{
"constant": false,
"inputs": [
{
"name": "_user_id",
"type": "string"
},
{
"name": "_link",
"type": "string"
}
],
"name": "storeLink",
"outputs": [],
"payable": false,
"type": "function",
"stateMutability": "nonpayable"
},
{
"constant": false,
"inputs": [],
"name": "totalUsers",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function",
"stateMutability": "nonpayable"
}
]';
abi = JSON.parse(json_string);
HealthcareContract = web3.eth.contract(abi);
// In your nodejs console, execute contractInstance.address to get the address at which the contract is deployed and change the line below to use your deployed address
var contractInstance = HealthcareContract.at('0xf25bafe8f789289c37e6df6888f4a3bb13e2a0c5');
var encryption_key_hash = "";
function validate_form(){
var name = document.getElementById("name").value;
if(name.length==0){
var name_err = document.getElementById("name_error");
name_err.style.display = "block";
return false;
}
var address = document.getElementById("address").value;
if(address.length==0){
var address_err = document.getElementById("address_error");
address_err.style.display = "block";
return false;
}
var dob = document.getElementById("dob").value;
var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
if(dob.length==0 ){
var dob_err = document.getElementById("dob_error");
dob_err.style.display = "block";
return false;
}
var key1 = document.getElementById("encrky").value;
if(key1.length==0){
var enncrky_err = document.getElementById("encrky_error");
enncrky_err.style.display = "block";
return false;
}
var key2 = document.getElementById("re_encrky").value;
if(key2 != key1){
var re_encrky_err = document.getElementById("re_encrky_error");
re_encrky_err.style.display = "block";
return false;
}
return true;
}
function encrypt_data(){
var key2 = document.getElementById("re_encrky").value;
var name = document.getElementById("name").value;
var address = document.getElementById("address").value;
var dob = document.getElementById("dob").value;
document.getElementById("name").value = CryptoJS.AES.encrypt(name, key2);
document.getElementById("address").value = CryptoJS.AES.encrypt(address, key2);;
document.getElementById("dob").value = CryptoJS.AES.encrypt(dob, key2);;
var hash = CryptoJS.SHA1(key2);
document.getElementById("encrky").value = CryptoJS.enc.Hex.stringify(hash);
encryption_key_hash = CryptoJS.enc.Hex.stringify(hash);
}
function submitButton(){
if(validate_form()){
encrypt_data();
var key2 = document.getElementById("re_encrky").value;
var name = document.getElementById("name").value;
var address = document.getElementById("address").value;
var dob = document.getElementById("dob").value;
var username = '{{user.username}}';
console.log("form about to be submitted");
contractInstance.addUser(username, encryption_key_hash, {from: web3.eth.accounts[0]}, function(result){
console.log(result);
$("form").submit();
})
}
}
</script>
<form method="post" class="w3-container w3-card-4 w3-light-grey w3-text-teal w3-margin" name="form" action="">{% csrf_token %}
<h2 class="w3-center">Profile</h2>
<div class="w3-row w3-section">
<div class="w3-rest">
<center>
<input type="text" id="name" name="name" placeholder="Full Name">
<div id="name_error" style="display: none;"> Please enter the name</div>
</center>
</div>
</div>
<div class="w3-row w3-section">
<div class="w3-rest">
<center>
<input type="text" id="address" name="address" placeholder="Address">
<div id="address_error" style="display: none;"> Please enter the address</div>
</center>
</div>
</div>
<div class="w3-row w3-section">
<div class="w3-rest">
<center>
<input type="text" id="dob" name="dob" placeholder="Date of Birth (eg. DD/MM/YYYY)" >
<div id="dob_error" style="display: none;"> Please enter the correct Date of Birth</div>
</center>
</div>
</div>
<div class="w3-row w3-section">
<div class="w3-rest">
<center>
<input type="text" id="encrky" name="encrky" placeholder="Encryption Key">
<div id="encrky_error" style="display: none;"> Please enter encryption key</div>
</center>
</div>
</div>
<div class="w3-row w3-section">
<div class="w3-rest">
<center>
<input type="text" id="re_encrky" placeholder="Encryption Key">
<div id="re_encrky_error" style="display: none;"> Encryption Key do not match</div>
</center>
</div>
</div>
<center>
<button type="button" class="w3-button w3-section w3-teal w3-ripple w3-padding" onclick="submitButton()">Save</button>
</center>
</form>
{% endblock %}