TRON API
TronApp クラスは、TRON
のアドレス取得、トランザクション署名、メッセージ署名を提供します。Ethereum
と同じ INS コードを使用しますが、BIP44 パスの
coin_type=195' によりファームウェアが TRON
モードで動作します。
Ethereum との主な違い
| 項目 |
Ethereum |
TRON |
| アドレス形式 |
0x... (hex) |
T... (Base58Check) |
| TX ハッシュ |
Keccak-256 |
SHA-256 |
| 署名レスポンス |
V(1) + R(32) + S(32) |
R(32) + S(32) + V(1) |
| V の値 |
27 or 28 |
0 or 1 (recovery ID) |
インポート
import {
TronApp,
DEFAULT_TRON_PATH,
type SignatureResult,
} from '@openloop/sdk-core'
初期化
const tron = new TronApp(transport)
メソッド一覧
| メソッド |
説明 |
getAddress(path) |
TRON アドレス(T...)と公開鍵を取得 |
signTransaction(path, rawDataHex) |
トランザクション raw_data に署名 |
signMessage(path, message) |
メッセージに署名 |
getAddress
デバイスから TRON アドレス(Base58Check 形式
T...)を取得します。
async getAddress(path: string): Promise<{
publicKey: string // 非圧縮公開鍵 (hex, 65 bytes)
address: string // Base58Check アドレス ("T...")
}>
パラメータ
| 名前 |
型 |
説明 |
path |
string |
BIP44 パス(例: "44'/195'/0'/0/0") |
デフォルトパス
DEFAULT_TRON_PATH = "44'/195'/0'/0/0"
使用例
const { address } = await tron.getAddress(DEFAULT_TRON_PATH)
// address: "TLsV62...txnV1Fm"
signTransaction
TRON トランザクションの raw_data に署名します。
async signTransaction(
path: string,
rawDataHex: string
): Promise<SignatureResult>
パラメータ
| 名前 |
型 |
説明 |
path |
string |
BIP44 パス |
rawDataHex |
string |
protobuf エンコード済み raw_data の
hex(0x あり/なし) |
返り値:
SignatureResult
| フィールド |
型 |
説明 |
v |
number |
リカバリー ID (0 or 1) |
r |
string |
R 値 (32 bytes hex) |
s |
string |
S 値 (32 bytes hex) |
使用例
const sig = await tron.signTransaction(
DEFAULT_TRON_PATH,
'a1b2c3d4...' // raw_data hex
)
signMessage
TRON メッセージに署名します。ファームウェアが BIP44 パスの
coin_type=195' を見て TRON
用のメッセージフォーマットを適用します。
async signMessage(
path: string,
message: string
): Promise<SignatureResult>
パラメータ
| 名前 |
型 |
説明 |
path |
string |
BIP44 パス |
message |
string |
UTF-8 メッセージ文字列 |
使用例
const sig = await tron.signMessage(DEFAULT_TRON_PATH, 'Hello, TRON!')
TronWeb 連携
import TronWeb from 'tronweb'
import { TronApp, DEFAULT_TRON_PATH } from '@openloop/sdk-core'
import { WebHidTransport } from '@openloop/transport-webhid'
const transport = await WebHidTransport.connect()
const tron = new TronApp(transport)
// アドレス取得
const { address } = await tron.getAddress(DEFAULT_TRON_PATH)
// TronWeb でトランザクション作成
const tronWeb = new TronWeb({ fullHost: 'https://api.trongrid.io' })
const transaction = await tronWeb.transactionBuilder.sendTrx(
'TLsV62...', // 送信先
1000000, // 1 TRX = 1,000,000 SUN
address
)
// raw_data を hex に変換してデバイスで署名
const rawDataHex = Buffer.from(
JSON.stringify(transaction.raw_data)
).toString('hex')
const sig = await tron.signTransaction(DEFAULT_TRON_PATH, rawDataHex)
// 署名をトランザクションに追加
const signedTx = {
...transaction,
signature: [`${sig.r}${sig.s}0${sig.v}`],
}
// ブロードキャスト
const result = await tronWeb.trx.sendRawTransaction(signedTx)
次のステップ