ethers-io / ethers.objc

Fast, simple and complete library for Ethereum in Objective-C.
MIT License
157 stars 57 forks source link

How can I new a account use password? #16

Open Musk66 opened 6 years ago

Musk66 commented 6 years ago

Hi,how can I add a account use password?I only use the method 'randomMnemonicAccoun',but how can I know the phrase?I want to use phase to add a account.

ricmoo commented 6 years ago

Here is a quick example of what you are trying to do:

var wallet = Wallet.createRandom()
console.log(wallet.mnemonic)
// front chair enroll shrug twin expand forest magnet ice magic sport diet
console.log(wallet.mnemonic)
// "m/44'/60'/0'/0/0"
wallet.encrypt('password123').then(function(json) { console.log(json); })
// {"address":"ce218cf9f551848292becabd20c2d0270777173f","id":"24ae1c9d-3277-44d5-852a-5bd109c1f6b5","version":3,"Crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"41e9fbcbb087bf41f34de7009a16fb3f"},"ciphertext":"4b50c613404c7d8ffa0c3f549f40e5aef08fc7125c5e0f608d531090c8a7636d","kdf":"scrypt","kdfparams":{"salt":"1f338ad7089f4de9d3479f899ee212707e5c28225ec9437c637505c1898e2321","n":131072,"dklen":32,"p":1,"r":8},"mac":"a7652cf43d85e5e110657d6425ac4711802370c360016f64b2fa45bf65818a65"},"x-ethers":{"client":"ethers.js","gethFilename":"UTC--2018-03-26T02-57-52.0Z--ce218cf9f551848292becabd20c2d0270777173f","mnemonicCounter":"3a98a527914f611046a994eee976f436","mnemonicCiphertext":"2a95aba57f66b2051a3aefad605253dd","version":"0.1"}}
ricmoo commented 6 years ago

Oh! Sorry... That was for ethers.js.

ricmoo commented 6 years ago

Here is the example for ethers.objc (sorry, most questions are for my JavaScript version and I didn't notice the repo):

Account *account = [Account randomMnemonicAccount];
NSLog(@"Menonic Phrase: %@", account.mnemonicPhrase);
[account encryptSecretStorageJSON: @"password123" callback: ^(NSString json) {
    NSLog(@"Password encrypted JSON: %@", json);
}];
Musk66 commented 6 years ago

Thanks.It works when I used this method.