Generating ethereum address from ECDSA Public Key - ethereum

I am using node-webcrypto-p11 and generating keys via following code
keys = crypto.subtle.generateKey({name: "ECDSA", namedCurve: "K-256"}, false, ["sign", "verify"]);
what is the eth address.
('0x' + keccak('keccak256').update(key).digest().slice(-20).toString('hex');)
I am finding eth address like this is this correct?

It seems right to me. You can test your solution with this little script
const wallet = require('ethereumjs-wallet');
var account = wallet.fromPrivateKey(Buffer.from('348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709', 'hex'));
console.log(account.getChecksumAddressString());
or with web3:
var account = web3.eth.accounts.privateKeyToAccount('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709');
console.log(account.address);

Related

Why I don't have to pay gas for listing an NFT on opensea?

How can this be done, is it a signed voucher? Any sample code that I can take a look?
Until anyone buys the NFT, it does not exist on the blockchain. It's "just" a record in OpenSea's offchain database.
Only when they buy the NFT through OpenSea, the buyser sends a transaction - effectively paying gas fees to mint the NFT.
Code example of signing a message and validating with ethers.js
const message = "hello";
const [signer] = await ethers.getSigners();
const signature = await signer.signMessage(message);
console.log(
ethers.utils.verifyMessage(message, signature) == signer.address
);

Google App Script - How to monitor for new users

I wondered if anyone could point me in the right direction here?
I want to monitor the Google Workspace estate, and when a new user has been created send them an email. I’ve looked through the APIs but nothing is jumping out at me. But I know there are 3rd party tools out there that do this, so there’s got to be something I have missed?
I just created this script in Google Apps Script which gets and prints the list of all the users that were created today.
You can use this as a guide and keep testing with it. To accomplish this I used the Reports API to get the admin logs and get the list of all the users that were created today.
function myFunction() {
var userKey = 'all';
var applicationName = 'admin';
var optionalArgs = {
eventName:'CREATE_USER',
startTime: "2022-03-23T12:00:00.000Z",
fields : "items.events.parameters.value"
};
var rep = AdminReports.Activities.list(userKey,applicationName,optionalArgs);
const A = (JSON.parse(rep));
var totalUsers = Object.keys(A.items).length;
for(var i=0; i<totalUsers; i++)
{
var userEmail = A.items[i].events[0].parameters[0].value;
Logger.log(userEmail);
}
}
You would just need to change the startTime value according to the date you need to use and implement the part of sending the email now that you have all the email addresses.
References
API method: activities.list
Apps Script reference: Reports API

How can I create a new account or address with web3.js?

I'm trying to interact with geth, and I need to create a new deposit address (and be able to control it). How can I implement this with web3.js?
You can use the Web3.eth.accounts.create() function. It will return an account object which you'll be able to control.
https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html
Example:
var Web3 = require("web3");
var web3 = new Web3('http://localhost:8545'); // your geth
var account = web3.eth.accounts.create();
You can also use the web3.eth.personal.newAccount() function.
http://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#newaccount
Check this blog post to help determine which is right for you.
https://medium.com/#andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb
Note: This answer is for Web3.js 1.0.

Google Contacts Address is not retrieved by my Script (although it is filled)

I try to fetch the content of certain google contacts via Google Apps Script. First I identified the ID of the contact via a getId Function. My Script is this:
var id = 'id';
var contact = ContactsApp.getContactById(id);
var address = contact.getAddresses();
GmailApp.sendEmail("email", address, "");
The return I get via mail is "AddressField", allthough the certain contact definitely has an address.
In Addition I also tried the following script from the official reference (which returns the same thing):
// Logs the address for the 'Home Address' field for contact 'John Doe'.
// Can be used similarly for other fields that contain addresses.
var contacts = ContactsApp.getContactsByName('John Doe');
var homeAddress = contacts[0].getAddresses(ContactsApp.Field.HOME_ADDRESS);
Logger.log(homeAddress[0].getAddress());
Can anyone help me?
Thanks a lot in advance.
Best, Phil
This works for me :
function testContact(){
var contacts = ContactsApp.getContactsByName('some name in my contacts');
Logger.log(contacts[0].getFamilyName());// just to check it's the right one
var address = contacts[0].getAddresses();
for (var n=0;n<address.length;n++){
Logger.log(address[n].getLabel()+' : '+address[n].getAddress());// get all address fields for this contact + corresponding label
}
}

