-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sign multi-sig transaction with trezor
- Loading branch information
1 parent
06289fb
commit e3d3503
Showing
6 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Address, CredentialType } from '../Address'; | ||
|
||
// TODO: Would like to verify if this is needed before writing test for it | ||
export const isScriptAddress = (address: string): boolean => { | ||
const baseAddress = Address.fromBech32(address).asBase(); | ||
const paymentCredential = baseAddress?.getPaymentCredential(); | ||
const stakeCredential = baseAddress?.getStakeCredential(); | ||
return paymentCredential?.type === CredentialType.ScriptHash && stakeCredential?.type === CredentialType.ScriptHash; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/wallet/test/hardware/trezor/TrezorSharedWalletKeyAgent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import * as Crypto from '@cardano-sdk/crypto'; | ||
import { BaseWallet, createSharedWallet } from '../../../src'; | ||
import { Cardano } from '@cardano-sdk/core'; | ||
import { CommunicationType, KeyPurpose, KeyRole, util } from '@cardano-sdk/key-management'; | ||
import { InitializeTxProps, InitializeTxResult } from '@cardano-sdk/tx-construction'; | ||
import { TrezorKeyAgent } from '@cardano-sdk/hardware-trezor'; | ||
import { dummyLogger as logger } from 'ts-log'; | ||
import { mockProviders as mocks } from '@cardano-sdk/util-dev'; | ||
|
||
describe('TrezorSharedWalletKeyAgent', () => { | ||
let wallet: BaseWallet; | ||
let trezorKeyAgent: TrezorKeyAgent; | ||
let txSubmitProvider: mocks.TxSubmitProviderStub; | ||
|
||
const trezorConfig = { | ||
communicationType: CommunicationType.Node, | ||
manifest: { | ||
appUrl: 'https://your.application.com', | ||
email: 'email@developer.com' | ||
} | ||
}; | ||
|
||
beforeAll(async () => { | ||
txSubmitProvider = mocks.mockTxSubmitProvider(); | ||
trezorKeyAgent = await TrezorKeyAgent.createWithDevice( | ||
{ | ||
chainId: Cardano.ChainIds.Preprod, | ||
purpose: KeyPurpose.MULTI_SIG, | ||
trezorConfig | ||
}, | ||
{ | ||
bip32Ed25519: await Crypto.SodiumBip32Ed25519.create(), | ||
logger | ||
} | ||
); | ||
|
||
const walletPubKey = await trezorKeyAgent.derivePublicKey({ index: 0, role: KeyRole.External }); | ||
const walletKeyHash = trezorKeyAgent.bip32Ed25519.getPubKeyHash(walletPubKey); | ||
|
||
const walletStakePubKey = await trezorKeyAgent.derivePublicKey({ index: 0, role: KeyRole.Stake }); | ||
const walletStakeKeyHash = trezorKeyAgent.bip32Ed25519.getPubKeyHash(walletStakePubKey); | ||
|
||
const paymentScript: Cardano.NativeScript = { | ||
__type: Cardano.ScriptType.Native, | ||
kind: Cardano.NativeScriptKind.RequireAnyOf, | ||
scripts: [ | ||
{ | ||
__type: Cardano.ScriptType.Native, | ||
keyHash: walletKeyHash, | ||
kind: Cardano.NativeScriptKind.RequireSignature | ||
}, | ||
{ | ||
__type: Cardano.ScriptType.Native, | ||
keyHash: Crypto.Ed25519KeyHashHex('b275b08c999097247f7c17e77007c7010cd19f20cc086ad99d398539'), | ||
kind: Cardano.NativeScriptKind.RequireSignature | ||
} | ||
] | ||
}; | ||
|
||
const stakingScript: Cardano.NativeScript = { | ||
__type: Cardano.ScriptType.Native, | ||
kind: Cardano.NativeScriptKind.RequireAnyOf, | ||
scripts: [ | ||
{ | ||
__type: Cardano.ScriptType.Native, | ||
keyHash: walletStakeKeyHash, | ||
kind: Cardano.NativeScriptKind.RequireSignature | ||
}, | ||
{ | ||
__type: Cardano.ScriptType.Native, | ||
keyHash: Crypto.Ed25519KeyHashHex('b275b08c999097247f7c17e77007c7010cd19f20cc086ad99d398539'), | ||
kind: Cardano.NativeScriptKind.RequireSignature | ||
} | ||
] | ||
}; | ||
|
||
wallet = createSharedWallet( | ||
{ name: 'Shared HW Wallet' }, | ||
{ | ||
assetProvider: mocks.mockAssetProvider(), | ||
chainHistoryProvider: mocks.mockChainHistoryProvider(), | ||
logger, | ||
networkInfoProvider: mocks.mockNetworkInfoProvider(), | ||
paymentScript, | ||
rewardAccountInfoProvider: mocks.mockRewardAccountInfoProvider(), | ||
rewardsProvider: mocks.mockRewardsProvider(), | ||
stakingScript, | ||
// txSubmitProvider: mocks.mockTxSubmitProvider(), | ||
txSubmitProvider, | ||
utxoProvider: mocks.mockUtxoProvider(), | ||
witnesser: util.createBip32Ed25519Witnesser(util.createAsyncKeyAgent(trezorKeyAgent)) | ||
} | ||
); | ||
}); | ||
|
||
afterAll(() => wallet.shutdown()); | ||
|
||
describe('Sign Transaction', () => { | ||
let props: InitializeTxProps; | ||
let txInternals: InitializeTxResult; | ||
const simpleOutput = { | ||
address: Cardano.PaymentAddress( | ||
'addr_test1qpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5ewvxwdrt70qlcpeeagscasafhffqsxy36t90ldv06wqrk2qum8x5w' | ||
), | ||
value: { coins: 11_111_111n } | ||
}; | ||
|
||
it('should sign simple multi-sig transaction', async () => { | ||
props = { | ||
outputs: new Set<Cardano.TxOut>([simpleOutput]) | ||
}; | ||
txInternals = await wallet.initializeTx(props); | ||
const witnessedTx = await wallet.finalizeTx({ tx: txInternals }); | ||
expect(witnessedTx.witness.signatures.size).toBe(1); | ||
}); | ||
}); | ||
}); |