From b3832ab8c77488896b792089cc8727e39fa1cd4f Mon Sep 17 00:00:00 2001 From: Hendrik Hofstadt Date: Wed, 5 Aug 2020 13:12:54 +0200 Subject: [PATCH] Fully implement U256; add source_address to transfer VAA --- bridge/pkg/vaa/structs.go | 7 +++ bridge/pkg/vaa/types_test.go | 3 + docs/protocol.md | 1 + solana/bridge/Cargo.lock | 99 ++++++++------------------------ solana/bridge/Cargo.toml | 4 +- solana/bridge/src/instruction.rs | 3 +- solana/bridge/src/processor.rs | 19 ++++-- solana/bridge/src/vaa.rs | 12 +++- solana/cli/Cargo.lock | 69 ++++++---------------- 9 files changed, 80 insertions(+), 137 deletions(-) diff --git a/bridge/pkg/vaa/structs.go b/bridge/pkg/vaa/structs.go index d0ecd8bc..6b640681 100644 --- a/bridge/pkg/vaa/structs.go +++ b/bridge/pkg/vaa/structs.go @@ -69,6 +69,8 @@ type ( SourceChain ChainID // TargetChain is the id of the chain the transfer is directed to TargetChain ChainID + // TargetAddress is the address of the sender on SourceChain + SourceAddress Address // TargetAddress is the address of the recipient on TargetChain TargetAddress Address // Asset is the asset to be transferred @@ -273,6 +275,10 @@ func parseBodyTransfer(r io.Reader) (*BodyTransfer, error) { return nil, fmt.Errorf("failed to read target chain: %w", err) } + if n, err := r.Read(b.SourceAddress[:]); err != nil || n != 32 { + return nil, fmt.Errorf("failed to read source address: %w", err) + } + if n, err := r.Read(b.TargetAddress[:]); err != nil || n != 32 { return nil, fmt.Errorf("failed to read target address: %w", err) } @@ -303,6 +309,7 @@ func (v *BodyTransfer) serialize() ([]byte, error) { MustWrite(buf, binary.BigEndian, v.Nonce) MustWrite(buf, binary.BigEndian, v.SourceChain) MustWrite(buf, binary.BigEndian, v.TargetChain) + buf.Write(v.SourceAddress[:]) buf.Write(v.TargetAddress[:]) if v.Asset == nil { diff --git a/bridge/pkg/vaa/types_test.go b/bridge/pkg/vaa/types_test.go index bedb3dd7..bbd0b94d 100644 --- a/bridge/pkg/vaa/types_test.go +++ b/bridge/pkg/vaa/types_test.go @@ -1,6 +1,7 @@ package vaa import ( + "encoding/hex" "github.com/certusone/wormhole/bridge/third_party/chainlink/cryptotest" "github.com/certusone/wormhole/bridge/third_party/chainlink/secp256k1" "github.com/ethereum/go-ethereum/common" @@ -31,6 +32,7 @@ func TestSerializeDeserialize(t *testing.T) { Nonce: 38, SourceChain: 2, TargetChain: 1, + SourceAddress: Address{2, 1, 4}, TargetAddress: Address{2, 1, 3}, Asset: &AssetMeta{ Chain: 9, @@ -63,6 +65,7 @@ func TestSerializeDeserialize(t *testing.T) { vaaData, err := test.vaa.Serialize() require.NoError(t, err) + println(hex.EncodeToString(vaaData)) vaaParsed, err := ParseVAA(vaaData) require.NoError(t, err) diff --git a/docs/protocol.md b/docs/protocol.md index 53b55808..464e856a 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -180,6 +180,7 @@ Payload: uint32 nonce uint8 source_chain uint8 target_chain +[32]uint8 source_address [32]uint8 target_address uint8 token_chain [32]uint8 token_address diff --git a/solana/bridge/Cargo.lock b/solana/bridge/Cargo.lock index a83bf351..7b76db4c 100644 --- a/solana/bridge/Cargo.lock +++ b/solana/bridge/Cargo.lock @@ -33,12 +33,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "arrayvec" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" - [[package]] name = "assert_matches" version = "1.3.0" @@ -99,16 +93,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" -[[package]] -name = "bitvec" -version = "0.17.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41262f11d771fd4a61aa3ce019fca363b4b6c282fca9da2a31186d3965a47a5c" -dependencies = [ - "either", - "radium", -] - [[package]] name = "block-buffer" version = "0.7.3" @@ -168,12 +152,6 @@ dependencies = [ "serde", ] -[[package]] -name = "byte-slice-cast" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3" - [[package]] name = "byte-tools" version = "0.3.1" @@ -216,7 +194,7 @@ dependencies = [ "quote 1.0.7", "serde", "serde_json", - "syn 1.0.36", + "syn 1.0.38", "tempfile", "toml", ] @@ -443,7 +421,7 @@ checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", "synstructure", ] @@ -465,9 +443,6 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11498d382790b7a8f2fd211780bec78619bba81cdad3a283997c0c41f836759c" dependencies = [ - "byteorder", - "rand", - "rustc-hex", "static_assertions", ] @@ -529,7 +504,7 @@ dependencies = [ "proc-macro-hack", "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -751,15 +726,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "impl-codec" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1be51a921b067b0eaca2fad532d9400041561aa922221cc65f95a85641c6bf53" -dependencies = [ - "parity-scale-codec", -] - [[package]] name = "indexmap" version = "1.5.0" @@ -1001,7 +967,7 @@ checksum = "e0396233fb2d5b0ae3f05ff6aba9a09185f7f6e70f87fb01147d545f85364665" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -1057,18 +1023,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" -[[package]] -name = "parity-scale-codec" -version = "1.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34d38aeaffc032ec69faa476b3caaca8d4dd7f3f798137ff30359e5c7869ceb6" -dependencies = [ - "arrayvec", - "bitvec", - "byte-slice-cast", - "serde", -] - [[package]] name = "parking_lot" version = "0.9.0" @@ -1128,7 +1082,7 @@ checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -1156,7 +1110,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c55c21c64d0eaa4d7ed885d959ef2d62d9e488c27c0e02d9aa5ce6c877b7d5f8" dependencies = [ "fixed-hash", - "impl-codec", "uint", ] @@ -1214,12 +1167,6 @@ dependencies = [ "proc-macro2 1.0.19", ] -[[package]] -name = "radium" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "def50a86306165861203e7f84ecffbbdfdea79f0e51039b33de1e952358c47ac" - [[package]] name = "rand" version = "0.7.3" @@ -1388,7 +1335,7 @@ checksum = "b9bdc5e856e51e685846fb6c13a1f5e5432946c2c90501bdc76a1319f19e29da" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -1454,7 +1401,7 @@ checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -1557,7 +1504,7 @@ dependencies = [ "reqwest", "serde", "syn 0.15.44", - "syn 1.0.36", + "syn 1.0.38", "tokio 0.1.22", "winapi 0.3.9", ] @@ -1620,7 +1567,7 @@ dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", "rustversion", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -1631,13 +1578,13 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "spl-token" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07c8482ae4aac6bb7d73aef79df5fb403a16a0cfbe200442532cff6b98613383" +version = "1.0.3" +source = "git+https://github.com/hendrikhofstadt/solana-program-library?branch=256b-primitive-types#10c1e62b9503e646b403cd48231b47e6d605c54f" dependencies = [ "cbindgen", - "num-derive 0.2.5", + "num-derive 0.3.1", "num-traits", + "primitive-types", "remove_dir_all", "solana-sdk", "thiserror", @@ -1680,9 +1627,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.36" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cdb98bcb1f9d81d07b536179c269ea15999b5d14ea958196413869445bb5250" +checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", @@ -1697,7 +1644,7 @@ checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", "unicode-xid 0.2.1", ] @@ -1750,7 +1697,7 @@ checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", ] [[package]] @@ -2032,9 +1979,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2734b5a028fa697686f16c6d18c2c6a3c7e41513f9a213abb6754c4acb3c8d7" +checksum = "d593f98af59ebc017c0648f0117525db358745a8894a8d684e185ba3f45954f9" dependencies = [ "lazy_static", ] @@ -2182,7 +2129,7 @@ dependencies = [ "log", "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", "wasm-bindgen-shared", ] @@ -2216,7 +2163,7 @@ checksum = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2353,7 +2300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d498dbd1fd7beb83c86709ae1c33ca50942889473473d287d56ce4770a18edfb" dependencies = [ "proc-macro2 1.0.19", - "syn 1.0.36", + "syn 1.0.38", "synstructure", ] @@ -2374,6 +2321,6 @@ checksum = "de251eec69fc7c1bc3923403d18ececb929380e016afe103da75f396704f8ca2" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", - "syn 1.0.36", + "syn 1.0.38", "synstructure", ] diff --git a/solana/bridge/Cargo.toml b/solana/bridge/Cargo.toml index 8f8c7ecc..d0831781 100644 --- a/solana/bridge/Cargo.toml +++ b/solana/bridge/Cargo.toml @@ -20,12 +20,12 @@ num-derive = "0.2" num-traits = "0.2" remove_dir_all = "=0.5.0" solana-sdk = { version = "=1.2.17", default-features = false, optional = true } -spl-token = { package = "spl-token", version = "1.0.6", default-features = false, optional = true } +spl-token = { package = "spl-token", git="https://github.com/hendrikhofstadt/solana-program-library", branch="256b-primitive-types", default-features = false, optional = true } thiserror = "1.0" byteorder = "1.3.4" zerocopy = "0.3.0" sha3 = "0.9.1" -primitive-types = "0.7.2" +primitive-types = {version ="0.7.2", default-features = false} [dev-dependencies] rand = { version = "0.7.0" } diff --git a/solana/bridge/src/instruction.rs b/solana/bridge/src/instruction.rs index 6067a550..f42d39d2 100644 --- a/solana/bridge/src/instruction.rs +++ b/solana/bridge/src/instruction.rs @@ -15,6 +15,7 @@ use crate::error::Error; use crate::instruction::BridgeInstruction::Initialize; use crate::state::{AssetMeta, BridgeConfig}; use crate::syscalls::RawKey; +use primitive_types::U256; /// chain id of this chain pub const CHAIN_ID_SOLANA: u8 = 1; @@ -45,7 +46,7 @@ pub struct InitializePayload { #[derive(Clone, Copy)] pub struct TransferOutPayload { /// amount to transfer - pub amount: u64, + pub amount: U256, /// chain id to transfer to pub chain_id: u8, /// Information about the asset to be transferred diff --git a/solana/bridge/src/processor.rs b/solana/bridge/src/processor.rs index 06cfc20d..c088ccdb 100644 --- a/solana/bridge/src/processor.rs +++ b/solana/bridge/src/processor.rs @@ -32,6 +32,7 @@ use crate::state::*; use crate::syscalls::{sol_verify_schnorr, RawKey, SchnorrifyInput}; use crate::vaa::{BodyTransfer, BodyUpdateGuardianSet, VAABody, VAA}; use crate::{error::Error, instruction::unpack}; +use primitive_types::U256; /// Instruction processing logic impl Bridge { @@ -620,7 +621,7 @@ impl Bridge { token_program_id: &Pubkey, authority: &Pubkey, token_account: &Pubkey, - amount: u64, + amount: U256, ) -> Result<(), ProgramError> { let all_signers: Vec<&Pubkey> = accounts .iter() @@ -643,7 +644,7 @@ impl Bridge { mint: &Pubkey, destination: &Pubkey, bridge: &Pubkey, - amount: u64, + amount: U256, ) -> Result<(), ProgramError> { let ix = spl_token::instruction::mint_to( token_program_id, @@ -663,7 +664,7 @@ impl Bridge { source: &Pubkey, destination: &Pubkey, authority: &Pubkey, - amount: u64, + amount: U256, ) -> Result<(), ProgramError> { let all_signers: Vec<&Pubkey> = accounts .iter() @@ -687,7 +688,7 @@ impl Bridge { bridge: &Pubkey, source: &Pubkey, destination: &Pubkey, - amount: u64, + amount: U256, ) -> Result<(), ProgramError> { let ix = spl_token::instruction::transfer( token_program_id, @@ -738,8 +739,14 @@ impl Bridge { payer, Self::derive_wrapped_asset_seeds(bridge, asset.chain, asset.address), )?; - let ix = - spl_token::instruction::initialize_mint(token_program, mint, None, Some(bridge), 0, 8)?; + let ix = spl_token::instruction::initialize_mint( + token_program, + mint, + None, + Some(bridge), + U256::from(0), + 8, + )?; invoke_signed(&ix, accounts, &[&[&bridge.to_bytes()[..32]][..]]) } diff --git a/solana/bridge/src/vaa.rs b/solana/bridge/src/vaa.rs index 032a962a..834ade1b 100644 --- a/solana/bridge/src/vaa.rs +++ b/solana/bridge/src/vaa.rs @@ -163,6 +163,7 @@ pub struct BodyTransfer { pub nonce: u32, pub source_chain: u8, pub target_chain: u8, + pub source_address: ForeignAddress, pub target_address: ForeignAddress, pub asset: AssetMeta, pub amount: U256, @@ -209,6 +210,8 @@ impl BodyTransfer { let nonce = data.read_u32::()?; let source_chain = data.read_u8()?; let target_chain = data.read_u8()?; + let mut source_address: ForeignAddress = ForeignAddress::default(); + data.read_exact(&mut source_address)?; let mut target_address: ForeignAddress = ForeignAddress::default(); data.read_exact(&mut target_address)?; let token_chain = data.read_u8()?; @@ -223,6 +226,7 @@ impl BodyTransfer { nonce, source_chain, target_chain, + source_address, target_address, asset: AssetMeta { address: token_address, @@ -237,6 +241,7 @@ impl BodyTransfer { v.write_u32::(self.nonce)?; v.write_u8(self.source_chain)?; v.write_u8(self.target_chain)?; + v.write(&self.source_address)?; v.write(&self.target_address)?; v.write_u8(self.asset.chain)?; v.write(&self.asset.address)?; @@ -273,6 +278,7 @@ mod tests { nonce: 28, source_chain: 1, target_chain: 2, + source_address: [1; 32], target_address: [1; 32], asset: AssetMeta { address: [2; 32], @@ -354,6 +360,10 @@ mod tests { nonce: 38, source_chain: 2, target_chain: 1, + source_address: [ + 2, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ], target_address: [ 2, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -368,7 +378,7 @@ mod tests { amount: U256::from(29), })), }; - let data = hex::decode("01000000090208000000000000000000000000000000000000000000000000000000000000010203040000000000000000000000000000000000000b1510670000002602010201030000000000000000000000000000000000000000000000000000000000090902040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d").unwrap(); + let data = hex::decode("01000000090208000000000000000000000000000000000000000000000000000000000000010203040000000000000000000000000000000000000b15108700000026020102010400000000000000000000000000000000000000000000000000000000000201030000000000000000000000000000000000000000000000000000000000090902040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d").unwrap(); let parsed_vaa = VAA::deserialize(data.as_slice()).unwrap(); assert_eq!(vaa, parsed_vaa); diff --git a/solana/cli/Cargo.lock b/solana/cli/Cargo.lock index 7a151505..bc3609fa 100644 --- a/solana/cli/Cargo.lock +++ b/solana/cli/Cargo.lock @@ -127,16 +127,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" -[[package]] -name = "bitvec" -version = "0.17.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41262f11d771fd4a61aa3ce019fca363b4b6c282fca9da2a31186d3965a47a5c" -dependencies = [ - "either", - "radium", -] - [[package]] name = "blake2b_simd" version = "0.5.10" @@ -207,12 +197,6 @@ dependencies = [ "serde", ] -[[package]] -name = "byte-slice-cast" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3" - [[package]] name = "byte-tools" version = "0.3.1" @@ -313,7 +297,7 @@ dependencies = [ "solana-faucet", "solana-logger", "solana-sdk", - "spl-token", + "spl-token 1.0.6", "wormhole-bridge", ] @@ -625,9 +609,6 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11498d382790b7a8f2fd211780bec78619bba81cdad3a283997c0c41f836759c" dependencies = [ - "byteorder", - "rand", - "rustc-hex", "static_assertions", ] @@ -947,15 +928,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "impl-codec" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1be51a921b067b0eaca2fad532d9400041561aa922221cc65f95a85641c6bf53" -dependencies = [ - "parity-scale-codec", -] - [[package]] name = "indexmap" version = "1.5.0" @@ -1366,18 +1338,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "parity-scale-codec" -version = "1.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34d38aeaffc032ec69faa476b3caaca8d4dd7f3f798137ff30359e5c7869ceb6" -dependencies = [ - "arrayvec", - "bitvec", - "byte-slice-cast", - "serde", -] - [[package]] name = "parking_lot" version = "0.9.0" @@ -1495,7 +1455,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c55c21c64d0eaa4d7ed885d959ef2d62d9e488c27c0e02d9aa5ce6c877b7d5f8" dependencies = [ "fixed-hash", - "impl-codec", "uint", ] @@ -1553,12 +1512,6 @@ dependencies = [ "proc-macro2 1.0.19", ] -[[package]] -name = "radium" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "def50a86306165861203e7f84ecffbbdfdea79f0e51039b33de1e952358c47ac" - [[package]] name = "rand" version = "0.7.3" @@ -2018,7 +1971,7 @@ dependencies = [ "serde_json", "solana-sdk", "solana-vote-program", - "spl-token", + "spl-token 1.0.6", "thiserror", ] @@ -2258,7 +2211,7 @@ dependencies = [ "solana-account-decoder", "solana-sdk", "spl-memo", - "spl-token", + "spl-token 1.0.6", "thiserror", ] @@ -2305,6 +2258,20 @@ dependencies = [ "solana-sdk", ] +[[package]] +name = "spl-token" +version = "1.0.3" +source = "git+https://github.com/hendrikhofstadt/solana-program-library?branch=256b-primitive-types#10c1e62b9503e646b403cd48231b47e6d605c54f" +dependencies = [ + "cbindgen", + "num-derive 0.3.1", + "num-traits", + "primitive-types", + "remove_dir_all", + "solana-sdk", + "thiserror", +] + [[package]] name = "spl-token" version = "1.0.6" @@ -3068,7 +3035,7 @@ dependencies = [ "remove_dir_all", "sha3", "solana-sdk", - "spl-token", + "spl-token 1.0.3", "thiserror", "zerocopy", ]