@keystonehq/hw-app-tron 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +5 -0
- package/lib/index.d.ts +47 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +117 -0
- package/lib/index.js.map +1 -0
- package/package.json +28 -0
- package/src/index.ts +81 -0
- package/tsconfig.json +9 -0
package/jest.config.js
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Base from '@keystonehq/hw-app-base';
|
|
2
|
+
import { TransportHID } from '@keystonehq/hw-transport-usb';
|
|
3
|
+
import { UR } from '@ngraveio/bc-ur';
|
|
4
|
+
/**
|
|
5
|
+
* Tron Hardware Wallet Application.
|
|
6
|
+
* Extends the Base class to reuse common USB transport logic.
|
|
7
|
+
*/
|
|
8
|
+
export default class Tron extends Base {
|
|
9
|
+
/**
|
|
10
|
+
* Constructs a new instance of the Tron class.
|
|
11
|
+
*
|
|
12
|
+
* @param transport - An object of type TransportHID for USB communication.
|
|
13
|
+
* @param mfp - Optional parameter for the master fingerprint of the wallet.
|
|
14
|
+
*/
|
|
15
|
+
constructor(transport: TransportHID, mfp?: string);
|
|
16
|
+
/**
|
|
17
|
+
* Signs a Tron transaction using the specified derivation path.
|
|
18
|
+
*/
|
|
19
|
+
signTransactionBuffer(path: string, rawTx: Buffer, xfp: string, origin: string): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* signs a Tron transaction given the raw transaction data in hexadecimal format.
|
|
22
|
+
* @param path
|
|
23
|
+
* @param rawTx
|
|
24
|
+
* @param xfp
|
|
25
|
+
* @param origin
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
signTransaction(path: string, rawTx: string, xfp: string, origin: string): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Constructs a UR-encoded request for Tron signing.
|
|
31
|
+
*
|
|
32
|
+
* @param txBuffer - The transaction data to be signed.
|
|
33
|
+
* @param path - The derivation path.
|
|
34
|
+
* @param mfp - The master fingerprint of the wallet.
|
|
35
|
+
* @param origin - The origin of the signing request, used for user confirmation on the device.
|
|
36
|
+
* @returns A string representing the UR-encoded request in uppercase.
|
|
37
|
+
*/
|
|
38
|
+
generateSignRequest(txBuffer: Buffer, path: string, mfp: string, origin: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Parses the UR response from the device to extract the Tron signature.
|
|
41
|
+
*
|
|
42
|
+
* @param ur - The result UR from the device.
|
|
43
|
+
* @returns The signature string in hexadecimal format.
|
|
44
|
+
*/
|
|
45
|
+
parseSignature(ur: UR): string;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,IAA0B,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAW,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAErE,OAAO,EAAa,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,IAAK,SAAQ,IAAI;IAClC;;;;;OAKG;gBACS,SAAS,EAAE,YAAY,EAAE,GAAG,CAAC,EAAE,MAAM;IAIjD;;OAEG;IACG,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAStG;;;;;;;OAOG;IACG,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIhG;;;;;;;;OAQG;IACI,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAe/F;;;;;OAKG;IACI,cAAc,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM;CAIxC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
const uuid = __importStar(require("uuid"));
|
|
46
|
+
const hw_app_base_1 = __importStar(require("@keystonehq/hw-app-base"));
|
|
47
|
+
const hw_transport_usb_1 = require("@keystonehq/hw-transport-usb");
|
|
48
|
+
const bc_ur_registry_tron_1 = require("@keystonehq/bc-ur-registry-tron");
|
|
49
|
+
const bc_ur_1 = require("@ngraveio/bc-ur");
|
|
50
|
+
/**
|
|
51
|
+
* Tron Hardware Wallet Application.
|
|
52
|
+
* Extends the Base class to reuse common USB transport logic.
|
|
53
|
+
*/
|
|
54
|
+
class Tron extends hw_app_base_1.default {
|
|
55
|
+
/**
|
|
56
|
+
* Constructs a new instance of the Tron class.
|
|
57
|
+
*
|
|
58
|
+
* @param transport - An object of type TransportHID for USB communication.
|
|
59
|
+
* @param mfp - Optional parameter for the master fingerprint of the wallet.
|
|
60
|
+
*/
|
|
61
|
+
constructor(transport, mfp) {
|
|
62
|
+
super(transport, mfp);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Signs a Tron transaction using the specified derivation path.
|
|
66
|
+
*/
|
|
67
|
+
signTransactionBuffer(path, rawTx, xfp, origin) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
this.precheck();
|
|
70
|
+
const encodedUR = this.generateSignRequest(rawTx, path, xfp || this.mfp, origin);
|
|
71
|
+
const response = yield this.sendToDevice(hw_transport_usb_1.Actions.CMD_RESOLVE_UR, encodedUR);
|
|
72
|
+
const resultUR = (0, hw_app_base_1.parseResponoseUR)(response.payload);
|
|
73
|
+
return this.parseSignature(resultUR);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* signs a Tron transaction given the raw transaction data in hexadecimal format.
|
|
78
|
+
* @param path
|
|
79
|
+
* @param rawTx
|
|
80
|
+
* @param xfp
|
|
81
|
+
* @param origin
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
signTransaction(path, rawTx, xfp, origin) {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
return yield this.signTransactionBuffer(path, Buffer.from(rawTx, 'hex'), xfp, origin);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Constructs a UR-encoded request for Tron signing.
|
|
91
|
+
*
|
|
92
|
+
* @param txBuffer - The transaction data to be signed.
|
|
93
|
+
* @param path - The derivation path.
|
|
94
|
+
* @param mfp - The master fingerprint of the wallet.
|
|
95
|
+
* @param origin - The origin of the signing request, used for user confirmation on the device.
|
|
96
|
+
* @returns A string representing the UR-encoded request in uppercase.
|
|
97
|
+
*/
|
|
98
|
+
generateSignRequest(txBuffer, path, mfp, origin) {
|
|
99
|
+
const requestId = uuid.v4();
|
|
100
|
+
const tronUR = bc_ur_registry_tron_1.TronSignRequest.constructTronRequest(txBuffer, path, mfp, requestId, undefined, origin);
|
|
101
|
+
const ur = tronUR.toUR();
|
|
102
|
+
return new bc_ur_1.UREncoder(ur, Infinity).nextPart().toUpperCase();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Parses the UR response from the device to extract the Tron signature.
|
|
106
|
+
*
|
|
107
|
+
* @param ur - The result UR from the device.
|
|
108
|
+
* @returns The signature string in hexadecimal format.
|
|
109
|
+
*/
|
|
110
|
+
parseSignature(ur) {
|
|
111
|
+
const signature = bc_ur_registry_tron_1.TronSignature.fromCBOR(ur.cbor);
|
|
112
|
+
return signature.getSignature().toString('hex');
|
|
113
|
+
}
|
|
114
|
+
;
|
|
115
|
+
}
|
|
116
|
+
exports.default = Tron;
|
|
117
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAC7B,uEAAiE;AACjE,mEAAqE;AACrE,yEAAiF;AACjF,2CAAgD;AAEhD;;;GAGG;AACH,MAAqB,IAAK,SAAQ,qBAAI;IAClC;;;;;OAKG;IACH,YAAY,SAAuB,EAAE,GAAY;QAC7C,KAAK,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED;;OAEG;IACG,qBAAqB,CAAC,IAAY,EAAE,KAAa,EAAE,GAAW,EAAE,MAAc;;YAChF,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,MAAM,SAAS,GAAG,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,IAAI,CAAC,GAAI,EAAE,MAAM,CAAC,CAAC;YAClF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,0BAAO,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;YAC5E,MAAM,QAAQ,GAAG,IAAA,8BAAgB,EAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YAEpD,OAAO,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QACzC,CAAC;KAAA;IAED;;;;;;;OAOG;IACG,eAAe,CAAC,IAAY,EAAE,KAAa,EAAE,GAAW,EAAE,MAAc;;YAC1E,OAAO,MAAM,IAAI,CAAC,qBAAqB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;QAC1F,CAAC;KAAA;IAED;;;;;;;;OAQG;IACI,mBAAmB,CAAC,QAAgB,EAAE,IAAY,EAAE,GAAW,EAAE,MAAc;QAClF,MAAM,SAAS,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,qCAAe,CAAC,oBAAoB,CAC/C,QAAQ,EACR,IAAI,EACJ,GAAG,EACH,SAAS,EACT,SAAS,EACT,MAAM,CACT,CAAC;QAEF,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;QACzB,OAAO,IAAI,iBAAS,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,CAAC;IAChE,CAAC;IAED;;;;;OAKG;IACI,cAAc,CAAC,EAAM;QACxB,MAAM,SAAS,GAAG,mCAAa,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;QAClD,OAAO,SAAS,CAAC,YAAY,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACpD,CAAC;IAAA,CAAC;CACL;AArED,uBAqEC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@keystonehq/hw-app-tron",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"types": "lib/index.d.ts",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@keystonehq/bc-ur-registry": "0.7.0",
|
|
8
|
+
"@keystonehq/bc-ur-registry-tron": "0.4.3",
|
|
9
|
+
"@ngraveio/bc-ur": "^1.1.6",
|
|
10
|
+
"@noble/hashes": "^1.5.0",
|
|
11
|
+
"bech32": "^2.0.0",
|
|
12
|
+
"uuid": "^8.3.2",
|
|
13
|
+
"@keystonehq/hw-app-base": "^0.1.7",
|
|
14
|
+
"@keystonehq/hw-transport-usb": "^0.2.0",
|
|
15
|
+
"@keystonehq/hw-transport-error": "^0.0.2"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/jest": "^27.5.2",
|
|
19
|
+
"@types/node": "^16.18.52",
|
|
20
|
+
"eslint": "^8.49.0",
|
|
21
|
+
"jest": "^27.4.3",
|
|
22
|
+
"typescript": "^5.2.2"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc --project tsconfig.json",
|
|
26
|
+
"test": "jest"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as uuid from 'uuid';
|
|
2
|
+
import Base, { parseResponoseUR } from '@keystonehq/hw-app-base';
|
|
3
|
+
import { Actions, TransportHID } from '@keystonehq/hw-transport-usb';
|
|
4
|
+
import { TronSignRequest, TronSignature } from '@keystonehq/bc-ur-registry-tron';
|
|
5
|
+
import { UREncoder, UR } from '@ngraveio/bc-ur';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tron Hardware Wallet Application.
|
|
9
|
+
* Extends the Base class to reuse common USB transport logic.
|
|
10
|
+
*/
|
|
11
|
+
export default class Tron extends Base {
|
|
12
|
+
/**
|
|
13
|
+
* Constructs a new instance of the Tron class.
|
|
14
|
+
*
|
|
15
|
+
* @param transport - An object of type TransportHID for USB communication.
|
|
16
|
+
* @param mfp - Optional parameter for the master fingerprint of the wallet.
|
|
17
|
+
*/
|
|
18
|
+
constructor(transport: TransportHID, mfp?: string) {
|
|
19
|
+
super(transport, mfp);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Signs a Tron transaction using the specified derivation path.
|
|
24
|
+
*/
|
|
25
|
+
async signTransactionBuffer(path: string, rawTx: Buffer, xfp: string, origin: string): Promise<string> {
|
|
26
|
+
this.precheck();
|
|
27
|
+
const encodedUR = this.generateSignRequest(rawTx, path, xfp || this.mfp!, origin);
|
|
28
|
+
const response = await this.sendToDevice(Actions.CMD_RESOLVE_UR, encodedUR);
|
|
29
|
+
const resultUR = parseResponoseUR(response.payload);
|
|
30
|
+
|
|
31
|
+
return this.parseSignature(resultUR);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* signs a Tron transaction given the raw transaction data in hexadecimal format.
|
|
36
|
+
* @param path
|
|
37
|
+
* @param rawTx
|
|
38
|
+
* @param xfp
|
|
39
|
+
* @param origin
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
async signTransaction(path: string, rawTx: string, xfp: string, origin: string): Promise<string> {
|
|
43
|
+
return await this.signTransactionBuffer(path, Buffer.from(rawTx, 'hex'), xfp, origin);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Constructs a UR-encoded request for Tron signing.
|
|
48
|
+
*
|
|
49
|
+
* @param txBuffer - The transaction data to be signed.
|
|
50
|
+
* @param path - The derivation path.
|
|
51
|
+
* @param mfp - The master fingerprint of the wallet.
|
|
52
|
+
* @param origin - The origin of the signing request, used for user confirmation on the device.
|
|
53
|
+
* @returns A string representing the UR-encoded request in uppercase.
|
|
54
|
+
*/
|
|
55
|
+
public generateSignRequest(txBuffer: Buffer, path: string, mfp: string, origin: string): string {
|
|
56
|
+
const requestId = uuid.v4();
|
|
57
|
+
const tronUR = TronSignRequest.constructTronRequest(
|
|
58
|
+
txBuffer,
|
|
59
|
+
path,
|
|
60
|
+
mfp,
|
|
61
|
+
requestId,
|
|
62
|
+
undefined,
|
|
63
|
+
origin
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const ur = tronUR.toUR();
|
|
67
|
+
return new UREncoder(ur, Infinity).nextPart().toUpperCase();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Parses the UR response from the device to extract the Tron signature.
|
|
72
|
+
*
|
|
73
|
+
* @param ur - The result UR from the device.
|
|
74
|
+
* @returns The signature string in hexadecimal format.
|
|
75
|
+
*/
|
|
76
|
+
public parseSignature(ur: UR): string {
|
|
77
|
+
const signature = TronSignature.fromCBOR(ur.cbor);
|
|
78
|
+
return signature.getSignature().toString('hex');
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|