All private keys listed have been changed and they are addresses on a test network anyway so don't get any ideas!
The issue:
I'm attempting to use the Hex encoding of a Buffer object, however, when I try and use the Buffer.from(key, 'hex') function, I get an empty Buffer object back. Anytime I omit the Hex encoding, the Buffer object acts normally.
Any ideas as to what the root of the issue is?
const privateKey1 = Buffer.from('0x6ee345295b6b2a6df6e06ba3bb5f09a301acf5b1af3c23452025373e15fdb230', 'hex')
console.log(privateKey1)
The above console.log returns <Buffer >
const privateKey2 = Buffer.from('0x6ee88295b6b2a6df6e06ea3ab5f09a401acf5b1af3c75672025373e15edb230')
console.log(privateKey2)
The above console.log returns <Buffer 30 78 36 65 65 38 37 32 32 39 35 62 36 62 32 61 36 64 66 36 65 30 36 63 61 33 61 62 35 66 30 39 61 34 30 31 61 63 66 35 62 31 61 66 33 63 37 38 33 30 ... 16 more bytes>
I also run into the same problem
The way I resolved it was to use a substring to get rid of the 0x value that is at the beginning of the private key
let private_key = '0x6ee88295b6b2a6df6e06ea3ab5f09a401acf5b1af3c75672025373e15edb230';
const privateKey2 = Buffer.from(private_key.substring(2) , 'hex')
Related
I am working on retrieving the image data url from postgresql database and convert back to img src in react. However the below code is not work and the base64 string is kind of strange when console log.
const bufs = Buffer.from(returnUser[0].img);
const base64 = bufs.toString("base64");
res.json({
userName: returnUser[0].username,
id: returnUser[0].id,
userImg: `data:image/jpeg;base64,${base64}`,
});
In nodejs, the data I got back from database returnUser[0].img is like <Buffer 64 61 74 61 3a 69 6d 61 67 65 2f 6a 70 65 67 3b 62 61 73 65 36 34 2c 2f 39 6a 2f 34 41 41 51 53 6b 5a 4a 52 67 41 42 41 51 45 41 53 41 42 49 41 41 44 ... 67141 more bytes>
I console the base64 is like ZGF0YTppbWFnZS9qcGVnO2Jhc2U2NCwvOWovNEF... Then I copy all the base64 stuff to the online decode website. https://base64.guru/converter/decode/file. The result coming back is what I want: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gOTIK/9sAQwAG...
The above result I copy to the react image, it works.
But I don't know how I can get this result in nodejs. Are there any code I am missing?
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBG...
Thanks so much for the answers!
I have been working on S3 storage application using python boto client. Client requests pre-signed upload (Pre-signed PUT) and download (Pre-signed GET) URLs from server for files.
Using boto3 s3 session, this can be done using
response = session.generate_presigned_url(
"put_object",
Params={
"Bucket": client.aws_bucket,
"Key": s3_object,
},
ExpiresIn=client.url_expiration,
)
However, now I am trying to achieve the same using CloudFront apis of boto3. I followed this example to generate a download URL. (Keys are setup in aws console as documentation suggested).
If I upload using s3 pre-signed PUT URL, I cannot download the file generated via CloudFront pre-signed URL. It leads to the following error (some hash values have been changed to hide details):
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<AWSAccessKeyId>removed access key</AWSAccessKeyId>
<StringToSign>AWS4-HMAC-SHA256 20200909T010545Z 20200909/us-east-1/s3/aws4_request dec845474b8be721379ebb7b43a31ad34c658eaf3c9668a428fc0bc0dff02b63</StringToSign>
<SignatureProvided>cd87ad2fc7d6248f8046dbda7aa3db8914c8704d2ab4e939aeeecabf98c8ea37</SignatureProvided>
<StringToSignBytes>41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 32 30 30 39 30 39 54 30 31 30 35 34 35 5a 0a 32 30 32 30 30 39 30 39 2f 75 73 2d 65 61 73 74 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 64 65 63 38 34 35 34 37 34 62 38 62 65 37 32 31 33 37 39 65 62 62 37 62 34 33 61 33 31 61 64 33 34 63 36 35 38 65 61 66 33 63 39 36 36 38 61 34 32 38 66 63 30 62 63 30 64 66 66 30 32 62 36 33</StringToSignBytes>
<CanonicalRequest>GET somefile.png host:example.com x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 x-amz-date:20200909T010545Z host;x-amz-content-sha256;x-amz-date e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855</CanonicalRequest>
<CanonicalRequestBytes>47 45 54 0a 2f 62 33 64 61 61 37 37 62 34 63 30 34 61 39 35 35 31 62 38 37 38 31 64 30 33 31 39 31 66 65 30 39 38 66 33 32 35 65 36 37 2f 73 73 2e 70 6e 67 0a 0a 68 6f 73 74 3a 62 6f 69 6e 67 2e 70 61 6e 61 63 65 61 68 65 61 6c 74 68 2e 61 69 0a 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35 0a 78 2d 61 6d 7a 2d 64 61 74 65 3a 32 30 32 30 30 39 30 39 54 30 31 30 35 34 35 5a 0a 0a 68 6f 73 74 3b 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3b 78 2d 61 6d 7a 2d 64 61 74 65 0a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35</CanonicalRequestBytes>
<RequestId>14670F5525B7189A</RequestId>
<HostId>2xVb5KggcsomehostidpartJjLHVlD0ZDA7TIMWuThXJyYrR/B9g3+RbhPZ7xjoHzKGI=</HostId>
</Error>
Is there a way to generate pre-signed PUT URLs using CloudFront APIs? I could not find anything that is useful. Or should the client always upload using the pre-signed PUT URL generated using s3 session object?
After looking for a few days, I finally noticed the mistakes. From cloudFront control, the policy had to include both PutObject and GetObject. This allowed using the same URL with both PUT and GET methods to upload and download files respectively.
"Action": [
"s3:PutObject",
"s3:GetObject"
]
Also there seemed to be an issue when copying the *.pem file containing key, downloading the file again, and replacing the old one removed the authentication issues.
I'm using following code to generate random number in Groovy. I can run it in e.g. Groovy Web Console (https://groovyconsole.appspot.com/) and it works, however it fails when I try to run it in Mule. Here is the code I use:
log.info ">>run"
Random random = new Random()
def ranInt = random.nextInt()
def ran = Math.abs(ranInt)%200;
log.info ">>sleep counter:"+flowVars.counter+" ran: "+ran
sleep(ran)
And here is an exception that gets thrown:
Caused by:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed: Script26.groovy: 9: expecting EOF, found '?' # line 9,
column 25. def ran = Math.abs(?400)?%20?0;
^
1 error
You have some extra unicode characters in line 4. If you convert it to hex you will get:
64 65 66 20 72 61 6e 20 3d 20 4d 61 74 68 2e 61 62 73 28 e2 80 8b 72 61 6e 49 6e 74 29 e2 80 8b 25 32 30 e2 80 8b 30 3b
Now if you convert this hex back to ascii, you will get:
def ran = Math.abs(​ranInt)​%20​0;
There is a character ​ added after first (, after ) and after first 0. If you remove it, your code will compile correctly.
Here is the hex of curated line:
64 65 66 20 72 61 6e 20 3d 20 4d 61 74 68 2e 61 62 73 28 72 61 6e 49 6e 74 29 25 32 30 30 3b
And the line itself:
def ran = Math.abs(ranInt)%200;
So im trying to try stuff with buffers i opened a file in buffer mode then i copied it, but how do i define it back in a variable? Like so i can give the buffer object to someone else and they can assign it as buffer and use it normally i tried googling around and testing stuff. Hope someone can help if you didn't understand anythin feel free to ask.
More details:
const fs = require("fs");
const f =
fs.readFileSync("path/to/file.txt")
f
// now a buffer
// copied buffer and kept it
// now how do i turn it back into a buffer object for later use?
For example i have
<Buffer 53 45 43 52 45 54 20 4d 45 53 53 41 47
45 20 50 41 53 53 57 4f 52 44 20 3d 0a 44 72 6d
43 6b 4c 38 56 71 45 76 48 4f 70 6c 4d 61 77 52
59 33 4e 31 6d ... >
How would i enter it to node? If i just pasted it it as it is or as string won't detect it as a buffer dunno if this is possible
Use Buffer.from(string);
const fs = require("fs");
const f = fs.readFileSync("path/to/file.txt");
let st = f.toString();
let new_f = new Buffer.from(st);
//Now new_f is the buffer object 'f' you got from reading the file.
How to print the stdout from console.log(require("child_process").execSync('ls'))
I tried in ts
import { execSync } from 'child_process';
console.log(execSync('ls -la'));
which was then compiled to js:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
console.log(child_process_1.execSync('ls -la'));
but when I run it I get only like buffer how to get the stdout?
$ node app.js
$ <Buffer 74 6f 74 61 6c 20 38 38 0a 64 72 77 78 72 2d 78 72 2d 78 20 20 31 31 20 74 6f 6d 65 72 2e 62 65 6e 64 61 76 69 64 20 20 73 74 61 66 66 20 20 20 20 33 ... >
What am i missing? how to get the textual stdout?
Your last line should be:
console.log(child_process_1.execSync('ls -la').toString());
execSync returns a buffer, simply call toString on the buffer to get the contents of the buffer as a string.