How do I getGivenName() of getActiveUser()

Each user on the domain initiates a simple script we run for leave entitlements but we want the welcome message to be "Hi First Name," however the script doesn't seem to be able to fetch getGivenName() from getActiveUser() for a standard user.
Is there a way?
As noted in comments, and in Documentation, the UserManager Service is only accessible by Domain Administrators.
Here's an alternative. Domain Users may have themselves in their own contacts, so how about a best-effort attempt at finding themselves there?
/**
* Get current user's name, by accessing their contacts.
*
* #returns {String} First name (GivenName) if available,
* else FullName, or login ID (userName)
* if record not found in contacts.
*/
function getOwnName(){
var email = Session.getEffectiveUser().getEmail();
var self = ContactsApp.getContact(email);
// If user has themselves in their contacts, return their name
if (self) {
// Prefer given name, if that's available
var name = self.getGivenName();
// But we will settle for the full name
if (!name) name = self.getFullName();
return name;
}
// If they don't have themselves in Contacts, return the bald userName.
else {
var userName = Session.getEffectiveUser().getUsername();
return userName;
}
}
In Apps Script, I was able to get this information using the About REST API: https://developers.google.com/drive/v2/reference/about/get
var aboutData = DriveApp.About.get();
var userEmail = aboutData["user"]["emailAddress"];
var userDisplayName = aboutData["user"]["displayName"];
You can get a user name but first you have to create a domain user using the provisioning api. You can enable the API by logging in to your admin account, and select Domain settings and the User settings tab to select the checkbox enabling the Provisioning API. Read more about it here
You can then use
user = user.getgivenName()
Since the UserManager Service is only available to a Domain Administrator, you could publish a service as the administrator, that serves user's Given Names, and invoke that from the user-run script using the UrlFetchApp.
The UserName Service
Refer to the Content Service Documentation for the background information this is based upon.
The service accepts a parameter, userName, which it uses to perform a lookup as the administrator.
Paste the following code into a script, then deploy the script as a web service. This must be done by a Domain Administrator, as the service access the UserManager Service, but the script must be made accessible by all users in the domain. (Since I'm not an admin in my domain, I cannot access the UserManager, so I've included a domain-user-invokable line for testing, calling the getOwnName() function I described in my first answer.)
Remember to invoke doGet() from the debugger to go through the authorization before accessing the published service.
/**
* When invoked as a Web Service running as Domain Administrator,
* returns the GivenName of the requested user.
*
* #param {String} userName= Should be set to Session.getEffectiveUser().getUsername().
*/
function doGet(request) {
//return ContentService.createTextOutput(getOwnName()); // for testing by non-admin user
var userName = request.parameters.userName;
var givenName = UserManager.getUser(userName).getGivenName();
return ContentService.createTextOutput(givenName);
}
Invoke service using UrlFetch
Refer to Using External APIs for an explanation of how to make use of the service written in the previous section. I'll show how to access the service from another script, but remember that you can also do this from web pages within your domain.
We will use UrlFetchApp.fetch() to get our service to return the user's first name as a String.
The service was written to accept one parameter, userName, and we append this to the url, in the form userName=<string>.
With the URL built, we fetch(), then retrieve the name from the response. While this example returns just the name, you may choose to change the service to return the complete "Hello User" string.
function testService() {
var domain = "my-google-domain.com";
var scriptId = "Script ID of service";
var url = "https://script.google.com/a/macros/"+domain+"/s/"+scriptId+"/exec?"
+ "userName="+Session.getEffectiveUser().getUsername();
var response = UrlFetchApp.fetch(url);
var myName = response.getContentText();
debugger; // pause in debugger
}
Another potential way of getting the display name on a gmail account is to find a Draft email in the GmailApp, and get the From header, which may have the full name. Some drafts might be setup with no display name in gmail, in which case the From header will only be the email address, but typically the From header is in the format:
Firstname Lastname <email#domain.com>
This code should get you the string above from the first gmail Draft: (note this will probably throw an exception if there are no drafts, so check that first.)
GmailApp.getDrafts()[0].getMessage().getHeader("From")
Ref: https://developers.google.com/apps-script/reference/gmail/gmail-message#getHeader(String)
Ref: https://www.ietf.org/rfc/rfc2822.txt