@ledgerhq/hw-app-algorand 6.24.1 → 6.25.1-alpha.3
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/lib-es/Algorand.js.flow +151 -0
- package/package.json +3 -3
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Ledger Node JS API
|
|
3
|
+
* (c) 2017-2018 Ledger
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
********************************************************************************/
|
|
17
|
+
//@flow
|
|
18
|
+
|
|
19
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
20
|
+
import BIPPath from "bip32-path";
|
|
21
|
+
import { UserRefusedOnDevice } from "@ledgerhq/errors";
|
|
22
|
+
const CHUNK_SIZE = 250;
|
|
23
|
+
|
|
24
|
+
// const P1_FIRST = 0x00;
|
|
25
|
+
const P1_MORE = 0x80;
|
|
26
|
+
const P1_WITH_ACCOUNT_ID = 0x01;
|
|
27
|
+
const P2_LAST = 0x00;
|
|
28
|
+
const P2_MORE = 0x80;
|
|
29
|
+
|
|
30
|
+
const SW_OK = 0x9000;
|
|
31
|
+
const SW_CANCEL = 0x6986;
|
|
32
|
+
|
|
33
|
+
const P1_WITH_REQUEST_USER_APPROVAL = 0x80;
|
|
34
|
+
|
|
35
|
+
// algo spec
|
|
36
|
+
const CLA = 0x80;
|
|
37
|
+
const INS_GET_PUBLIC_KEY = 0x03;
|
|
38
|
+
const INS_SIGN_MSGPACK = 0x08;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Cosmos API
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* import Algorand from "@ledgerhq/hw-app-algorand";
|
|
45
|
+
* const algo = new Algorand(transport)
|
|
46
|
+
*/
|
|
47
|
+
export default class Algorand {
|
|
48
|
+
transport: Transport<*>;
|
|
49
|
+
|
|
50
|
+
constructor(transport: Transport<*>) {
|
|
51
|
+
this.transport = transport;
|
|
52
|
+
transport.decorateAppAPIMethods(this, ["getAddress", "sign"], "ALGO");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* get Algorajt address for a given BIP 32 path.
|
|
57
|
+
* @param path a path in BIP 32 format
|
|
58
|
+
* @option boolDisplay optionally enable or not the display
|
|
59
|
+
* @return an object with a publicKey, address and (optionally) chainCode
|
|
60
|
+
* @example
|
|
61
|
+
* cosmos.getAddress("44'/60'/0'/0/0").then(o => o.address)
|
|
62
|
+
*/
|
|
63
|
+
getAddress(
|
|
64
|
+
path: string,
|
|
65
|
+
boolDisplay?: boolean
|
|
66
|
+
): Promise<{ publicKey: string, address: string }> {
|
|
67
|
+
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
68
|
+
|
|
69
|
+
const buf = Buffer.alloc(4);
|
|
70
|
+
buf.writeUInt32BE(bipPath[2], 0);
|
|
71
|
+
|
|
72
|
+
return this.transport
|
|
73
|
+
.send(
|
|
74
|
+
CLA,
|
|
75
|
+
INS_GET_PUBLIC_KEY,
|
|
76
|
+
boolDisplay ? P1_WITH_REQUEST_USER_APPROVAL : 0,
|
|
77
|
+
0,
|
|
78
|
+
buf,
|
|
79
|
+
[SW_OK]
|
|
80
|
+
)
|
|
81
|
+
.then((response) => {
|
|
82
|
+
const publicKey = Buffer.from(response.slice(0, 32)).toString("hex");
|
|
83
|
+
return {
|
|
84
|
+
publicKey,
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
foreach<T, A>(arr: T[], callback: (T, number) => Promise<A>): Promise<A[]> {
|
|
90
|
+
function iterate(index, array, result) {
|
|
91
|
+
if (index >= array.length) {
|
|
92
|
+
return result;
|
|
93
|
+
} else
|
|
94
|
+
return callback(array[index], index).then(function (res) {
|
|
95
|
+
result.push(res);
|
|
96
|
+
return iterate(index + 1, array, result);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return Promise.resolve().then(() => iterate(0, arr, []));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async sign(
|
|
103
|
+
path: string,
|
|
104
|
+
message: string
|
|
105
|
+
): Promise<{ signature: null | Buffer }> {
|
|
106
|
+
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
107
|
+
|
|
108
|
+
const buf = Buffer.alloc(4);
|
|
109
|
+
buf.writeUInt32BE(bipPath[2], 0);
|
|
110
|
+
|
|
111
|
+
const chunks = [];
|
|
112
|
+
const buffer = Buffer.concat([buf, Buffer.from(message, "hex")]);
|
|
113
|
+
|
|
114
|
+
for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
|
|
115
|
+
let end = i + CHUNK_SIZE;
|
|
116
|
+
if (i > buffer.length) {
|
|
117
|
+
end = buffer.length;
|
|
118
|
+
}
|
|
119
|
+
chunks.push(buffer.slice(i, end));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let response = {};
|
|
123
|
+
|
|
124
|
+
return this.foreach(chunks, (data, j) =>
|
|
125
|
+
this.transport
|
|
126
|
+
.send(
|
|
127
|
+
CLA,
|
|
128
|
+
INS_SIGN_MSGPACK,
|
|
129
|
+
j === 0 ? P1_WITH_ACCOUNT_ID : P1_MORE,
|
|
130
|
+
j + 1 === chunks.length ? P2_LAST : P2_MORE,
|
|
131
|
+
data,
|
|
132
|
+
[SW_OK, SW_CANCEL]
|
|
133
|
+
)
|
|
134
|
+
.then((apduResponse) => (response = apduResponse))
|
|
135
|
+
).then(() => {
|
|
136
|
+
const errorCodeData = response;
|
|
137
|
+
if (errorCodeData === 0x6986) {
|
|
138
|
+
throw new UserRefusedOnDevice();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let signature = null;
|
|
142
|
+
if (response.length > 0) {
|
|
143
|
+
signature = response.slice(0, response.length);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
signature: signature,
|
|
148
|
+
};
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ledgerhq/hw-app-algorand",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.25.1-alpha.3+eb669e17",
|
|
4
4
|
"description": "Ledger Hardware Wallet Algorand Application API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ledger",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@ledgerhq/errors": "^6.10.0",
|
|
31
|
-
"@ledgerhq/hw-transport": "^6.
|
|
31
|
+
"@ledgerhq/hw-transport": "^6.25.1-alpha.3+eb669e17",
|
|
32
32
|
"bip32-path": "^0.4.2",
|
|
33
33
|
"hi-base32": "^0.5.1",
|
|
34
34
|
"js-sha512": "^0.8.0",
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"watch": "bash ../../script/watch.sh",
|
|
41
41
|
"doc": "bash ../../script/doc.sh"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "eb669e17dd87d3ab568beab1f9a5ddb1a2536e83"
|
|
44
44
|
}
|