diff --git a/smartcontract/README.md b/smartcontract/README.md index 940bb217..005b8001 100644 --- a/smartcontract/README.md +++ b/smartcontract/README.md @@ -54,48 +54,48 @@ You can also create and deploy any custom wallet (contract), see below. ### Create and deploy SimpleR3 wallet ```java -byte[]secretKey=Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F2C1D960A1F1D65E1E4"); - TweetNaclFast.Signature.KeyPair keyPair=TweetNaclFast.Signature.keyPair_fromSeed(secretKey); +byte[] secretKey = Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F2C1D960A1F1D65E1E4"); +TweetNaclFast.Signature.KeyPair keyPair = TweetNaclFast.Signature.keyPair_fromSeed(secretKey); - Options options=Options.builder() - .publicKey(keyPair.getPublicKey()) - .wc(0L) - .build(); +Options options = Options.builder() + .publicKey(keyPair.getPublicKey()) + .wc(0L) + .build(); - Wallet wallet=new Wallet(WalletVersions.simpleR3,options); - SimpleWalletContractR3 contract=wallet.create(); - InitExternalMessage msg=contract.createInitExternalMessage(keyPair.getSecretKey()); - Address walletAddress=msg.address; +Wallet wallet = new Wallet(WalletVersions.simpleR3,options); +SimpleWalletContractR3 contract = wallet.create(); +InitExternalMessage msg = contract.createInitExternalMessage(keyPair.getSecretKey()); +Address walletAddress = msg.address; - log.info("new wallet address = {}",walletAddress.toString(false)); - log.info("Non-bounceable address (for init): {}",walletAddress.toString(true,true,false,true)); - log.info("Bounceable address (for later access): {}",walletAddress.toString(true,true,true,true)); +log.info("new wallet address = {}", walletAddress.toString(false)); +log.info("Non-bounceable address (for init): {}", walletAddress.toString(true,true,false,true)); +log.info("Bounceable address (for later access): {}", walletAddress.toString(true,true,true,true)); // Before sending wallet's smart contract code, send some Toncoins to non-bouncelable address. // deploy - Tonlib tonlib=Tonlib.builder().build(); - String base64boc=Utils.bytesToBase64(msg.message.toBoc(false)); - tonlib.sendRawMessage(base64boc); +Tonlib tonlib = Tonlib.builder().build(); +String base64boc = Utils.bytesToBase64(msg.message.toBoc(false)); +tonlib.sendRawMessage(base64boc); ``` ### Send Toncoins ```java ... - ExternalMessage msg=contract.createTransferMessage(keyPair.getSecretKey(), - "0:258e549638a6980ae5d3c76382afd3f4f32e34482dafc3751e3358589c8de00d", //destination address - Utils.toNano(1), // toncoin - 1L); // seqno - Address address=msg.address; - log.info("Source wallet address = {}",address.toString(false)); - log.info("signing message: {}",msg.signingMessage.print()); - log.info("resulting external message: {}",msg.message.print()); +ExternalMessage msg = contract.createTransferMessage(keyPair.getSecretKey(), +"0:258e549638a6980ae5d3c76382afd3f4f32e34482dafc3751e3358589c8de00d", //destination address + Utils.toNano(1), // toncoin + 1L); // seqno +Address address = msg.address; +log.info("Source wallet address = {}", address.toString(false)); +log.info("signing message: {}", msg.signingMessage.print()); +log.info("resulting external message: {}", msg.message.print()); // send external message - Tonlib tonlib=Tonlib.builder().build(); - String base64boc=Utils.bytesToBase64(msg.message.toBoc(false)); - tonlib.sendRawMessage(base64boc); +Tonlib tonlib = Tonlib.builder().build(); +String base64boc = Utils.bytesToBase64(msg.message.toBoc(false)); +tonlib.sendRawMessage(base64boc); ``` ### Deploy custom contract @@ -113,33 +113,20 @@ it. var signature = in_msg~load_bits(512); ;; signature in msg body, see createInitExternalMessage() var cs = in_msg; var (msg_seqno, valid_until, extra_field) = (cs~load_uint(32), cs~load_uint(32), cs~load_uint(64)); ;; payload in message body, see createSigningMessage() - throw_if - - - - - - - - - - - - - (35, valid_until < = now()); - var ds = get_data().begin_parse(); - var (stored_seqno, public_key, stored_x_data) = (ds~load_uint(32), ds~load_uint(256), ds~load_uint(64)); ;; data in stateInit or in storage. See createDataCell() - ;; ds.end_parse(); - throw_unless(33, msg_seqno == stored_seqno); - throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); - accept_message(); - cs~touch(); ;; process data stored in ext message refs - while (cs.slice_refs()) { ;; transfer msg. Data inside the body of external msg. See - var mode = cs~load_uint(8); - send_raw_message(cs~load_ref(), mode); - } - cs.end_parse(); - set_data(begin_cell().store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).store_uint(stored_x_data, 64).store_uint(extra_field, 64).end_cell()); + throw_if(35, valid_until < = now()); + var ds = get_data().begin_parse(); + var (stored_seqno, public_key, stored_x_data) = (ds~load_uint(32), ds~load_uint(256), ds~load_uint(64)); ;; data in stateInit or in storage. See createDataCell() + ;; ds.end_parse(); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); + accept_message(); + cs~touch(); ;; process data stored in ext message refs + while (cs.slice_refs()) { ;; transfer msg. Data inside the body of external msg. See + var mode = cs~load_uint(8); + send_raw_message(cs~load_ref(), mode); + } +cs.end_parse(); +set_data(begin_cell().store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).store_uint(stored_x_data, 64).store_uint(extra_field, 64).end_cell()); } int seqno() method_id { @@ -380,21 +367,20 @@ public class CustomContract implements WalletContract { Now you are ready to deploy your custom smart contract. ```java +byte[] secretKey = Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F2C1D960A1F1D65E1E4"); +TweetNaclFast.Signature.KeyPair keyPair = TweetNaclFast.Signature.keyPair_fromSeed(secretKey); -byte[]secretKey=Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F2C1D960A1F1D65E1E4"); - TweetNaclFast.Signature.KeyPair keyPair=TweetNaclFast.Signature.keyPair_fromSeed(secretKey); - - Options options=Options.builder() - .publicKey(keyPair.getPublicKey()) - .wc(0L) - .build(); +Options options = Options.builder() + .publicKey(keyPair.getPublicKey()) + .wc(0L) + .build(); - CustomContract customContract=new CustomContract(options); +CustomContract customContract = new CustomContract(options); - InitExternalMessage msg=customContract.createInitExternalMessage(keyPair.getSecretKey()); - Address address=msg.address; +InitExternalMessage msg = customContract.createInitExternalMessage(keyPair.getSecretKey()); +Address address = msg.address; - log.info("Creating new wallet in workchain {} \n"+ +log.info("Creating new wallet in workchain {} \n"+ "Loading private key from file new-wallet.pk\n"+ "StateInit: {}\nnew wallet address = {}\n"+ "(Saving address to file new-wallet.addr)\n"+ @@ -416,45 +402,44 @@ byte[]secretKey=Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F Send some toincoins to non-bouncelable address above and then upload smart contract using Tonlib ```java -import org.ton.java.tonlib.Tonlib; -Tonlib tonlib=Tonlib.builder().build(); - String base64boc=Utils.bytesToBase64(msg.message.toBoc(false)); - log.info(base64boc); - tonlib.sendRawMessage(base64boc); +Tonlib tonlib =Tonlib.builder().build(); +String base64boc = Utils.bytesToBase64(msg.message.toBoc(false)); +log.info(base64boc); +tonlib.sendRawMessage(base64boc); ``` Check if contract was deployed successfully ```java -Tonlib tonlib=Tonlib.builder().build(); +Tonlib tonlib = Tonlib.builder().build(); - RunResult result=tonlib.runMethod(address,"seqno"); - TvmStackEntryNumber seqno=(TvmStackEntryNumber)result.getStackEntry(); - log.info("seqno: {}",seqno.getNumber()); +RunResult result = tonlib.runMethod(address,"seqno"); +TvmStackEntryNumber seqno = (TvmStackEntryNumber) result.getStackEntry(); +log.info("seqno: {}", seqno.getNumber()); - result=tonlib.runMethod(address,"get_x_data"); - TvmStackEntryNumber x_data=(TvmStackEntryNumber)result.getStackEntry(); - log.info("x_data: {}",seqno.getNumber()); +result=tonlib.runMethod(address,"get_x_data"); +TvmStackEntryNumber x_data = (TvmStackEntryNumber) result.getStackEntry(); +log.info("x_data: {}", seqno.getNumber()); - result=tonlib.runMethod(address,"get_extra_field"); - TvmStackEntryNumber extra_field=(TvmStackEntryNumber)result.getStackEntry(); - log.info("extra_field: {}",seqno.getNumber()); +result=tonlib.runMethod(address,"get_extra_field"); +TvmStackEntryNumber extra_field = (TvmStackEntryNumber) result.getStackEntry(); +log.info("extra_field: {}", seqno.getNumber()); // result - seqno:1 - x_data:2 - extra_field:4 +seqno:1 +x_data:2 +extra_field:4 ``` Transfer Toncoins ```java -Address destinationAddress=Address.of("kf_sPxv06KagKaRmOOKxeDQwApCx3i8IQOwv507XD51JOLka"); - BigInteger amount=Utils.toNano(2); //2 Toncoins or 2bln nano-toncoins - long seqNumber=1; - ExternalMessage extMsg=customContract.createTransferMessage(keyPair.getSecretKey(),destinationAddress,amount,seqNumber); - String base64bocExtMsg=Utils.bytesToBase64(extMsg.message.toBoc(false)); - tonlib.sendRawMessage(base64bocExtMsg); +Address destinationAddress = Address.of("kf_sPxv06KagKaRmOOKxeDQwApCx3i8IQOwv507XD51JOLka"); +BigInteger amount = Utils.toNano(2); //2 Toncoins or 2bln nano-toncoins +long seqNumber = 1; +ExternalMessage extMsg = customContract.createTransferMessage(keyPair.getSecretKey(),destinationAddress,amount,seqNumber); +String base64bocExtMsg = Utils.bytesToBase64(extMsg.message.toBoc(false)); +tonlib.sendRawMessage(base64bocExtMsg); ``` [maven-central-svg]: https://img.shields.io/maven-central/v/org.ton.java/smartcontract diff --git a/utils/README.md b/utils/README.md index 971db13b..427f9e18 100644 --- a/utils/README.md +++ b/utils/README.md @@ -38,24 +38,24 @@ You can use Utils methods to generate new key pair or import an existing private ```java // generate new key kair -TweetNaclFast.Signature.KeyPair keyPair=Utils.generateSignatureKeyPair(); +TweetNaclFast.Signature.KeyPair keyPair = Utils.generateSignatureKeyPair(); ``` ```java // import existing private key -byte[]secretKey=Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F2C1D960A1F1D65E1E4"); - TweetNaclFast.Signature.KeyPair keyPair=Utils.generateSignatureKeyPairFromSeed(secretKey); +byte[] secretKey = Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F2C1D960A1F1D65E1E4"); +TweetNaclFast.Signature.KeyPair keyPair=Utils.generateSignatureKeyPairFromSeed(secretKey); - byte[]pubKey=keyPair.getPublicKey(); - byte[]secKey=keyPair.getSecretKey(); +byte[] pubKey = keyPair.getPublicKey(); +byte[] secKey = keyPair.getSecretKey(); - String msg="ABC"; - String msgHashSha256=Utils.sha256(msg); +String msg = "ABC"; +String msgHashSha256 = Utils.sha256(msg); - TweetNacl.Signature sig=new TweetNacl.Signature(pubKey,secKey); +TweetNacl.Signature sig = new TweetNacl.Signature(pubKey,secKey); // sign msg with signature - byte[]signed=sig.detached(Utils.hexToBytes(msgHashSha256)); +byte[] signed = sig.detached(Utils.hexToBytes(msgHashSha256)); ``` ## Other helpful methods @@ -63,9 +63,9 @@ byte[]secretKey=Utils.hexToBytes("F182111193F30D79D517F2339A1BA7C25FDF6C52142F0F ```java static String base64ToBitString(String base64) -static byte[]base64ToBytes(String base64) +static byte[] base64ToBytes(String base64) -static byte[]base64SafeUrlToBytes(String base64) +static byte[] base64SafeUrlToBytes(String base64) static String base64ToHexString(String base64) @@ -89,41 +89,41 @@ static byte[]concatBytes(byte[]a,byte[]b) static com.iwebpp.crypto.TweetNaclFast.Box.KeyPair generateKeyPair() -static com.iwebpp.crypto.TweetNaclFast.Box.KeyPair generatePairFromSecretKey(byte[]secretKey) +static com.iwebpp.crypto.TweetNaclFast.Box.KeyPair generatePairFromSecretKey(byte[] secretKey) static com.iwebpp.crypto.TweetNaclFast.Signature.KeyPair generateSignatureKeyPair() -static com.iwebpp.crypto.TweetNaclFast.Signature.KeyPair generateSignatureKeyPairFromSeed(byte[]secretKey) +static com.iwebpp.crypto.TweetNaclFast.Signature.KeyPair generateSignatureKeyPairFromSeed(byte[] secretKey) -static byte[]getCRC16ChecksumAsBytes(byte[]bytes) +static byte[]getCRC16ChecksumAsBytes(byte[] bytes) -static String getCRC16ChecksumAsHex(byte[]bytes) +static String getCRC16ChecksumAsHex(byte[] bytes) -static int getCRC16ChecksumAsInt(byte[]bytes) +static int getCRC16ChecksumAsInt(byte[] bytes) -static byte[]getCRC32ChecksumAsBytes(byte[]bytes) +static byte[]getCRC32ChecksumAsBytes(byte[] bytes) -static byte[]getCRC32ChecksumAsBytesReversed(byte[]bytes) +static byte[]getCRC32ChecksumAsBytesReversed(byte[] bytes) -static String getCRC32ChecksumAsHex(byte[]bytes) +static String getCRC32ChecksumAsHex(byte[] bytes) -static Long getCRC32ChecksumAsLong(byte[]bytes) //uses POLY 0x1EDC6F41 +static Long getCRC32ChecksumAsLong(byte[] bytes) //uses POLY 0x1EDC6F41 static Utils.OS getOS() -static String getSafeString(String originalResult,String processResult,String template) +static String getSafeString(String originalResult, String processResult,String template) -static com.iwebpp.crypto.TweetNaclFast.Signature getSignature(byte[]pubKey,byte[]prvKey) +static com.iwebpp.crypto.TweetNaclFast.Signature getSignature(byte[] pubKey,byte[] prvKey) static String hexStringToBase64(String hex) static String hexStringToBase64UrlSafe(String hex) -static byte[]hexToBytes(String hex) +static byte[] hexToBytes(String hex) -static byte[]intToByteArray(int value) +static byte[] intToByteArray(int value) -static int readNBytesFromArray(int n,byte[]ui8array) +static int readNBytesFromArray(int n, byte[] ui8array) static String sha256(byte[]bytes) @@ -139,7 +139,7 @@ static String streamToString(InputStream is) static String formatNanoValue(String value) // returns formatted, e.g. 100,451.515633556 -static String formatNanoValue(String value,int scale) // rounds to scale, e.g. 100,451.52 +static String formatNanoValue(String value, int scale) // rounds to scale, e.g. 100,451.52 static BigInteger toNano(BigDecimal toncoins) @@ -153,7 +153,7 @@ static BigInteger toNano(long toncoins) static BigDecimal fromNano(long toncoins) -static BigDecimal fromNano(long toncoins,int scale) +static BigDecimal fromNano(long toncoins, int scale) ``` [maven-central-svg]: https://img.shields.io/maven-central/v/org.ton.java/utils