Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ledger support for Sui #463

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@axelar-network/interchain-token-service": "2.0.1",
"@cosmjs/cosmwasm-stargate": "^0.32.1",
"@ledgerhq/hw-app-eth": "6.32.2",
"@mysten/ledgerjs-hw-app-sui": "^0.4.1",
"@mysten/sui": "^1.3.0",
"@stellar/stellar-sdk": "^13.0.0",
"axios": "^1.7.2",
Expand Down
2 changes: 2 additions & 0 deletions sui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ example for adding multisig info to chains config:
}
```

*Note: To sign via ledger replace private-key with 'ledger' keyword in env and update key scheme to ed25519, as it is the only signatureScheme supported by Ledger currently for Sui.

## Contract Interactions

### Call Contract
Expand Down
80 changes: 80 additions & 0 deletions sui/utils/LedgerSigner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

const { toSerializedSignature, messageWithIntent } = require('@mysten/sui/cryptography');
const { Signer } = require('@mysten/sui/cryptography');
const { Ed25519PublicKey } = require('@mysten/sui/keypairs/ed25519');
const TransportNodeHid = require('@ledgerhq/hw-transport-node-hid').default;
const Sui = require('@mysten/ledgerjs-hw-app-sui').default;

class LedgerSigner extends Signer {
constructor(path) {
super();
this.path = "44'/784'/0'/0'/0'";
this.sui = null;
}

async init() {
try {
if (!this.sui) {
this.sui = await this.getSuiTransport();
await this.sui.getPublicKey(this.path);
}

return this;
} catch (error) {
if (error.message.includes('cannot open device')) {
throw new Error(
'Cannot connect to Ledger device. Please ensure:\n' +
'1. Ledger device is connected\n' +
'2. Sui app is open on the device\n' +
'3. Device is unlocked\n' +
'4. No other applications are using the device',
);
}

throw error;
}
}

async getPublicKey() {
if (!this.sui) {
await this.init();
}

return await this.sui.getPublicKey(this.path);
}

async toSuiAddress() {
if (!this.sui) {
await this.init();
}

return `0x${(await this.sui.getPublicKey(this.path)).address.toString('hex')}`;
}

async signTransaction(bytes) {
if (!this.sui) {
await this.init();
}

const ledgerPublicKey = await this.getPublicKey();
const publicKey = new Ed25519PublicKey(ledgerPublicKey.publicKey);
const msgWithIntent = messageWithIntent('TransactionData', bytes);

return {
signature: toSerializedSignature({
...(await this.sui.signTransaction(this.path, msgWithIntent)),
signatureScheme: 'ED25519',
publicKey,
}),
};
}

async getSuiTransport() {
return new Sui(await TransportNodeHid.create());
}
}

module.exports = {
LedgerSigner,
};
1 change: 1 addition & 0 deletions sui/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ module.exports = {
...require('./utils'),
...require('./upgrade-utils'),
...require('./cli-utils'),
...require('./LedgerSigner'),
};
35 changes: 22 additions & 13 deletions sui/utils/sign-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { fromB64, fromHEX } = require('@mysten/bcs');
const { execute } = require('@axelar-network/axelar-cgp-sui');
const { printInfo } = require('../../common/utils');
const { ethers } = require('hardhat');
const { LedgerSigner } = require('./LedgerSigner');
const {
utils: { hexlify },
} = ethers;
Expand Down Expand Up @@ -40,6 +41,13 @@ function getWallet(chain, options) {
}
}

const client = getSuiClient(chain, options.rpc);

if (options.privateKey === 'ledger') {
blockchainguyy marked this conversation as resolved.
Show resolved Hide resolved
keypair = new LedgerSigner();
return [keypair, client];
}

switch (options.privateKeyType) {
case 'bech32': {
const decodedKey = decodeSuiPrivateKey(options.privateKey);
Expand All @@ -64,8 +72,6 @@ function getWallet(chain, options) {
}
}

const client = getSuiClient(chain, options.rpc);

return [keypair, client];
}

Expand All @@ -75,10 +81,18 @@ function getSuiClient(chain, rpc) {
}

async function printWalletInfo(wallet, client, chain, options = {}) {
const owner =
wallet instanceof Ed25519Keypair || wallet instanceof Secp256k1Keypair || wallet instanceof Secp256r1Keypair
? wallet.toSuiAddress()
: wallet;
let owner;

if (options.privateKey !== 'ledger') {
owner =
wallet instanceof Ed25519Keypair || wallet instanceof Secp256k1Keypair || wallet instanceof Secp256r1Keypair
? wallet.toSuiAddress()
: wallet;
} else {
owner = await wallet.toSuiAddress();
printInfo('PublicKey', (await wallet.getPublicKey()).address.toString('base64'));
}

printInfo('Wallet address', owner);

if (!options.offline) {
Expand Down Expand Up @@ -157,15 +171,10 @@ async function broadcastSignature(client, txBytes, signature, actionName) {

async function signTransactionBlockBytes(keypair, client, txBytes, options) {
const serializedSignature = (await keypair.signTransaction(txBytes)).signature;
let publicKey;

try {
publicKey = await verifyTransactionSignature(txBytes, serializedSignature);
} catch {
throw new Error(`Cannot verify tx signature`);
}
const publicKey = await verifyTransactionSignature(txBytes, serializedSignature);

if (publicKey.toSuiAddress() !== keypair.toSuiAddress()) {
if (publicKey.toSuiAddress() !== (await keypair.toSuiAddress())) {
npty marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Verification failed for address ${keypair.toSuiAddress()}`);
}

Expand Down
32 changes: 32 additions & 0 deletions sui/wallet-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const { saveConfig, loadConfig, getChainConfig } = require('../common/utils');
const { getWallet, addBaseOptions, printWalletInfo } = require('./utils');
const { Command } = require('commander');

async function processCommand(config, chain, options) {
const [keypair, client] = getWallet(chain, options);
await printWalletInfo(keypair, client, chain, options);
}

async function mainProcessor(options, processor) {
const config = loadConfig(options.env);
const chain = getChainConfig(config, options.chainName);
await processor(config, chain, options);
saveConfig(config, options.env);
}

if (require.main === module) {
const program = new Command();

program
.name('getPublicKey')
.description('Query the public key and sui address for the ledger')
.action((options) => {
mainProcessor(options, processCommand);
});

addBaseOptions(program);

program.parse();
}
Loading