출처 : https://developers.tron.network/v3.0/reference#sign

참고 : https://github.com/tronprotocol/tron-web/issues/104

 

 

index.js

const TronWeb = require('../../dist/TronWeb.node.js');

const fullNode = 'https://api.shasta.trongrid.io';
const solidityNode = 'https://api.shasta.trongrid.io';
const eventServer = 'https://api.shasta.trongrid.io';
const privateKey = '개인키';

const app = async () => {
const tronWeb = new TronWeb(
    fullNode,
    solidityNode,
    eventServer,
    privateKey
);
tronWeb.setDefaultBlock('latest');

const nodes = await tronWeb.isConnected();
const connected = !Object.entries(nodes).map(([name, connected]) => {
    if (!connected)
        console.error(`Error: ${name} is not connected`);
        return connected;
    }).includes(false);

    if (!connected)
        return;
    const sendTransaction = await tronWeb.transactionBuilder.sendTrx('TMDBC4DoS1yQCjBgnPBJWnfTZN9rAKBUHc', 1, 'TS4AYYxrF38EA3fDw92mMWbWdFWRJ4VKih');

    console.group('Unsigned send TRX transaction');
    console.log('- Recipient: TGEJj8eus46QMHPgWQe1FJ2ymBXRm96fn1');
    console.log('- Transaction:\n' + JSON.stringify(sendTransaction, null, 2), '\n');
    console.groupEnd();

};
app(); 

 

트랜젝션 생성은 되는거 같은데

TronLink에서 보면 송금은 안되있다.

 

root@tkpark-VirtualBox:~/tron-web/examples/server# nodejs index.js
Unsigned send TRX transaction
  - Recipient: TGEJj8eus46QMHPgWQe1FJ2ymBXRm96fn1
  - Transaction:
  {
    "txID": "1af7a6cdb907dbd8477f65ecb51bb4c64e874fa5d4ca1639b65985b0125148bc",
    "raw_data": {
      "contract": [
        {
          "parameter": {
            "value": {
              "amount": 1,
              "owner_address": "41b0720a94d13ef83434e1385b543c9291c79a6d14",
              "to_address": "417b4db2232332c2267271d10c55c126fae6f67de1"
            },
            "type_url": "type.googleapis.com/protocol.TransferContract"
          },
          "type": "TransferContract"
        }
      ],
      "ref_block_bytes": "2658",
      "ref_block_hash": "3183fbce05f7e625",
      "expiration": 1545098463000,
      "timestamp": 1545098404591
    }
  }

root@tkpark-VirtualBox:~/tron-web/examples/server#
 

 

이더리움은 개인키로 서명해서  send트렌젝션 한다면

트론은 서버 접속할때 개인키를 하나 들고 있어야되고

서버에서 서명해서 보낼거 같은데 테스텟이 아직 불안해서 그런가 싶기도 하다.

그리고 API 메뉴얼에 "Create an unsigned transaction"으로 되어 있다.

 

참고 사이트 보니 트랜젝션을 만들고 그뒤에 사인을 해서 보낸다.

결국 이더랑 유사하다.

 

테스트넷도 블록 익스프러러가 빨리 만들어져야 될텐데

 

내가 코드를 짜는건지 코드가 나를 짜는건지 모르겠지만 ㅎㅎ

index.js

const TronWeb = require('../../dist/TronWeb.node.js');

const fullNode = 'https://api.shasta.trongrid.io';
const solidityNode = 'https://api.shasta.trongrid.io';
const eventServer = 'https://api.shasta.trongrid.io';
const privateKey = '개인키';

