Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Added address encoding to algorand app #667

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ This repository hosts the `@ledgerhq/*` JavaScript libraries.

> NB. for the 3 points above, the best is to have integration of Prettier,
> ESlint, Flowtype in your text editor (there are plugin for most editors).

## Documentation

* [documentation.js](https://documentation.js.org/) for generation markdown documentations of libraries. You can run `yarn doc`.
2 changes: 1 addition & 1 deletion packages/hw-app-algorand/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ get Algorajt address for a given BIP 32 path.
cosmos.getAddress("44'/60'/0'/0/0").then(o => o.address)
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{publicKey: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), address: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?}>** an object with a publicKey, address and (optionally) chainCode
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{publicKey: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), address: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>** an object with a publicKey, address and (optionally) chainCode
5 changes: 4 additions & 1 deletion packages/hw-app-algorand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"dependencies": {
"@ledgerhq/errors": "^6.2.0",
"@ledgerhq/hw-transport": "^6.7.0",
"bip32-path": "^0.4.2"
"bip32-path": "^0.4.2",
"hi-base32": "^0.5.1",
"js-sha512": "^0.8.0",
"tweetnacl": "^1.0.3"
},
"scripts": {
"clean": "bash ../../script/clean.sh",
Expand Down
8 changes: 6 additions & 2 deletions packages/hw-app-algorand/src/Algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import type Transport from "@ledgerhq/hw-transport";
import BIPPath from "bip32-path";
import { UserRefusedOnDevice } from "@ledgerhq/errors";
import { encodeAddress } from "./utils";
const CHUNK_SIZE = 250;
// const P1_FIRST = 0x00;
const P1_MORE = 0x80;
Expand Down Expand Up @@ -59,7 +60,7 @@ export default class Algorand {
boolDisplay?: boolean
): Promise<{
publicKey: string;
address?: string;
address: string;
}> {
const bipPath = BIPPath.fromString(path).toPathArray();
const buf = Buffer.alloc(4);
Expand All @@ -74,9 +75,12 @@ export default class Algorand {
[SW_OK]
)
.then((response) => {
const publicKey = Buffer.from(response.slice(0, 32)).toString("hex");
const buffer = Buffer.from(response.slice(0, 32));
const publicKey = buffer.toString("hex");
const address = encodeAddress(buffer);
return {
publicKey,
address,
};
});
}
Expand Down
32 changes: 32 additions & 0 deletions packages/hw-app-algorand/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import nacl from "tweetnacl";
import base32 from "hi-base32";
import sha512 from "js-sha512";

const PUBLIC_KEY_LENGTH = nacl.sign.publicKeyLength;
const ALGORAND_ADDRESS_LENGTH = 58;
const ALGORAND_CHECKSUM_BYTE_LENGTH = 4;

export const encodeAddress = (address: Uint8Array): string => {
const checksum = sha512.sha512_256
.array(address)
.slice(
PUBLIC_KEY_LENGTH - ALGORAND_CHECKSUM_BYTE_LENGTH,
PUBLIC_KEY_LENGTH
);
const addr = base32.encode(concatArrays(address, checksum));

return addr.toString().slice(0, ALGORAND_ADDRESS_LENGTH);
};

function concatArrays(...arrs: ArrayLike<number>[]): Uint8Array {
const size = arrs.reduce((sum, arr) => sum + arr.length, 0);
const c = new Uint8Array(size);

let offset = 0;
for (let i = 0; i < arrs.length; i++) {
c.set(arrs[i], offset);
offset += arrs[i].length;
}

return c;
}
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6197,6 +6197,23 @@ he@^1.1.0:
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==

hermes-engine@~0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/hermes-engine/-/hermes-engine-0.5.1.tgz#601115e4b1e0a17d9aa91243b96277de4e926e09"
integrity sha512-hLwqh8dejHayjlpvZY40e1aDCDvyP98cWx/L5DhAjSJLH8g4z9Tp08D7y4+3vErDsncPOdf1bxm+zUWpx0/Fxg==

hermes-profile-transformer@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/hermes-profile-transformer/-/hermes-profile-transformer-0.0.6.tgz#bd0f5ecceda80dd0ddaae443469ab26fb38fc27b"
integrity sha512-cnN7bQUm65UWOy6cbGcCcZ3rpwW8Q/j4OP5aWRhEry4Z2t2aR1cjrbp0BS+KiBN0smvP1caBgAuxutvyvJILzQ==
dependencies:
source-map "^0.7.3"

hi-base32@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/hi-base32/-/hi-base32-0.5.1.tgz#1279f2ddae2673219ea5870c2121d2a33132857e"
integrity sha512-EmBBpvdYh/4XxsnUybsPag6VikPYnN30td+vQk+GI3qpahVEG9+gTkG0aXVxTjBqQ5T6ijbWIu77O+C5WFWsnA==

highlight.js@^10.5.0:
version "10.5.0"
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.5.0.tgz#3f09fede6a865757378f2d9ebdcbc15ba268f98f"
Expand Down Expand Up @@ -7402,6 +7419,11 @@ js-sha3@0.5.7:
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.5.7.tgz#0d4ffd8002d5333aabaf4a23eed2f6374c9f28e7"
integrity sha1-DU/9gALVMzqrr0oj7tL2N0yfKOc=

js-sha512@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha512/-/js-sha512-0.8.0.tgz#dd22db8d02756faccf19f218e3ed61ec8249f7d4"
integrity sha512-PWsmefG6Jkodqt+ePTvBZCSMFgN7Clckjd0O7su3I0+BW2QWUTJNzjktHsztGLhncP2h8mcF9V9Y2Ha59pAViQ==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down