我目前正在 Solidity 中开发一个 dApp,并正在开发一个 web3 库来处理与其通信。
我在 web3 中创建新帐户和签署交易的过程中遇到了困难。在继续之前,值得注意的是,我正在运行自己的本地私有区块链(目前使用 Ganache)。
我的代码如下所示:
try{
let a = web3.eth.accounts.create()
let dataTx = someContract.methods.someMethod().encodeABI()
let rawTx = {
to: someContract._address,
from: account.address,
data: dataTx,
gas: 10000000000
}
const transaction = web3.eth.accounts.signTransaction(rawTx, util.toBuffer(account.privateKey))
web3.eth.sendTransaction(rawTx).then(console.log)
}
catch(e){
console.log(e)
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是该web3.eth.sendTransaction()方法引发以下异常:
错误:返回错误:无法识别发件人帐户。
我的理解是web3.eth.accounts用于管理本地帐户并web3.eth.personal用于与客户端(例如 Geth)进行通信。我希望保留我的应用程序在 web3 客户端设备上本地创建的帐户的私钥,但它引发了此异常。
我哪里错了?我应该在运行交易之前在某个地方注册新创建的帐户吗?我在这里遗漏了一些重要信息吗?
到目前为止,我已经使用Spring Boot完成了一个(REST)项目,并且非常喜欢它。我发现有些曲解是我对的理解@RequestBody。
假设我有下面的POST方法来登录用户。我的用户实体可能包含除我希望发布请求后具有的用户名和密码之外的其他属性。在这种情况下,我看不到其他选择,只能创建一个额外的对象(LoginRequest)来保存传入数据的数据。
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> login(@RequestBody LoginRequest request) {
User p = null;
if (request != null) {
p = User.login(request.getEmail(), request.getPassword()); // validates and returns user if exists
if (p != null){
return new ResponseEntity<User>(p, HttpStatus.OK);
}
}
throw new IllegalArgumentException("Password or email incorrect");
}
Run Code Online (Sandbox Code Playgroud)
同样,我想@ResponseBody返回一个最小化版本的User对象,例如,其中排除了密码。
解决此问题的标准方法有哪些?我真的必须为每个“ json-view”创建一个单独的对象吗?我之前在python中做了一些REST的工作,在这里我只是拥有一个包含请求属性的Dictionary。有类似的方法吗?
C使用Xcode 运行这个小脚本时,我收到以下消息:
Format specifies type 'float *' but the argument has type 'double" at scanf("%f", v) and scanf("%f", i).
我没有得到它,因为我没有声明任何double类型变量.
int main(int argc, const char * argv[]) {
char choice[10];
float v;
float i;
float r;
printf("What would you like to calculate?: ");
scanf("%s", choice);
printf("\nYou chose: \n""%s", choice);
if (strcmp(choice, "r") == 0)
{
printf("\nPlease enter voltage (V): \n");
scanf("%f", v);
printf("\nPlease enter current (I): \n");
scanf("%f", i);
r = v/i;
printf("%f", r);
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?