const app = async () => {
const tronWeb = new TronWeb(
    fullNode,
    solidityNode,
    eventServer,
    privateKey
);
tronWeb.setDefaultBlock('latest');

const nodes = await tronWeb.isConnected();
const connected = !Object.entries(nodes).map(([name, connected]) => {
    if (!connected)
        console.error(`Error: ${name} is not connected`);
        return connected;
    }).includes(false);
    sendTransaction = await tronWeb.transactionBuilder.sendTrx('TS4AYYxrF38EA3fDw92mMWbWdFWRJ4VKih', 1);
    sendTransaction.raw_data.data = 'hex message';

    const signedTransaction = await tronWeb.trx.sign(sendTransaction);
    console.group('Signed');
    console.log('- Transaction:\n' + JSON.stringify(signedTransaction, null, 2), '\n');
    console.groupEnd(); // no error {raw_data.data = 'hex_message'} Added

    const sendRaw = await tronWeb.trx.sendRawTransaction(signedTransaction); // error
    console.group('Success');
    console.log('- Transaction:\n' + JSON.stringify(sendRaw, null, 2), '\n');
    console.groupEnd();

};
app(); 

 

먼가 되는거 같다.

트랜젝션 쏘기 전에 이제 Sign부터 넣었다.

트론이 이더리움을 배꼈다면 그렇게 만들수 밖에 없다.

이더리움은  개인키로 전자서명하고 공개키를 같이 node에 보낸다.

그래서 서버단에서 PKI검증한다.

트론도 비슷하지 않을까 싶다.

 

 

root@tkpark-VirtualBox:~/tron-web/examples/server# nodejs index.js
Signed
  - Transaction:
  {
    "txID": "c544fb84a26dd202c9e2fdc85663a4ee41401c005c4d994dc0ad81a84b5

40a4d",
    "raw_data": {
      "contract": [
        {
          "parameter": {
            "value": {
              "amount": 1,
              "owner_address": "417b4db2232332c2267271d10c55c126fae6f67de1",
              "to_address": "41b0720a94d13ef83434e1385b543c9291c79a6d14"
            },
            "type_url": "type.googleapis.com/protocol.TransferContract"
          },
          "type": "TransferContract"
        }
      ],
      "ref_block_bytes": "39d1",
      "ref_block_hash": "59ddb3394b45a28e",
      "expiration": 1545113424000,
      "timestamp": 1545113364738,
      "data": "hex message"
    },
    "signature": [
      "b2dbad9bcc00d2ae2624dd1edcfd80e00e303e7a71a6da7c76127f6030b09

cc47b91e9f81                                                                                             cbe26b1dc40bbbf3bdb333245ffa65d117533219680d041675071e900"
    ]
  }

Success
  - Transaction:
  {
    "code": "OTHER_ERROR",
    "message": "6f74686572206572726f72203a206e756c6c"
  }

root@tkpark-VirtualBox:~/tron-web/examples/server# ~

 

 

먼가 올라간거 같은데,,OTHER_ERROR를 토한다.

주문 오류란다. 식당도 아닌데 말이다 ㅋㅋ

 

TronLink에서 출금된 트렌젝션이 없다.

 

먼가 잘못 되었다는거다.  또 다시 삽질하고 추리하자..

소스 그대로 복붙해서 그런지 저 깃허브 에러메세지랑 유사하다.

밑에 해결책이 있을듯,,,,,

 

순서는

 

Build -> Sign -> Send 순서다.

 

저 위에서 소스에서 빨간색 부분 빼고 보내면 된다.

1개 보낼러면 소수점 6개라서 백만 입력 해야 된다.

 

index.js

const TronWeb = require('../../dist/TronWeb.node.js');

const fullNode = 'https://api.shasta.trongrid.io';
const solidityNode = 'https://api.shasta.trongrid.io';
const eventServer = 'https://api.shasta.trongrid.io';
const privateKey = '개인키';

