New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Tips and tricks for generating vanity addresses - Andrej Rakic | Chainlink

ETH Belgrade CommunitySat, Oct 7, 2023, 12:00 AM

Transcript

[Music] okay cool okay we can start so thank you for being patient and for waiting we can now start with the talk okay so the first thing I want from you is to take a look at this address so this address is actually a smart contract address what I want from you is to try to analyze it see maybe if you see something unusual or strange spoil alert this address is the address of a uni token on ethereum mainnet so the token from units for operating so if you get take a deeper look we can see that this contract starts and ends with the same sequences of characters so 1f 984 at the beginning and 1f984 at the very end so we all know that unit swap is like their branding branding is around unicorns everything is pink blah blah so you probably know that each emoji on your little smartphone has its own Unicode and one F 984 is actually a Unicode for unicorn Emoji so we can interpret this address of a unit token on ethereum mainnet as this address you know it starts and ends with unicorn so unit swap guys cleverly embedded their logo into their own uh into the into the address of their own token if you're curious how you can do the same thing this talk is all about this stuff okay so my name is Andre I'm been in this space for a while I've been to several conferences watched dozens of talks both in person and online and I'm extremely grateful and thankful to be part of the first ever ethereum conference in Belgrade in my own country so everything started with this tweet basically I wanted to create something new some brand new talk beautiful Builders with zero Shilling uh just as a gratitude and to say thank you to all of those previous Builders who created dozens of quality dogs which helped me to be a better developer which helped me to be a better engineer to understand this space even better and decision came to to 180 addresses so it's going to be that tiny little thing which you can do in solidity or in ethereum mainnet okay so why vanity addresses in the first place first of all a lot of contracts a lot of protocols actually wants to have a same contract address on all different genes if you have like a cross stream protocol for various different reasons that's a good idea to have like a same contract address on all different things right so UNICEF does that a lot of other protocols does that second one what they saw during on previous slides branding purposes you can embed something from your brand into smart contract address whether that's like an emoji or some clever word like I don't know Dad or coffee or whatever can be interpreted with hex numbers and characters that we can read it's a it's a good it's a good candidate to be part of your address then reducing gas cost so this is actually really common especially in the Mev World they tend to deploy contracts with bunch of leading zeros because when you interact with contracts with bunch of leading zeros that can actually cost less gas because when you have in a call data zeros costs less than non-zero Wallace and when you combine multiple zeros it can reduce your gas cost slightly and finally the Easter eggs Dev Prestige uh defining vanity addresses is extremely heavily heavy process so devs tends to break how they are good because they mind that specific address okay so first of all same contract address on multiple Chains It's useful for several release reasons UI development you will make the life of your front-end team easier if your smart contract address is the same on all of these different chains because they need to handle multiple things when you connect with your wallet and you switch from chain to chain they will need to switch also the addresses maybe if it's allowed to maybe API will slightly change because of some L2 checks Etc so there is like a room for error when you have like same smart contract address it's easier for them also it's easier for animal else that wants to build on top of your protocol because they can put this address as a mutable variable or constant and they know that it's the same on all different chains and then your marketing team can fit something like this my awesome Project X is available in this address on ethereum arbitrum optimism polygon all of these stuff by the way the address we are seeing now is the address of a deposit contract for like merged purposes okay so how to do this when we are deploying smart contract it is create opcode so when you like even like when you're in remix and you have like connect with your metamask and skit that orange deploy button it will use create opcode when you put mnemonic or private key in your tnv file and run deployment script it will use create opcode right and create opcode calculates the address of 2B deployed contract as right most 20 bytes of catch up to 256 hash off deployer address and deployer nouns so deployer address is you know the address of eoi who who tries to deploy smart contract and it's always the same right when you create a new ethereum account your address is unique and that's it so what you can do is tweak the non's parameter so do we know what nonce is yeah yeah okay cool so nouns stands for number only once to prevent replay attacks Etc so does anyone kind of idea now how we can have the same address to multiple chains using the regular deployment way we are create top code yeah so what we can do is basically if we want our address to be the same of all different trains and we doesn't care how that address looks like we can create a fresh new externally owned account denounce will be zero right we'll fund it with some coins like predefined coin well value of coins for gas purposes and run the deployment script on all of different chains so I'll run the plan and script on ethereum mainnet and polygon or arbitrum optimism Etc and because the address of my deployer account is whatever and nonce is zero because that's my first account the address will be the same because it's going to be right rightmost 20 20 bytes of catrak 256 of my address and zero because that's my nonsense it's a fresh new account and it address even has this tiny function get from util's package get Contract address which can calculate you the address of smart contract before you deploy it if you're going to deploy it in the next in the next transaction so you know the nodes unfront so if we take this deployer address this is actually the address of vitalik's wallet and if it's known c0 then we can calculate how would the address contract address if we targeted it as his first transaction will look like okay this is cool right easy to do but it's not very sustainable and why so this solution as stated at this slide is a bit naively solution because it relies on the fact that the new accounts nouns is synced across all chains so what if we have some transaction in the middle what if our deployment script fails then we'll have like a failed transaction but the nonce will increase right because our nonses are now not in synced the the address of on the next chain will not be the same as the contract address on all previous genes which we don't want so obviously we can do better there is something called create to opcode so it's like for regular deployments we use create top code but there is a possibility to use create to opcode which essentially pre-computes the address of your smart contract before deployment and create two op code Works a bit different than create topcode so what you can see on this slide is that contract address is compute as again rightmost 20 bytes of care check 256 of some other parameters first of all FF this is just the constants to prevent Collision from when when we are doing with create op code so just a constant nothing we can do really about it again deployer's address the 14 is actually to be deployed contracts init code hash and the third thing is I'll I intentionally put it as the last last step and this slide is something called salt so salt is an arbitrary value provided by deployer so FF obviously a constant nothing we can do about it deploys smart contract deploys UI address nothing we could do about it we can maybe change the deployer address but eventually that's going to be a constant and to be deployed contracts init code hash well that's also there's no no much thing that we can do about it either because it's like a product of our solidity code right and we don't want to compromise security so what we can tweak is actually this solve parameter right so to find to find all of these fancy addresses there is something there is a technique called salt Mining and we're going to tackle it later but another important thing is that we're we're dealing with create two in most of the cases you're not going to deploy it using external account you're going to deploy it using another smart contract because that factory contract can actually trigger to create to opcode under the hood so you can have a view function which does exactly what we saw in the previous slide you it calculates the address of 2B deployed smart contract so you can see that we are providing init code as a function argument and salt so what you can do is maybe you can tweak that sold to find the perfect the perfect address okay which create your factory to use there are a bunch of options which you can see at the following slide so create your deployer there's something called deterministic deployment proxy from zol2 and another one for arachnid that third one is actually using in Foundry under the hood so if you take a look at this syntax over there like Fourier equals new Foo and these parenthesis with salt that syntax actually using creative Factory by by arachnid which is like a third third URL over here and the fourth one is just open supplements implementation right so the tldr from this slide is that you can do this without knowing what creative Factory is without developing that contract you can just do it from Foundry for example with this tiny syntax right so salt mining the important thing in finding those addresses is actually finding a salt value so this is where Dev tends to brag between each other because this process is sometimes extremely heavy so I think like one of the best repos for doing that is from zero age he's like one of the main devs behind the seaport protocol and the repo is called create2crunch it's mainly written in Rust and there's some type tiny python code snippet but basically when you get clone query to Crunch and it's Stars the command first of all your laptop is gonna be extremely starting to pronounce uh like wild noise noises maybe to to get hit Etc but it will try to my new efficient addresses when it's what I mean by efficient it will try to menu addresses solves for addresses that start with leading zeros second option it's present in Foundry so Foundry has this tiny CLI command so this command using this command you can also mine salts for various different addresses okay how this do so example number one you can see the address of one inch router and they're intentionally it starts with one two seven number ones because one inch one is part of their branding right so they wanted to create a smart contract that starts with not a couple number ones okay okay so how you can do replicate the same thing in Foundry well you can run cast create to command using D flag pass the deployer address which will be create two Factory smart contract address using flag I paste the init code hash and there is this tiny flag says starts with seven seven ones example number two this is the latest version of C Port smart contract it was mined using Create to to Crunch the the repo you saw on the previous slide not by Foundry but you can replicate the same thing running The Foundry command this address has like 12 or 13 zeros so finding that address takes a long time extremely long time if you have lucky maybe less but you know that's that's like a Dev Prestige because they have uh an address with so many zeros and interacting with C Port is going to be cheaper for regular users because this address is going to be part of call data and bunch of leading zeros will cost less gas okay example number three are famous uni token address from the beginning so what you can like it starts and ends with unicorn Emoji so the same thing what you can do is in Foundry with cast create two command it have like has flag starts with and ends with so with when running this command you should be able to find a similar address actually the address which starts and ends with the same pattern okay but this is all Theory let's do it now in practice okay I have one uh like really dummy smart contract most of it most of their parts is like written by GitHub go file it but it's like a contract for donations you know like buy me a coffee contract which can only receive native coins and uh only beneficiary address can withdraw the money from the contract right our job for today is to find a smart contract address which will end with c0 ffee with something that we can read as a human's as coffee right so that's our goal for today's we have the smart contract so what we can do is that we can calculate init code hash and we'll see about the deployer what we can do so this is what we want to accomplish in our test so this is like a simple dummy test written in Foundry some setup is over there and what we can what we want is actually in this task deployment address function is to console log the address of us to be deployed smart contract and to see that actually indeed ends with word coffee or c0 ffe is pretty basic so we have one beneficiary address like completely completely default one we have salt which is currently set to zero so we need to mine that salt and finally we have this syntax you already saw from Foundry with those parenthesis salt equals salt okay uh what you can actually do now is that you can calculate init code hash so init code hash can be calculated in multiple multiple ways this one is in Foundry because everything in solidity you can get creation code code okay by doing the the bytes memory equals creation code blah blah from the first line okay if your smart contract doesn't have any Constructor arguments you will just hash that value that's your init code hash if but I intentionally set beneficiary address to be part of Constructor because initcode hash then will depend on input values so init code now is like we need to ABI encode uh the regular creation code and the uh and the beneficial address which is like a Constructor argument right and then we're gonna hash everything using of course catrick 256 cache because that's the official hashing algorithm on ethereum right that's how we find found the init code hash now we need create to deploy our address because on a previous slide you saw that you can use that default syntax from Foundry with parenthesis you can go to their official documentation and find that at least in local the create 2 deployer on on Anvil node the create to deployer address is that one present at the very top of the file so that's just the address of their create to deploy our contract again using uh deployment proxy from arachnid and that's it that's like your constant now what we need to do is actually to run this command so cast create two Dash D is a flag for deployer we literally copy pasted the degree to deploy from command from comment above like from Foundry then I that's our init code hash so we calculated this value from our previous function get creation code encode encode arguments and then hash it and finally it should end with coffee or c0 FF EE okay I run this command and my own laptop and under in 12 seconds it was able to mine me the address you see here so it's 0x3ao something but it ends with a sequence I wanted c0 ffee and this is the recommend this is the salt which will produce this address combining initial Dash and deploy our address so if I want to do this on a local animal node in Foundry this these are the parameters I need to I need to provide okay the difference between cast command cast to create cast create to command from Foundry and that creates to Crunch from zero age is that Foundry will literally stop when it's fine the first address while created to Crunch is going to run maybe infinitely if you want because it will provide you a bunch of options and then you can literally choose salt for various different addresses if you like those addresses better okay so that's pretty much it we're on time we have we had 20 minutes for this talk we have some questions time for some questions if you if you have it from the audience I hope you like that I hope you like this this talk if you love it we can grab a cup of coffee afterwards but please don't mix salt with sugar thank you [Applause] just wait for the mic thanks Andre uh question um so on your laptop it took 12 seconds to find Seoul for uh address with six characters so how long do you think it took for optimism smart contract address which is ridiculously vanity how did they generate it I don't think it's it was it doesn't um it's not related to us to the blockchain you want to deploy it because this 12 seconds are related to the power of my machine so it's going to be the same thing for ethereum optimism no no I got it but the optimism smart contract addresses Ox four two zero zero zero zero zero four two so two mine sold for that one on I don't know what machine you have to have I think it's how do they manage to do that uh well what I know is that zero age used for for uh Seaport at least they spin up the AWS instance I think there's like a thread on his Twitter uh and say about the parameters machines and that's how it is however they're I don't think they're present on optimism so it really depends for for other different chains it really depends about the op code itself like is it compatible if it's create to op code like it's gonna be it's gonna be the same but again uh it's really like if you want to have another chain which has like different op codes Under the Hood it's not going to be the same process obviously it wasn't about different chains so in ethereum the optimism smart contract deploying ethereum mainnet it has like really super ridiculously vanity address okay which is okay okay how they did it yeah we can ask them I don't know like this is how you can do it using Foundry you can spin up like AWS instance you can use create your crunch I'm not sure like what they did again this is like part of devs being devs and you know like devs Prestige I'm not part of that team so they can know that okay I understood the question now this is like a general like obviously when uniswap deployed the unit token created to con op code wasn't part of ethereum yet I think so they use this different mechanism obviously but now you can do it using something like this hope that answers your question anything else yeah over there hello uh thank you for the nice presentation this is very interesting but I have one question so uh doesn't something stops me from uh generating my address that starts with unicorn and ends with unicorn uh no because probably not going to be the same as unit tokens one but if it's you know yeah yeah okay thank you you're welcome you got another one yeah test okay I guess this is a very Brute Force approach right yeah it is so have you thought about if there's any analytics that would help you guess the salt and I don't think speed up there are at the moment it's literally a Brute Force where you like switch salt is until you find the desired pattern that's it okay no thank you very much [Applause]

Automatic transcript — names and jargon may be misspelled.