Qucik Learning Symbol Chapter 3
Account
An account is a data deposit structure in which information and assets associated with a private key is recorded. Only by signing with the private key associated with the account is the data updated on the blockchain.
3.1 Creating an account
The account contains a key pair, which is a set of private and public keys, an address and other information. First of all, try creating an account randomly and check the information that is contained.
3.1.1 Create a new account
$aliceKey = $facade->createAccount(PrivateKey::random());
var_dump($aliceKey);
Sample output
object(SymbolSdk\Symbol\SymbolAccount)#29 (4) {
["keyPair"]=>
object(SymbolSdk\Symbol\KeyPair)#27 (2) {
["_privateKey":"SymbolSdk\Symbol\KeyPair":private]=>
object(SymbolSdk\CryptoTypes\PrivateKey)#39 (1) {
["binaryData"]=>
string(32) "a �
���J}�i�h�H�E1=���|��nB�"
}
["_keyPair":"SymbolSdk\Symbol\KeyPair":private]=>
array(2) {
["publicKey"]=>
string(32) "v������z�|���f�*���%�43�1�S�"
["secretKey"]=>
string(64) "a �
���J}�i�h�H�E1=���|��nB�v������z�|���f�*���%�43�1�S�"
}
}
["_facade":protected]=>
object(SymbolSdk\Facade\SymbolFacade)#35 (1) {
["network"]=>
object(SymbolSdk\Symbol\Network)#43 (8) {
["generationHashSeed"]=>
object(SymbolSdk\CryptoTypes\Hash256)#41 (1) {
["binaryData"]=>
string(32) "I���'j����#I�̣�0.z�T��"yIO�e�"
}
["name"]=>
string(7) "testnet"
["identifier"]=>
int(152)
["datetimeConverter"]=>
object(SymbolSdk\Network\NetworkTimestampDatetimeConverter)#37 (2) {
["epoch"]=>
object(DateTime)#42 (3) {
["date"]=>
string(26) "2022-10-31 21:07:47.000000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(1) "Z"
}
["timeUnits"]=>
int(1)
}
["_addressHasher":protected]=>
string(8) "sha3-256"
["_createAddress":protected]=>
object(Closure)#38 (2) {
["this"]=>
*RECURSION*
["parameter"]=>
array(2) {
["$addressWithoutChecksum"]=>
string(10) ""
["$checksum"]=>
string(10) ""
}
}
["_addressClass":protected]=>
object(ReflectionClass)#21 (1) {
["name"]=>
string(24) "SymbolSdk\Symbol\Address"
}
["networkTimestampClass"]=>
object(ReflectionClass)#40 (1) {
["name"]=>
string(33) "SymbolSdk\Symbol\NetworkTimestamp"
}
}
}
["publicKey"]=>
object(SymbolSdk\Symbol\Models\PublicKey)#65 (1) {
["binaryData"]=>
string(32) "v������z�|���f�*���%�43�1�S�"
}
["address"]=>
object(SymbolSdk\Symbol\Models\UnresolvedAddress)#66 (1) {
["binaryData"]=>
string(24) "��@b����3
���-��CM�3p�"
}
}
echo $aliceKey->publicKey. PHP_EOL;
echo $aliceKey->keyPair->privateKey(). PHP_EOL;
Sample output
> 85B1B06DD5EE2A611325287705FA909442969B3C7FF47672B1EC34E9C*******
> EA648F781721F5094D77149886F18F6B2C2F621B2A3FB8CFA1E382B9E011DFF3
If the private key is lost, the data associated with that account can never be changed and any funds will be lost. In addition, the private key must not be shared with others since knowledge of the private key will give full access to the account. In general web services, passwords are allocated to an "account ID", so passwords can be changed from the account, but in blockchain, a unique ID (address) is allocated to the private key that is the password, thus it is not possible to change or re-generate the private key associated with an account from the account.
3.1.3 Deriving of address
$aliceRawAddress = $aliceKey->address;
echo $aliceRawAddress . PHP_EOL;
Sample output
> TCTEAYWL2X5PQMYKZD26SLPWUZBRX3KNWMZXBFY
These things above are the most basic information for operating the blockchain. It is also better to check how to generate accounts from a private key and how to generate classes that only deal with publickey and addresses.
3.1.4 Account generation from private key
$aliceKey = $facade->createAccount(new PrivateKey('85B1B06DD5EE2A611325287705FA909442969B3C7FF47672B1EC34E9C*******'));
$aliceRawAddress = $aliceKey->address;
echo $aliceRawAddress . PHP_EOL;
Sample output
>TCTEAYWL2X5PQMYKZD26SLPWUZBRX3KNWMZXBFY
3.1.5 Public key class generation
$alicePublicAccount = $facade->createPublicAccount(new PublicKey('EA648F781721F5094D77149886F18F6B2C2F621B2A3FB8CFA1E382B9E011DFF3'));
var_dump($alicePublicAccount->address);
echo substr($alicePublicAccount->publicKey, 2, 66) . PHP_EOL;
Sample output
object(SymbolSdk\Symbol\Models\UnresolvedAddress)#68 (1) {
["binaryData"]=>
string(24) "�o��dOv|����h!峯oZ4~\�"
}
EA648F781721F5094D77149886F18F6B2C2F621B2A3FB8CFA1E382B9E011DFF3
3.1.6 Address class generation
$aliceAddress = new Address('TDNX2EJJKC3C4QOGURQZLQS6KJLQACTGWYZFKTQ');
echo $aliceAddress->__tostring() . PHP_EOL;
Sample output
TDSSDPIPAJHVRZTQUAR36OQU6O7MV4BIAOLL5UA
3.2 TransferTransaction to another account
Creating an account does not simply mean that data can be transferred on the blockchain. Public blockchains require fees for data transfer in order to utilise resources effectively. On the Symbol blockchain, fees are paid with a native token which is called XYM. Once you have generated an account, send XYM to the account to cover transaction fees (described in later chapters).
3.2.1 Receive XYM from the faucet
Testnet XYM can be obtained for free using the faucet. For Mainnet transactions, you can buy XYM on exchanges, or use tipping services such QUEST to have obtain donations.
3.2.2 Using the explorer
Transactions can be viewed in the explorer after transferring from the faucet to the account you have created.
3.3 Check account information
Retrieve the account information stored by the node.
3.3.1 Retrieve a list of owned mosaics
$config = new Configuration();
$config->setHost($NODE_URL);
$client = new \GuzzleHttp\Client();
$accountApiInstance = new AccountRoutesApi($client, $config);
$account = $accountApiInstance->getAccountInfo($aliceAddress);
echo "\n===アカウント情報の確認===" . PHP_EOL;
echo $account . PHP_EOL;
Sample output
{
"id": "669B74CB84E82060AFB8C21B",
"account": {
"version": 1,
"address": "98DB7D112950B62E41C6A46195C25E5257000A66B632554E",
"addressHeight": "1591329",
"publicKey": "0000000000000000000000000000000000000000000000000000000000000000",
"publicKeyHeight": "0",
"accountType": 0,
"supplementalPublicKeys": {},
"activityBuckets": [],
"mosaics": [
{
"id": "72C0212E67A08BCE",
"amount": "1000000000"
}
],
"importance": "0",
"importanceHeight": "0"
}
}
publicKey
Account information which has just been created on the client side and has not yet been involved in a transaction on the blockchain is not recorded. Account information will be stored on the blockchain when when the address first appears in a transaction. Therefore, the publicKey is noted as `00000...` at this moment.
3.4 Tips for use
3.4.1 Encryption and signatures
Both private and public keys generated for an account can be used for conventional encryption and digital signatures. Data confidentiality and legitimacy can be verified on a p2p (end-to-end) basis, even if applications have reliability issues.Advance preparation: generating Bob account for connectivity test
$bobKey = new KeyPair(PrivateKey::random());
Encryption
Encrypt with Alice's private key and Bob's public key and decrypt with Alice's public key and Bob's private key (AES-GCM format).
$bobKey = new KeyPair(PrivateKey::random());
$message = "Hello Symbol!";
$encryptedMessage = $aliceKey->messageEncoder()->encode($bobKey->publicKey(), $message);
$encryptedMessage = $aliceMesgEncoder->encode($bobKey->publicKey(), $message);
echo strtoupper(bin2hex($encryptedMessage)) . PHP_EOL;
Sample output
> 0105949FB22EF11566D1E7F3EED230CE73D93D71BFD1AAE19D78315FD4F8028D1D16AC223A8A7E9D5AAB
Decrypt
$bobMsgEncoder = new MessageEncoder($bobKey);
$decryptMessageData = $bobMsgEncoder->tryDecode($aliceKey->keyPair->publicKey(), $encryptedMessage);
var_dump($decryptMessageData);
if($decryptMessageData['isDecoded']){
echo "\nDecoded message: " . PHP_EOL;
echo $decryptMessageData["message"] . PHP_EOL;
}else{
echo "\nFailed to decode message" . PHP_EOL;
}
Sample output
array(2) {
["isDecoded"]=>
bool(true)
["message"]=>
string(13) "Hello Symbol!"
}
> "Hello Symol!"
Signature
Sign the message with Alice's private key and verify the message with Alice's public key and signature.
$payload = "Hellow Symbol!";
$signature = $aliceKey->keyPair->sign($payload);
echo "\n===署名===" . PHP_EOL;
echo $signature . PHP_EOL;
Sample output
> 0x1D37BA4A84CF6161A3659038040BD446AC531F116083847E73795DD7188A512AE03BF5326991A81A5D6A0308662242BD77E767AF8F9B3CCC401CCE765AF17C03
Verification
$v = new Verifier($aliceKey->keyPair->publicKey());
$isVerified = $v->verify($payload, $signature);
echo "alice verified: " . PHP_EOL;
var_dump($isVerified);
$bobKey = new KeyPair(PrivateKey::random());
$v = new Verifier($bobKey->publicKey());
$isVerified = $v->verify($payload, $signature);
echo "bob verified: " . PHP_EOL;
var_dump($isVerified);
Sample output
alice verified:
bool(true)
bob verified:
bool(false)
Note that signatures that do not use the blockchain may be re-used many times.