const app = async () => {
const tronWeb = new TronWeb(
    fullNode,
    solidityNode,
    eventServer,
    privateKey
);
tronWeb.setDefaultBlock('latest');

const nodes = await tronWeb.isConnected();
const connected = !Object.entries(nodes).map(([name, connected]) => {
    if (!connected)
        console.error(`Error: ${name} is not connected`);
        return connected;
    }).includes(false);
    sendTransaction = await tronWeb.transactionBuilder.sendTrx('TS4AYYxrF38EA3fDw92mMWbWdFWRJ4VKih', 1000000);

    const signedTransaction = await tronWeb.trx.sign(sendTransaction);
    console.group('Signed');
    console.log('- Transaction:\n' + JSON.stringify(signedTransaction, null, 2), '\n');
    console.groupEnd(); // no error {raw_data.data = 'hex_message'} Added

    const sendRaw = await tronWeb.trx.sendRawTransaction(signedTransaction); // error
    console.group('Success');
    console.log('- Transaction:\n' + JSON.stringify(sendRaw, null, 2), '\n');
    console.groupEnd();

};
app(); 

 

실행결과

root@tkpark-VirtualBox:~/tron-web/examples/server# nodejs index.js
Signed
  - Transaction:
  {
    "txID": "59c8d68a939b87180da5ddf78c2f03257e0a6ed682711ae5f77588357c138e34",
    "raw_data": {
      "contract": [
        {
          "parameter": {
            "value": {
              "amount": 1000000,
              "owner_address": "417b4db2232332c2267271d10c55c126fae6f67de1",
              "to_address": "41b0720a94d13ef83434e1385b543c9291c79a6d14"
            },
            "type_url": "type.googleapis.com/protocol.TransferContract"
          },
          "type": "TransferContract"
        }
      ],
      "ref_block_bytes": "423f",
      "ref_block_hash": "d68872a44e72ff02",
      "expiration": 1545119898000,
      "timestamp": 1545119838729
    },
    "signature": [
      "568bba26156139fcd20b3128a5748ae32dba482a308d5f1afdcfbf621316c888e

a529b07a30b90961de27393c9e046e6ef54abed7966db0754af611a824108e800"
    ]
  }

Success
  - Transaction:
  {
    "result": true,
    "transaction": {
      "txID": "59c8d68a939b87180da5ddf78c2f03257e0a6ed682711ae5f77588357c13

8e34"                                                                                             ,
      "raw_data": {
        "contract": [
          {
            "parameter": {
              "value": {
                "amount": 1000000,
                "owner_address": "417b4db2232332c2267271d10c55c126fae6f67de1",
                "to_address": "41b0720a94d13ef83434e1385b543c9291c79a6d14"
              },
              "type_url": "type.googleapis.com/protocol.TransferContract"
            },
            "type": "TransferContract"
          }
        ],
        "ref_block_bytes": "423f",
        "ref_block_hash": "d68872a44e72ff02",
        "expiration": 1545119898000,
        "timestamp": 1545119838729
      },
      "signature": [
        "568bba26156139fcd20b3128a5748ae32dba482a308d5f1

afdcfbf621

316c888ea529b07a30b90961de27393c9e046e6ef54abed7966d

b0754af611

a824108e800"
      ]
    }
  }

root@tkpark-VirtualBox:~/tron-web/examples/server#

 

 

 

에러는 이제 안뜸

 

 

생각해보니 송금하는데 스마트컨트렉도 아니고 메세지를 왜 집어 넣었을가?

트론이 트래픽 작고 수수료 싸다고 무시하는건지

이건 암호화폐 메커니즘의 기본이다.

 

저 글 올린 친구 아직 해결했는지 모르겠다.

 

API도 많이 읽었고 공부 많이 했다. 해결도 했고 천천히 다음진도 나가자

저 API가이드에서 강조하는건 절대 web에서 전자서명 하지 말라는것

 

당연한거지만 Private가 유출될수 있다는 위험성이다.

 

 

 

개발하면서 꼭 명심하자...

 

 

 

 

 

 

'트론' 카테고리의 다른 글

tron-web 분해하기4(getTransactionstoAddress)  (0) 2018.12.19
tron-web 분해하기3(getTransactionsFromAddress)  (0) 2018.12.18
tron-web 분해하기1 (tronWeb.createAccount)  (0) 2018.12.17
tron-web 설치  (0) 2018.12.17
tronbox 설치  (1) 2018.12.17
블로그 이미지

iesay

,