Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.
Closed
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
10 changes: 5 additions & 5 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[submodule "popup/trezor-crypto"]
path = popup/trezor-crypto
url = https://github.com/trezor/trezor-crypto.git
branch = master
path = popup/trezor-crypto
url = https://github.com/trezor/trezor-crypto.git
branch = master
[submodule "popup/trezor-common"]
path = popup/trezor-common
url = https://github.com/trezor/trezor-common.git
path = popup/trezor-common
url = https://github.com/trezor/trezor-common.git
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,52 @@ TrezorConnect.ethereumVerifyMessage(path, signature, message, function (result)
```
The message can be UTF-8; however, TREZOR is not displaying non-ascii characters, and third-party apps are not dealing with them correctly either. Therefore, using ASCII only is recommended.

## Sign & Verify Cardano message

`TrezorConnect.adaSignMessage(path, message, callback)` asks device to
sign a message using the private key derived by given BIP32 path. Path can be specified
either as an array of numbers or as string m/A'/B'/C'/D/E'

Message is signed and address + signature is returned

[Example:](examples/signmsg-cardano.html)

```javascript
var path="m/44'/1815'/0'/0'";
var message="Example message";
TrezorConnect.adaSignMessage(path, message, function (result) {
if (result.success) {
console.log('Message signed!', result.signature); // signature in hex
console.log('Signing public key:', result.public_key);
} else {
console.error('Error:', result.error); // error message
}
});
```
The message can be UTF-8; however, TREZOR is not displaying non-ascii characters, and third-party apps are not dealing with them correctly either. Therefore, using ASCII only is recommended.

## Verify Cardano message
`TrezorConnect.adaVerifyMessage(publicKey, signature, message, callback)` asks device to
verify a message using the cardano public key and signature.

Message is verified and success is returned.

[Example:](examples/signmsg-cardano.html)

```javascript
var publicKey="e34e53a1ef72f0493a227e051b0f223fa27aab2ee98af48fc2a8cb29407c2f27";
var signature="ac979a384a5eacebdcb11770d1202ca19791bff5fca95d64eb287d15009cfb4808ed95025ebf1b4abf394fba683ffab85321e588d4982ac99a6ad69017139d00"; // signature in hex
var message="Example message"; // message utf8
TrezorConnect.adaVerifyMessage(publicKey, signature, message, function (result) {
if (result.success) {
console.log(result.success);
} else {
console.error('Error:', result.error); // error message
}
});
```
The message can be UTF-8; however, TREZOR is not displaying non-ascii characters, and third-party apps are not dealing with them correctly either. Therefore, using ASCII only is recommended.

## Symmetric key-value encryption

`TrezorConnect.cipherKeyValue(path, key, value, encrypt, ask_on_encrypt, ask_on_decrypt, callback)` asks device to
Expand Down Expand Up @@ -540,3 +586,65 @@ TrezorConnect.ethereumGetAddress(path, function (response) {
}
});
```

## Cardano - Show address / get address

`TrezorConnect.adaGetAddress(path, show_confirmation, callback)` shows addresses on device and returns it to the caller

[Example:](examples/getaddress.html)

```javascript
var paths = ["m/44'/1815'/0'/0/0'", "m/44'/1815'/0'/0/5'"];

TrezorConnect.adaGetAddress(path, true, function (response) {
console.log("TrezorConnect.adaGetAddress", response);
});
```

## Export Cardano public key

`TrezorConnect.adaGetPublicKey(path, callback)` retrieves xpub key
by path. User is presented with a description of the requested key and asked to
confirm the export.


[Example:](examples/xpubkey-cardano.html)

```javascript
var path = "m/44'/1815'/0'/0/0'";

TrezorConnect.adaGetPublicKey(path, function (result) {
if (result.success) {
console.log('Public key:', result);
} else {
console.error('Error:', result.error);
}
});
```

## Sign Cardano transaction

```javascript
const inputs = [
{prev_hash: "2effff328b76a8113e32a218f7af99e77768289c9201e8d26a9cda0edaf59bfd", address_n: "m/44'/1815'/0'/0/0", prev_index: 0, type: 0}
];
const outputs = [
{address: "2w1sdSJu3GVeNrv8NVHmWNBqK6ssW84An4pExajjdFgXx6k4gksoo6CP1qTwbE34qjKEHZtUKGxY1GMkApUnNEMwGPTgLc7Yghs", amount: 1000000},
{address_n: "m/44'/1815'/0'/0/1", amount: 7120787}
];
const transactions = ["839f8200d81858248258208f088493a600c7d897ef89caeb060e8e8137a1e5aa52e32f6262ec5a087341a6008200d81858248258208f088493a600c7d897ef89caeb060e8e8137a1e5aa52e32f6262ec5a087341a601ff9f8282d818583e83581c3a043fc1baa52fe4df2be89c689c953a52e7c13e51551ef5a3ed1e3da101581a5818360a746c532b81f364ce25168befa6cb2e29d5eccc4883bc001a90ce7a581a007e86118282d818584283581cc52ea4de5aacc58e0fab8b1a55e8bc194c6fd22eebd93fab56cf2789a101581e581c8c44aea4dee0952907690336fa16773c53257ec002dbd219d3970747001a409c205e01ffa0"];

TrezorConnect.adaSignTransaction(
inputs,
outputs,
transactions,
function (response) {
document.getElementById("response").innerHTML = JSON.stringify(response, undefined, 2);
}
);
```

This will return tx_hash and tx_body.

[Cardano sign transaction example:](examples/signtx-cardano.html)

146 changes: 146 additions & 0 deletions connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,105 @@ this.TrezorConnect = (function () {
}, requiredFirmware), callback);
};

