Skip to content

Commit f211ed6

Browse files
committed
Cardano: Add vote delegation to node api
Added vote delegation to node api and wasm generation. Signed-off-by: RostarMarek <[email protected]>
1 parent 5f22d90 commit f211ed6

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

CHANGELOG-npm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- btc: add support for regtest
88
- btc: add support for Taproot wallet policies
99
- eth: add method to help clients identify and specify address case (upper/lower/mixed)
10+
- cardano: allow vote delegation
1011

1112
## 0.6.0
1213

sandbox/src/Cardano.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ function CardanoAddress({ bb02 }: Props) {
133133
<ShowError err={err} />
134134
</form>
135135
</div>
136-
137136
);
138137
}
139138

140139
function CardanoSignTransaction({ bb02 }: Props) {
141-
type TxType = 'normal' | 'zero-ttl' | 'tokens' | 'delegate' | 'withdraw-staking-rewards';
140+
type TxType = 'normal' | 'zero-ttl' | 'tokens' | 'delegate' | 'vote-delegation' | 'withdraw-staking-rewards';
142141
const [txType, setTxType] = useState<TxType>('normal');
143142
const [running, setRunning] = useState(false);
144143
const [result, setResult] = useState<bitbox.CardanoSignTransactionResult | undefined>();
@@ -151,6 +150,7 @@ function CardanoSignTransaction({ bb02 }: Props) {
151150
['zero-ttl', 'Transaction with TTL=0'],
152151
['tokens', 'Transaction sending tokens'],
153152
['delegate', 'Delegate staking to a pool'],
153+
['vote-delegation', 'Delegate vote to a dRep'],
154154
['withdraw-staking-rewards', 'Withdraw staking rewards'],
155155
];
156156

@@ -176,6 +176,7 @@ function CardanoSignTransaction({ bb02 }: Props) {
176176
};
177177

178178
const changeAddress = await bb02.cardanoAddress(network, changeConfig, false);
179+
const drepType: bitbox.CardanoDrepType = 'alwaysAbstain';
179180
const transaction = () => {
180181
switch (txType) {
181182
case 'normal':
@@ -290,6 +291,31 @@ function CardanoSignTransaction({ bb02 }: Props) {
290291
validityIntervalStart: BigInt(41110811),
291292
allowZeroTTL: false,
292293
};
294+
case 'vote-delegation':
295+
return {
296+
network,
297+
inputs,
298+
outputs: [
299+
{
300+
encodedAddress: changeAddress,
301+
value: BigInt(2741512),
302+
scriptConfig: changeConfig,
303+
},
304+
],
305+
fee: BigInt(191681),
306+
ttl: BigInt(41539125),
307+
certificates: [
308+
{
309+
voteDelegation: {
310+
keypath: "m/1852'/1815'/0'/2/0",
311+
type: drepType,
312+
},
313+
},
314+
],
315+
withdrawals: [],
316+
validityIntervalStart: BigInt(41110811),
317+
allowZeroTTL: false,
318+
};
293319
case 'withdraw-staking-rewards':
294320
return {
295321
network,

scripts/build-protos.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ fn add_serde_attrs(c: &mut prost_build::Config) {
6767
"shiftcrypto.bitbox02.BTCPubRequest.XPubType.CAPITAL_YPUB",
6868
"serde(rename = \"Ypub\")",
6969
),
70+
// Cardano
7071
(
7172
"shiftcrypto.bitbox02.CardanoNetwork.CardanoMainnet",
7273
"serde(rename = \"mainnet\")",
7374
),
74-
// Cardano
7575
(
7676
"shiftcrypto.bitbox02.CardanoNetwork.CardanoTestnet",
7777
"serde(rename = \"testnet\")",
@@ -92,6 +92,10 @@ fn add_serde_attrs(c: &mut prost_build::Config) {
9292
"keypath_stake",
9393
"serde(deserialize_with = \"crate::keypath::serde_deserialize\")",
9494
),
95+
(
96+
"shiftcrypto.bitbox02.CardanoSignTransactionRequest.Certificate.VoteDelegation.type",
97+
"serde(deserialize_with = \"crate::cardano::serde_deserialize_drep_type\")",
98+
),
9599
];
96100

97101
for (path, attr) in type_attrs {

src/cardano.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ where
1515
Ok(network as i32)
1616
}
1717

18+
#[cfg(feature = "wasm")]
19+
pub(crate) fn serde_deserialize_drep_type<'de, D>(deserializer: D) -> Result<i32, D::Error>
20+
where
21+
D: serde::Deserializer<'de>,
22+
{
23+
use serde::Deserialize;
24+
let drep_type = pb::cardano_sign_transaction_request::certificate::vote_delegation::CardanoDRepType::deserialize(deserializer)?;
25+
Ok(drep_type as i32)
26+
}
27+
1828
#[cfg(feature = "wasm")]
1929
#[derive(serde::Deserialize)]
2030
pub(crate) struct SerdeScriptConfig(pb::cardano_script_config::Config);

src/shiftcrypto.bitbox02.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,10 @@ pub mod cardano_sign_transaction_request {
12481248
)]
12491249
pub keypath: ::prost::alloc::vec::Vec<u32>,
12501250
#[prost(enumeration = "vote_delegation::CardanoDRepType", tag = "2")]
1251+
#[cfg_attr(
1252+
feature = "wasm",
1253+
serde(deserialize_with = "crate::cardano::serde_deserialize_drep_type")
1254+
)]
12511255
pub r#type: i32,
12521256
#[prost(bytes = "vec", optional, tag = "3")]
12531257
pub drep_credhash: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
@@ -1269,9 +1273,13 @@ pub mod cardano_sign_transaction_request {
12691273
)]
12701274
#[repr(i32)]
12711275
pub enum CardanoDRepType {
1276+
#[cfg_attr(feature = "wasm", serde(rename = "keyHash"))]
12721277
KeyHash = 0,
1278+
#[cfg_attr(feature = "wasm", serde(rename = "scriptHash"))]
12731279
ScriptHash = 1,
1280+
#[cfg_attr(feature = "wasm", serde(rename = "alwaysAbstain"))]
12741281
AlwaysAbstain = 2,
1282+
#[cfg_attr(feature = "wasm", serde(rename = "alwaysNoConfidence"))]
12751283
AlwaysNoConfidence = 3,
12761284
}
12771285
impl CardanoDRepType {

src/wasm/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type CardanoOutput = {
104104
scriptConfig?: CardanoScriptConfig;
105105
assetGroups?: CardanoAssetGroup[];
106106
}
107+
type CardanoDrepType = 'keyHash' | 'scriptHash' | 'alwaysAbstain' | 'alwaysNoConfidence'
107108
type CardanoCertificate =
108109
| {
109110
stakeRegistration: {
@@ -120,6 +121,13 @@ type CardanoCertificate =
120121
keypath: Keypath
121122
poolKeyhash: Uint8Array
122123
}
124+
}
125+
| {
126+
voteDelegation: {
127+
keypath: Keypath
128+
type: CardanoDrepType
129+
drepCredhash?: Uint8Array
130+
}
123131
};
124132
type CardanoWithdrawal = {
125133
keypath: Keypath;

0 commit comments

Comments
 (0)