@ledgerhq/hw-app-kaspa 1.3.0-nightly.0
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/.prettierrc +4 -0
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE.txt +21 -0
- package/README.md +105 -0
- package/jest.config.ts +10 -0
- package/lib/Kaspa.d.ts +59 -0
- package/lib/Kaspa.d.ts.map +1 -0
- package/lib/Kaspa.js +185 -0
- package/lib/Kaspa.js.map +1 -0
- package/lib/base32.d.ts +11 -0
- package/lib/base32.d.ts.map +1 -0
- package/lib/base32.js +36 -0
- package/lib/base32.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +13 -0
- package/lib/index.js.map +1 -0
- package/lib/kaspa-util.d.ts +3 -0
- package/lib/kaspa-util.d.ts.map +1 -0
- package/lib/kaspa-util.js +102 -0
- package/lib/kaspa-util.js.map +1 -0
- package/lib/kaspaHwTransaction.d.ts +89 -0
- package/lib/kaspaHwTransaction.d.ts.map +1 -0
- package/lib/kaspaHwTransaction.js +157 -0
- package/lib/kaspaHwTransaction.js.map +1 -0
- package/lib-es/Kaspa.d.ts +59 -0
- package/lib-es/Kaspa.d.ts.map +1 -0
- package/lib-es/Kaspa.js +179 -0
- package/lib-es/Kaspa.js.map +1 -0
- package/lib-es/base32.d.ts +11 -0
- package/lib-es/base32.d.ts.map +1 -0
- package/lib-es/base32.js +33 -0
- package/lib-es/base32.js.map +1 -0
- package/lib-es/index.d.ts +4 -0
- package/lib-es/index.d.ts.map +1 -0
- package/lib-es/index.js +4 -0
- package/lib-es/index.js.map +1 -0
- package/lib-es/kaspa-util.d.ts +3 -0
- package/lib-es/kaspa-util.d.ts.map +1 -0
- package/lib-es/kaspa-util.js +95 -0
- package/lib-es/kaspa-util.js.map +1 -0
- package/lib-es/kaspaHwTransaction.d.ts +89 -0
- package/lib-es/kaspaHwTransaction.d.ts.map +1 -0
- package/lib-es/kaspaHwTransaction.js +150 -0
- package/lib-es/kaspaHwTransaction.js.map +1 -0
- package/package.json +43 -0
- package/src/Kaspa.ts +261 -0
- package/src/base32.ts +36 -0
- package/src/index.ts +9 -0
- package/src/kaspa-util.ts +107 -0
- package/src/kaspaHwTransaction.ts +244 -0
- package/tests/kaspa.test.ts +724 -0
- package/tsconfig.json +25 -0
package/.prettierrc
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @ledgerhq/hw-app-kaspa
|
|
2
|
+
|
|
3
|
+
## 1.3.0-nightly.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#11672](https://github.com/LedgerHQ/ledger-live/pull/11672) [`d56bebe`](https://github.com/LedgerHQ/ledger-live/commit/d56bebe672a1ed825697b371662dbff19dcc63d8) Thanks [@semeano](https://github.com/semeano)! - Kaspa commons libs
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Ledger https://www.ledger.com/
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<img src="https://user-images.githubusercontent.com/4631227/191834116-59cf590e-25cc-4956-ae5c-812ea464f324.png" height="100" />
|
|
2
|
+
|
|
3
|
+
## coderofstuff/hw-app-kaspa
|
|
4
|
+
|
|
5
|
+
Ledger Hardware Wallet Kaspa JavaScript bindings.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## API
|
|
9
|
+
|
|
10
|
+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
11
|
+
|
|
12
|
+
#### Table of Contents
|
|
13
|
+
|
|
14
|
+
* [Kaspa](#kaspa)
|
|
15
|
+
* [Parameters](#parameters)
|
|
16
|
+
* [Examples](#examples)
|
|
17
|
+
* [getPublicKey](#getaddress)
|
|
18
|
+
* [Parameters](#parameters-1)
|
|
19
|
+
* [Examples](#examples-1)
|
|
20
|
+
* [signTransaction](#signtransaction)
|
|
21
|
+
* [Parameters](#parameters-2)
|
|
22
|
+
* [Examples](#examples-2)
|
|
23
|
+
|
|
24
|
+
### Kaspa
|
|
25
|
+
|
|
26
|
+
Kaspa API
|
|
27
|
+
|
|
28
|
+
#### Parameters
|
|
29
|
+
|
|
30
|
+
* `transport` **Transport** a transport for sending commands to a device
|
|
31
|
+
|
|
32
|
+
#### Examples
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import Kaspa from "hw-app-kaspa";
|
|
36
|
+
const kaspa = new Kaspa(transport);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### getPublicKey
|
|
40
|
+
|
|
41
|
+
Get Kaspa Public Key for a BIP32 path.
|
|
42
|
+
|
|
43
|
+
##### Parameters
|
|
44
|
+
|
|
45
|
+
* `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** a BIP32 path
|
|
46
|
+
* `display` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** flag to show display (optional, default `false`)
|
|
47
|
+
|
|
48
|
+
##### Examples
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
kaspa.getPublicKey("44'/111111'/0'")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Buffer](https://nodejs.org/api/buffer.html)>** the public key buffer with chain code
|
|
55
|
+
|
|
56
|
+
#### signTransaction
|
|
57
|
+
|
|
58
|
+
Sign a Kaspa transaction.
|
|
59
|
+
|
|
60
|
+
##### Parameters
|
|
61
|
+
|
|
62
|
+
* `transaction` **Transaction** from `src/transaction.js`
|
|
63
|
+
|
|
64
|
+
##### Examples
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import Kaspa from 'hw-app-kaspa';
|
|
68
|
+
import { TransactionInput, TransactionOutput, Transaction } from 'hw-app-kaspa';
|
|
69
|
+
|
|
70
|
+
...
|
|
71
|
+
|
|
72
|
+
const kaspa = new Kaspa(transport);
|
|
73
|
+
|
|
74
|
+
const txin = new TransactionInput({
|
|
75
|
+
prevTxId: "40b022362f1a303518e2b49f86f87a317c87b514ca0f3d08ad2e7cf49d08cc70",
|
|
76
|
+
value: 1100000,
|
|
77
|
+
addressType: 0,
|
|
78
|
+
addressIndex: 0,
|
|
79
|
+
outpointIndex: 0,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const txout = new TransactionOutput({
|
|
83
|
+
value: 1000000,
|
|
84
|
+
scriptPublicKey: "2011a7215f668e921013eb7aac9b7e64b9ec6e757c1b648e89388c919f676aa88cac",
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// By convention, the second output MUST be the change address
|
|
88
|
+
// It MUST set both addressType and addressIndex
|
|
89
|
+
const txoutchange = new TransactionOutput({
|
|
90
|
+
value: 90000,
|
|
91
|
+
scriptPublicKey: "2011a7215f668e921013eb7aac9b7e64b9ec6e757c1b648e89388c919f676aa88cac",
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const tx = new Transaction({
|
|
95
|
+
version: 0,
|
|
96
|
+
changeAddressType: 0,
|
|
97
|
+
changeAddressIndex: 0,
|
|
98
|
+
inputs: [txin],
|
|
99
|
+
outputs: [txout, txoutchange],
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
kaspa.signTransaction(tx);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Updates the transaction by filling in the `signature` property of each `TransactionInput` in the `Transaction` object.
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
preset: "ts-jest",
|
|
3
|
+
testEnvironment: "node",
|
|
4
|
+
testRegex: ".test.ts$",
|
|
5
|
+
collectCoverage: true,
|
|
6
|
+
testPathIgnorePatterns: ["packages/*/lib-es", "packages/*/lib"],
|
|
7
|
+
coveragePathIgnorePatterns: ["packages/create-dapp"],
|
|
8
|
+
passWithNoTests: true,
|
|
9
|
+
rootDir: __dirname,
|
|
10
|
+
};
|
package/lib/Kaspa.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import Transport from "@ledgerhq/hw-transport";
|
|
3
|
+
import { KaspaHwTransaction } from "./kaspaHwTransaction";
|
|
4
|
+
export default class Kaspa {
|
|
5
|
+
transport: Transport;
|
|
6
|
+
constructor(transport: Transport);
|
|
7
|
+
/**
|
|
8
|
+
* Get Kaspa address (public key) for a BIP32 path.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} path a BIP32 path
|
|
11
|
+
* @param {boolean} display flag to show display
|
|
12
|
+
* @return an object with the address field
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* kaspa.getAddress("44'/111111'/0'").then(r => r.address)
|
|
16
|
+
*/
|
|
17
|
+
getAddress(path: string, display?: boolean): Promise<{
|
|
18
|
+
publicKey: string;
|
|
19
|
+
address: string;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Sign a Kaspa transaction. Applies the signatures into the input objects
|
|
23
|
+
*
|
|
24
|
+
* @param {KaspaHwTransaction} transaction - the Transaction object
|
|
25
|
+
*
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* kaspa.signTransaction(transaction)
|
|
29
|
+
*/
|
|
30
|
+
signTransaction(transaction: KaspaHwTransaction): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Sign personal message on the device
|
|
33
|
+
* @param {String} message - the personal message string to sign. Max 120 len for Nano S, 200 len for others
|
|
34
|
+
* @param {0|1} addressType
|
|
35
|
+
* @param {number} addressIndex
|
|
36
|
+
*
|
|
37
|
+
* @returns {Buffer} application config object
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* kaspa.signMessage(message).then(r => r.version)
|
|
41
|
+
*/
|
|
42
|
+
signMessage(message: string, addressType?: 0 | 1, addressIndex?: number, account?: number): Promise<{
|
|
43
|
+
signature: string;
|
|
44
|
+
messageHash: string;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* Get application configuration.
|
|
48
|
+
*
|
|
49
|
+
* @returns {Buffer} application config object
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* kaspa.getVersion().then(r => r.version)
|
|
53
|
+
*/
|
|
54
|
+
getVersion(): Promise<{
|
|
55
|
+
version: string;
|
|
56
|
+
}>;
|
|
57
|
+
sendToDevice(instruction: any, p1: any, payload?: Buffer, p2?: number): Promise<Buffer>;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=Kaspa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Kaspa.d.ts","sourceRoot":"","sources":["../src/Kaspa.ts"],"names":[],"mappings":";AAAA,OAAO,SAAS,MAAM,wBAAwB,CAAC;AAI/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAwC1D,MAAM,CAAC,OAAO,OAAO,KAAK;IACxB,SAAS,EAAE,SAAS,CAAC;gBAET,SAAS,EAAE,SAAS;IAShC;;;;;;;;;OASG;IACG,UAAU,CACd,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,OAAe,GACvB,OAAO,CAAC;QACT,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IAiBF;;;;;;;;OAQG;IACG,eAAe,CAAC,WAAW,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IA8DrE;;;;;;;;;;OAUG;IACG,WAAW,CACf,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,CAAC,GAAG,CAAC,EACnB,YAAY,CAAC,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE,MAAM;;;;IAsDlB;;;;;;;OAOG;IACG,UAAU;;;IASV,YAAY,CAAC,WAAW,KAAA,EAAE,EAAE,KAAA,EAAE,OAAO,SAAkB,EAAE,EAAE,SAAU;CAc5E"}
|
package/lib/Kaspa.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const errors_1 = require("@ledgerhq/errors");
|
|
16
|
+
const kaspa_util_1 = require("./kaspa-util");
|
|
17
|
+
const bip32_path_1 = __importDefault(require("bip32-path"));
|
|
18
|
+
// Get Address
|
|
19
|
+
const P1_NON_CONFIRM = 0x00;
|
|
20
|
+
const P1_CONFIRM = 0x01;
|
|
21
|
+
// Sign Transaction
|
|
22
|
+
const P1_HEADER = 0x00;
|
|
23
|
+
const P1_OUTPUTS = 0x01;
|
|
24
|
+
const P1_INPUTS = 0x02;
|
|
25
|
+
const P1_NEXT_SIGNATURE = 0x03;
|
|
26
|
+
const P2_LAST = 0x00;
|
|
27
|
+
const P2_MORE = 0x80;
|
|
28
|
+
const LEDGER_CLA = 0xe0;
|
|
29
|
+
const INS = {
|
|
30
|
+
GET_VERSION: 0x04,
|
|
31
|
+
GET_ADDRESS: 0x05,
|
|
32
|
+
SIGN_TX: 0x06,
|
|
33
|
+
SIGN_MESSAGE: 0x07,
|
|
34
|
+
};
|
|
35
|
+
function pathToBuffer(originalPath) {
|
|
36
|
+
const pathNums = bip32_path_1.default.fromString(originalPath).toPathArray();
|
|
37
|
+
return serializePath(pathNums);
|
|
38
|
+
}
|
|
39
|
+
function serializePath(path) {
|
|
40
|
+
const buf = Buffer.alloc(1 + path.length * 4);
|
|
41
|
+
buf.writeUInt8(path.length, 0);
|
|
42
|
+
for (const [i, num] of path.entries()) {
|
|
43
|
+
buf.writeUInt32BE(num, 1 + i * 4);
|
|
44
|
+
}
|
|
45
|
+
return buf;
|
|
46
|
+
}
|
|
47
|
+
class Kaspa {
|
|
48
|
+
constructor(transport) {
|
|
49
|
+
this.transport = transport;
|
|
50
|
+
this.transport.decorateAppAPIMethods(this, ["getVersion", "getAddress", "signTransaction", "signMessage"], "kaspa");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get Kaspa address (public key) for a BIP32 path.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} path a BIP32 path
|
|
56
|
+
* @param {boolean} display flag to show display
|
|
57
|
+
* @return an object with the address field
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* kaspa.getAddress("44'/111111'/0'").then(r => r.address)
|
|
61
|
+
*/
|
|
62
|
+
getAddress(path_1) {
|
|
63
|
+
return __awaiter(this, arguments, void 0, function* (path, display = false) {
|
|
64
|
+
const pathBuffer = pathToBuffer(path);
|
|
65
|
+
const p1 = display ? P1_CONFIRM : P1_NON_CONFIRM;
|
|
66
|
+
const publicKeyBuffer = yield this.sendToDevice(INS.GET_ADDRESS, p1, pathBuffer);
|
|
67
|
+
return {
|
|
68
|
+
publicKey: publicKeyBuffer.toString("hex"),
|
|
69
|
+
address: (0, kaspa_util_1.publicKeyToAddress)(publicKeyBuffer.subarray(2, 34)),
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Sign a Kaspa transaction. Applies the signatures into the input objects
|
|
75
|
+
*
|
|
76
|
+
* @param {KaspaHwTransaction} transaction - the Transaction object
|
|
77
|
+
*
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* kaspa.signTransaction(transaction)
|
|
81
|
+
*/
|
|
82
|
+
signTransaction(transaction) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const header = transaction.serialize();
|
|
85
|
+
yield this.sendToDevice(INS.SIGN_TX, P1_HEADER, header, P2_MORE);
|
|
86
|
+
for (const output of transaction.outputs) {
|
|
87
|
+
yield this.sendToDevice(INS.SIGN_TX, P1_OUTPUTS, output.serialize(), P2_MORE);
|
|
88
|
+
}
|
|
89
|
+
let signatureBuffer = null;
|
|
90
|
+
for (let i = 0; i < transaction.inputs.length; i++) {
|
|
91
|
+
const p2 = i >= transaction.inputs.length - 1 ? P2_LAST : P2_MORE;
|
|
92
|
+
const input = transaction.inputs[i];
|
|
93
|
+
signatureBuffer = yield this.sendToDevice(INS.SIGN_TX, P1_INPUTS, input.serialize(), p2);
|
|
94
|
+
}
|
|
95
|
+
while (signatureBuffer) {
|
|
96
|
+
const [hasMore, inputIndex, sigLen, ...signatureAndSighash] = signatureBuffer;
|
|
97
|
+
const sigBuf = signatureAndSighash.slice(0, sigLen);
|
|
98
|
+
const sighashLen = signatureAndSighash[64];
|
|
99
|
+
const sighashBuf = signatureAndSighash.slice(65, 65 + sighashLen);
|
|
100
|
+
if (sigLen != 64) {
|
|
101
|
+
throw new Error(`Expected signature length is 64. Received ${sigLen} for input ${inputIndex}`);
|
|
102
|
+
}
|
|
103
|
+
if (sighashLen != 32) {
|
|
104
|
+
throw new Error(`Expected sighash length is 32. Received ${sighashLen} for input ${inputIndex}`);
|
|
105
|
+
}
|
|
106
|
+
transaction.inputs[inputIndex].setSignature(Buffer.from(sigBuf).toString("hex"));
|
|
107
|
+
transaction.inputs[inputIndex].setSighash(Buffer.from(sighashBuf).toString("hex"));
|
|
108
|
+
// Keep going as long as hasMore is true-ish
|
|
109
|
+
if (!hasMore) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
signatureBuffer = yield this.sendToDevice(INS.SIGN_TX, P1_NEXT_SIGNATURE);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Sign personal message on the device
|
|
118
|
+
* @param {String} message - the personal message string to sign. Max 120 len for Nano S, 200 len for others
|
|
119
|
+
* @param {0|1} addressType
|
|
120
|
+
* @param {number} addressIndex
|
|
121
|
+
*
|
|
122
|
+
* @returns {Buffer} application config object
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* kaspa.signMessage(message).then(r => r.version)
|
|
126
|
+
*/
|
|
127
|
+
signMessage(message, addressType, addressIndex, account) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
account = account !== null && account !== void 0 ? account : 0x80000000;
|
|
130
|
+
addressIndex = addressIndex !== null && addressIndex !== void 0 ? addressIndex : 0;
|
|
131
|
+
addressType = addressType !== null && addressType !== void 0 ? addressType : 0;
|
|
132
|
+
if (account < 0x80000000 || account > 0xffffffff) {
|
|
133
|
+
throw new Error("Account must be between 0x80000000 and 0xFFFFFFFF");
|
|
134
|
+
}
|
|
135
|
+
if (addressIndex < 0 || addressIndex > 0xffffffff) {
|
|
136
|
+
throw new Error("Address index must be an integer in range [0, 0xFFFFFFFF]");
|
|
137
|
+
}
|
|
138
|
+
const addressTypeBuf = Buffer.alloc(1);
|
|
139
|
+
addressTypeBuf.writeUInt8(addressType || 0);
|
|
140
|
+
const addressIndexBuf = Buffer.alloc(4);
|
|
141
|
+
addressIndexBuf.writeUInt32BE(addressIndex || 0);
|
|
142
|
+
const accountBuf = Buffer.alloc(4);
|
|
143
|
+
accountBuf.writeUInt32BE(account);
|
|
144
|
+
const messageBuffer = Buffer.from(message);
|
|
145
|
+
const messageLenBuf = Buffer.alloc(1);
|
|
146
|
+
messageLenBuf.writeUInt8(messageBuffer.length);
|
|
147
|
+
const payload = Buffer.concat([
|
|
148
|
+
addressTypeBuf,
|
|
149
|
+
addressIndexBuf,
|
|
150
|
+
accountBuf,
|
|
151
|
+
messageLenBuf,
|
|
152
|
+
messageBuffer,
|
|
153
|
+
]);
|
|
154
|
+
const signatureBuffer = yield this.sendToDevice(INS.SIGN_MESSAGE, P1_NON_CONFIRM, payload);
|
|
155
|
+
const [sigLen, ...signatureAndMessageHash] = signatureBuffer;
|
|
156
|
+
const signature = Buffer.from(signatureAndMessageHash.slice(0, sigLen)).toString("hex");
|
|
157
|
+
const messageHashLen = signatureAndMessageHash[64];
|
|
158
|
+
const messageHash = Buffer.from(signatureAndMessageHash.slice(65, 65 + messageHashLen)).toString("hex");
|
|
159
|
+
return { signature, messageHash };
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Get application configuration.
|
|
164
|
+
*
|
|
165
|
+
* @returns {Buffer} application config object
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* kaspa.getVersion().then(r => r.version)
|
|
169
|
+
*/
|
|
170
|
+
getVersion() {
|
|
171
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
172
|
+
const [major, minor, patch] = yield this.sendToDevice(INS.GET_VERSION, P1_NON_CONFIRM);
|
|
173
|
+
return { version: `${major}.${minor}.${patch}` };
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
sendToDevice(instruction_1, p1_1) {
|
|
177
|
+
return __awaiter(this, arguments, void 0, function* (instruction, p1, payload = Buffer.alloc(0), p2 = P2_LAST) {
|
|
178
|
+
const acceptStatusList = [errors_1.StatusCodes.OK];
|
|
179
|
+
const reply = yield this.transport.send(LEDGER_CLA, instruction, p1, p2, payload, acceptStatusList);
|
|
180
|
+
return reply.subarray(0, reply.length - 2);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
exports.default = Kaspa;
|
|
185
|
+
//# sourceMappingURL=Kaspa.js.map
|
package/lib/Kaspa.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Kaspa.js","sourceRoot":"","sources":["../src/Kaspa.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,6CAA+C;AAE/C,6CAAkD;AAGlD,4DAAmC;AAEnC,cAAc;AACd,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB,mBAAmB;AACnB,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,MAAM,iBAAiB,GAAG,IAAI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|
|
|
1
|
+
/***
|
|
2
|
+
* Encodes the given array of 5-bit integers as a base32-encoded string.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array} data Array of integers between 0 and 31 inclusive.
|
|
5
|
+
*/
|
|
6
|
+
export declare function encode(data: Array<number>): string;
|
|
7
|
+
declare const base32: {
|
|
8
|
+
encode: typeof encode;
|
|
9
|
+
};
|
|
10
|
+
export default base32;
|
|
11
|
+
//# sourceMappingURL=base32.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base32.d.ts","sourceRoot":"","sources":["../src/base32.ts"],"names":[],"mappings":"AAcA;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,UAUzC;AAED,QAAA,MAAM,MAAM;;CAEX,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
package/lib/base32.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encode = void 0;
|
|
4
|
+
/***
|
|
5
|
+
* https://github.com/bitcoincashjs/cashaddr
|
|
6
|
+
* Copyright (c) 2018 Matias Alejo Garcia
|
|
7
|
+
* Copyright (c) 2017 Emilio Almansi
|
|
8
|
+
* Distributed under the MIT software license, see the accompanying
|
|
9
|
+
* file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
10
|
+
*/
|
|
11
|
+
/***
|
|
12
|
+
* Charset containing the 32 symbols used in the base32 encoding.
|
|
13
|
+
*/
|
|
14
|
+
const CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
|
15
|
+
/***
|
|
16
|
+
* Encodes the given array of 5-bit integers as a base32-encoded string.
|
|
17
|
+
*
|
|
18
|
+
* @param {Array} data Array of integers between 0 and 31 inclusive.
|
|
19
|
+
*/
|
|
20
|
+
function encode(data) {
|
|
21
|
+
let base32 = "";
|
|
22
|
+
for (let i = 0; i < data.length; i++) {
|
|
23
|
+
const value = data[i];
|
|
24
|
+
if (!(0 <= value && value < 32)) {
|
|
25
|
+
throw new Error("value " + value);
|
|
26
|
+
}
|
|
27
|
+
base32 += CHARSET[value];
|
|
28
|
+
}
|
|
29
|
+
return base32;
|
|
30
|
+
}
|
|
31
|
+
exports.encode = encode;
|
|
32
|
+
const base32 = {
|
|
33
|
+
encode,
|
|
34
|
+
};
|
|
35
|
+
exports.default = base32;
|
|
36
|
+
//# sourceMappingURL=base32.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base32.js","sourceRoot":"","sources":["../src/base32.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AACb;;;;;;GAMG;AAEH;;GAEG;AACH,MAAM,OAAO,GAAG,kCAAkC,CAAC;AAEnD;;;;GAIG;AACH,SAAgB,MAAM,CAAC,IAAmB;IACxC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,KAAK,GAAG,EAAE,CAAC,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CAAC,QAAQ,GAAG,KAAK,CAAC,CAAC;QACpC,CAAC;QACD,MAAM,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAVD,wBAUC;AAED,MAAM,MAAM,GAAG;IACb,MAAM;CACP,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAC;AAE5B,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAE9B,eAAe,KAAK,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.KaspaHwTransaction = exports.TransactionOutput = exports.TransactionInput = void 0;
|
|
7
|
+
const Kaspa_1 = __importDefault(require("./Kaspa"));
|
|
8
|
+
var kaspaHwTransaction_1 = require("./kaspaHwTransaction");
|
|
9
|
+
Object.defineProperty(exports, "TransactionInput", { enumerable: true, get: function () { return kaspaHwTransaction_1.TransactionInput; } });
|
|
10
|
+
Object.defineProperty(exports, "TransactionOutput", { enumerable: true, get: function () { return kaspaHwTransaction_1.TransactionOutput; } });
|
|
11
|
+
Object.defineProperty(exports, "KaspaHwTransaction", { enumerable: true, get: function () { return kaspaHwTransaction_1.KaspaHwTransaction; } });
|
|
12
|
+
exports.default = Kaspa_1.default;
|
|
13
|
+
//# 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,oDAA4B;AAE5B,2DAI8B;AAH5B,sHAAA,gBAAgB,OAAA;AAChB,uHAAA,iBAAiB,OAAA;AACjB,wHAAA,kBAAkB,OAAA;AAGpB,kBAAe,eAAK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kaspa-util.d.ts","sourceRoot":"","sources":["../src/kaspa-util.ts"],"names":[],"mappings":";AAiGA,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAS7D"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.publicKeyToAddress = void 0;
|
|
7
|
+
const base32_1 = __importDefault(require("./base32"));
|
|
8
|
+
function convertBits(data, from, to, strict = false) {
|
|
9
|
+
strict = strict || false;
|
|
10
|
+
let accumulator = 0;
|
|
11
|
+
let bits = 0;
|
|
12
|
+
const result = [];
|
|
13
|
+
const mask = (1 << to) - 1;
|
|
14
|
+
for (let i = 0; i < data.length; i++) {
|
|
15
|
+
const value = data[i];
|
|
16
|
+
if (value < 0 || value >> from !== 0) {
|
|
17
|
+
throw new Error(`Invalid argument: value = ${value}`);
|
|
18
|
+
}
|
|
19
|
+
accumulator = (accumulator << from) | value;
|
|
20
|
+
bits += from;
|
|
21
|
+
while (bits >= to) {
|
|
22
|
+
bits -= to;
|
|
23
|
+
result.push((accumulator >> bits) & mask);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!strict) {
|
|
27
|
+
if (bits > 0) {
|
|
28
|
+
result.push((accumulator << (to - bits)) & mask);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (!(bits >= from || (accumulator << (to - bits)) & mask)) {
|
|
33
|
+
throw new Error("Conversion requires padding but strict mode was used");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
function prefixToArray(prefix) {
|
|
39
|
+
const result = [];
|
|
40
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
41
|
+
const char = prefix.charCodeAt(i);
|
|
42
|
+
result.push(char & 31);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
const GENERATOR1 = [0x98, 0x79, 0xf3, 0xae, 0x1e];
|
|
47
|
+
const GENERATOR2 = [0xf2bc8e61, 0xb76d99e2, 0x3e5fb3c4, 0x2eabe2a8, 0x4f43e470];
|
|
48
|
+
function polymod(data) {
|
|
49
|
+
// Treat c as 8 bits + 32 bits
|
|
50
|
+
let c0 = 0, c1 = 1, C = 0;
|
|
51
|
+
for (let j = 0; j < data.length; j++) {
|
|
52
|
+
// Set C to c shifted by 35
|
|
53
|
+
C = c0 >>> 3;
|
|
54
|
+
// 0x[07]ffffffff
|
|
55
|
+
c0 &= 0x07;
|
|
56
|
+
// Shift as a whole number
|
|
57
|
+
c0 <<= 5;
|
|
58
|
+
c0 |= c1 >>> 27;
|
|
59
|
+
// 0xffffffff >>> 5
|
|
60
|
+
c1 &= 0x07ffffff;
|
|
61
|
+
c1 <<= 5;
|
|
62
|
+
// xor the last 5 bits
|
|
63
|
+
c1 ^= data[j];
|
|
64
|
+
for (let i = 0; i < GENERATOR1.length; ++i) {
|
|
65
|
+
if (C & (1 << i)) {
|
|
66
|
+
c0 ^= GENERATOR1[i];
|
|
67
|
+
c1 ^= GENERATOR2[i];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
c1 ^= 1;
|
|
72
|
+
// Negative numbers -> large positive numbers
|
|
73
|
+
if (c1 < 0) {
|
|
74
|
+
c1 ^= 1 << 31;
|
|
75
|
+
c1 += (1 << 30) * 2;
|
|
76
|
+
}
|
|
77
|
+
// Unless bitwise operations are used,
|
|
78
|
+
// numbers are consisting of 52 bits, except
|
|
79
|
+
// the sign bit. The result is max 40 bits,
|
|
80
|
+
// so it fits perfectly in one number!
|
|
81
|
+
return c0 * (1 << 30) * 4 + c1;
|
|
82
|
+
}
|
|
83
|
+
function checksumToArray(checksum) {
|
|
84
|
+
const result = [];
|
|
85
|
+
for (let i = 0; i < 8; ++i) {
|
|
86
|
+
result.push(checksum & 31);
|
|
87
|
+
checksum /= 32;
|
|
88
|
+
}
|
|
89
|
+
return result.reverse();
|
|
90
|
+
}
|
|
91
|
+
function publicKeyToAddress(hashBuffer) {
|
|
92
|
+
const eight0 = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
93
|
+
const prefixData = prefixToArray("kaspa").concat([0]);
|
|
94
|
+
const versionByte = 0;
|
|
95
|
+
const arr = Array.prototype.slice.call(hashBuffer, 0);
|
|
96
|
+
const payloadData = convertBits([versionByte].concat(arr), 8, 5);
|
|
97
|
+
const checksumData = prefixData.concat(payloadData).concat(eight0);
|
|
98
|
+
const payload = payloadData.concat(checksumToArray(polymod(checksumData)));
|
|
99
|
+
return "kaspa:" + base32_1.default.encode(payload);
|
|
100
|
+
}
|
|
101
|
+
exports.publicKeyToAddress = publicKeyToAddress;
|
|
102
|
+
//# sourceMappingURL=kaspa-util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kaspa-util.js","sourceRoot":"","sources":["../src/kaspa-util.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAE9B,SAAS,WAAW,CAClB,IAAc,EACd,IAAY,EACZ,EAAU,EACV,SAAkB,KAAK;IAEvB,MAAM,GAAG,MAAM,IAAI,KAAK,CAAC;IACzB,IAAI,WAAW,GAAG,CAAC,CAAC;IACpB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;IAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,6BAA6B,KAAK,EAAE,CAAC,CAAC;QACxD,CAAC;QAED,WAAW,GAAG,CAAC,WAAW,IAAI,IAAI,CAAC,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