/**
* Sign a Cardano message
*
* @param {string|array} path
* @param {string} message to sign (ascii)
* @param {string|function(SignMessageResult)} callback
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.adaSignMessage = function (
path,
message,
callback,
requiredFirmware
) {
if (typeof path === 'string') {
path = parseHDPath(path);
}
if (!callback) {
throw new TypeError('TrezorConnect: callback not found');
}

manager.sendWithChannel(_fwStrFix({
type: 'signadamsg',
path: path,
message: message,
}, requiredFirmware), callback);
};

/**
* Sign a Cardano transaction
*
* @param {array} inputs
* @param {array} outputs
* @param {array} transactions
* @param {string|function(CardanoSignedTransaction)} callback
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.adaSignTransaction = function (
inputs,
outputs,
transactions,
callback,
requiredFirmware
) {
if (!callback) {
throw new TypeError('TrezorConnect: callback not found');
}

inputs = inputs.map(item => {
if (typeof item.address_n === 'string') {
item.address_n = parseHDPath(item.address_n);
}

return item;
});

outputs = outputs.map(item => {
if (item.address_n && typeof item.address_n === 'string') {
item.address_n = parseHDPath(item.address_n);
}
return item;
});

manager.sendWithChannel(_fwStrFix({
type: 'signadatransaction',
inputs,
outputs,
transactions,
}, requiredFirmware), callback);
};

/**
* Get a Cardano public key
*
* @param {string|array} path
* @param {string|function(string)} callback
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.adaGetPublicKey = function (
path,
callback,
requiredFirmware
) {
if (typeof path === 'string') {
path = parseHDPath(path);
}
if (!callback) {
throw new TypeError('TrezorConnect: callback not found');
}

manager.sendWithChannel(_fwStrFix({
type: 'adagetpublickey',
address_n: path,
}, requiredFirmware), callback);
};

/**
* Verify message
*
Expand Down Expand Up @@ -494,6 +593,34 @@ this.TrezorConnect = (function () {
}, requiredFirmware), callback);
};

/**
* Verify cardano message
*
* @param {string} publicKey (string)
* @param {string} signature (base64)
* @param {string} message (string)
* @param {string|function()} callback
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.adaVerifyMessage = function (
publicKey,
signature,
message,
callback,
requiredFirmware
) {
if (!callback) {
throw new TypeError('TrezorConnect: callback not found');
}
manager.sendWithChannel(_fwStrFix({
type: 'verifyadamsg',
public_key: publicKey,
signature: signature,
message: message,
}, requiredFirmware), callback);
};

/**
* Symmetric key-value encryption
*
Expand Down Expand Up @@ -641,6 +768,25 @@ this.TrezorConnect = (function () {
}, requiredFirmware), callback);
}

/**
* Get cardano address
*
* @param {array} path
* @param {?(string|array<number>)} requiredFirmware
*
*/
this.adaGetAddress = function (path, show_display, callback, requiredFirmware) {
if (typeof path === 'string') {
path = parseHDPath(path)
}

manager.sendWithChannel(_fwStrFix({
type: 'adagetaddress',
address_n: path,
show_display
}, requiredFirmware), callback);
}

var LOGIN_CSS =
'<style>@import url("@connect_path@/login_buttons.css")</style>';

Expand Down
17 changes: 17 additions & 0 deletions examples/getaddress.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
});
}

function trezoradaGetAddress() {
var path = document.getElementById('adaaddress').value;

TrezorConnect.adaGetAddress(path, true, function (response) {
console.log("TrezorConnect.adaGetAddress", response);
document.getElementById("response").innerHTML = JSON.stringify(response, undefined, 2);
});
}

function trezorNemGetAddress(network) {
var path = document.getElementById('nemaddress').value;

Expand Down Expand Up @@ -105,6 +114,14 @@ <h3>TrezorConnect.ethereumGetAddress</h3>
<button onclick="trezorEthereumGetAddress()">Ethereum Get Address</button>
</div>

<hr/>

<div>
<h3>TrezorConnect.adaGetAddress</h3>
<input type="text" id="adaaddress" name="adaaddress" value="m/44'/1815'/0'/0/0'" />
<button onclick="trezoradaGetAddress()">Cardano Get Address</button>
</div>

<hr/>

<div>
Expand Down
5 changes: 4 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ <h1>TREZOR Connect Examples</h1>
<ul>
<li><a href="xpubkey.html">Export public key</a></li>
<li><a href="xpubkey-ethereum.html">Export public key - Ethereum</a></li>
<li><a href="xpubkey-cardano.html">Export xpub key - Cardano</a></li>
<li><a href="accountinfo.html">Get account info</a></li>
<li><a href="getaddress.html">Get/show address</a></li> - bitcoin, segwit, ethereum
<li><a href="getaddress.html">Get/show address</a></li> - bitcoin, segwit, ethereum, cardano
</ul>
<ul>
<li><a href="signtx-paytoaddress.html">Sign P2PKH transaction</a></li>
<li><a href="signtx-opreturn.html">Sign OP_RETURN transaction</a></li>
<li><a href="signtx-paytomultisig.html">Sign multisig transaction</a></li>
<li><a href="signtx-paytomultisig-change.html">Sign multisig with change transaction</a></li>
<li><a href="signtx-ethereum.html">Sign Ethereum transaction</a></li>
<li><a href="signtx-cardano.html">Sign Cardano transaction</a></li>
<li><a href="signmsg.html">Sign &amp; Verify message</a></li>
<li><a href="signmsg-ethereum.html">Sign &amp; Verify Ethereum message</a></li>
<li><a href="signmsg-cardano.html">Sign &amp; Verify Cardano message</a></li>
<li><a href="cipherkv.html">Symmetrically encrypt / decrypt value</a> (defined in <a href="https://github.com/satoshilabs/slips/blob/master/slip-0011.md">SLIP-0011</a>)</li>
</ul>
<ul>
Expand Down
Loading