I'm attempting to use the private key I generated using the Solana command-line to create a wallet in JavaScript / Node.
I want to use the web3.Keypair.fromSeed() method.
Here are the steps I've take so far.
created a solana wallet : solana-keygen new -o keyfile.json
display what is in that file -- it's a 64 byte array (this is a test key so no worries that this is the private key
[237,158,92,107,132,192,1,57,8,20,213,108,29,227,37,8,3,105,196,244,8,221,184,199,62,253,98,131,33,165,165,215,14,7,46,23,221,242,240,226,94,79,161,31,192,163,13,25,106,53,34,215,83,124,162,156,8,97,194,180,213,179,33,68]
However, the call to fromSeed() only wants 32 bytes.
3. check the solana address so I know when everything is working properly :
> solana address -->
wm4MapPtFaUoSsyBJppVnChYMGvHzTeAL6BNZ5GmSqH
That's the public key
How do I call web3.Keypair.fromSeed() to load that private key and get my public address (aka public key)?
let web3 = require('#solana/web3.js');
let splToken = require('#solana/spl-token');
// load up the first 32 bytes of the 64 byte array that was in our keyfile.json
// Only need the first 32 bytes so I use slice() just to make sure it's the correct length
let firstWinPrivKey = [237,158,92,107,132,192,1,57,8,20,213,108,29,227,37,8,3,105,196,244,8,221,184,199,62,253,98,131,33,165,165,215,14,7,46,23,221,242,240,226,94,79,161,31,192,163,13,25,106,53,34,215,83,124,162,156,8,97,194,180,213,179,33,68]
.slice(0,32);
// print the length of the array so we know it is correct
// the fromSeed() method requires 32 bytes
console.log(firstWinPrivKey.length);
let firstWinWallet = web3.Keypair.fromSeed(Uint8Array.from(firstWinPrivKey));
console.log(firstWinWallet.secretKey);
console.log(firstWinWallet.publicKey.toString());
Notice that you have to cast the array to a Uint8Array (Uint8Array.from())
When we print out the secretKey, you'll see the same bytes you passed in.
And finally when we print out the publicKey you'll see that same value that we saw with the command line
> solana address
Now you can use the wallet in code.
Here's the final output from this short script:
32
Uint8Array(64) [
237, 158, 92, 107, 132, 192, 1, 57, 8, 20, 213,
108, 29, 227, 37, 8, 3, 105, 196, 244, 8, 221,
184, 199, 62, 253, 98, 131, 33, 165, 165, 215, 14,
7, 46, 23, 221, 242, 240, 226, 94, 79, 161, 31,
192, 163, 13, 25, 106, 53, 34, 215, 83, 124, 162,
156, 8, 97, 194, 180, 213, 179, 33, 68
]
wm4MapPtFaUoSsyBJppVnChYMGvHzTeAL6BNZ5GmSqH
If you want to use ".json" file, you can do something like this:
import Fs from "#supercharge/fs";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "#solana/web3.js";
const decodedKey = new Uint8Array(
JSON.parse(
//replace with actual path from home dir. For example '.config/solana/devnet.json'
Fs.readFileSync(Fs.homeDir("path to key.json")).toString();
));
let keyPair = Keypair.fromSecretKey(decodedKey);
I use additional package https://www.npmjs.com/package/#supercharge/fs for working with files.
import { Keypair } from "#solana/web3.js";
import fs from "fs";
function loadKeypairFromFile(filename: string): Keypair {
const secret = JSON.parse(fs.readFileSync(filename).toString()) as number[];
const secretKey = Uint8Array.from(secret);
return Keypair.fromSecretKey(secretKey);
}
Related
I'm facing trouble when training a model using pre-trained inceptionV3 for my own image data set.
I'm loading images using data.Dataset loader and 'transforms' for image transformation.
Here's my inceptionV3 model
inceptionV3 = torchvision.models.inception_v3(pretrained=True)
pretrained_model = nn.Sequential(*list(inceptionV3.children()))
pretrained_model = nn.Sequential(*list(pretrained_features.children())[:-1])
for param in pretrained_model.parameters(): param.requires_grad = False
Here's my transforms code
data_transforms = transforms.Compose([
transforms.Scale((299,299)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
I'm getting this error at the line 'out_features = pretrained_model(inputs)'
Traceback (most recent call last): File "build_models.py", line 42, in <module> use_gpu=use_gpu)
File "/home/ubuntu/apparel-styles/ml_src/classifiers.py", line 488, in create_attributes_model batch_size, num_workers, num_epochs, use_gpu=use_gpu)
File "/home/ubuntu/apparel-styles/ml_src/classifiers.py", line 276, in train_model flatten_pretrained_out=flatten_pretrained_out)
File "/home/ubuntu/apparel-styles/ml_src/classifiers.py", line 181, in train_attribute_model out_features = pretrained_model(inputs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 68, in forward outputs = self.parallel_apply(replicas, inputs, kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 78, in parallel_apply return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 67, in parallel_apply raise output
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 42, in _worker output = module(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward input = module(input)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torchvision/models/inception.py", line 312, in forward x = self.fc(x)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward return F.linear(input, self.weight, self.bias)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 835, in linear return torch.addmm(bias, input, weight.t()) RuntimeError: size mismatch at /pytorch/torch/lib/THC/generic/THCTensorMathBlas.cu:243
After getting this error, I tried to print the input size. It's value is
print(inputs.shape)
torch.Size([256, 3, 299, 299])
I want to get information from a snmp device which is configured with auth protocol MD5 and priv protocol 3DES.
The configuration statement is
snmp-server user testuser testgroup v3 auth md5 authmd5pwd priv 3des privpwddes
The pysnmp code to access the device is
from pysnmp.hlapi import *
def get(mib):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
UsmUserData('nmsuser', 'authmd5pwd', 'privpwddes',
authProtocol=usmHMACMD5AuthProtocol,
privProtocol=usm3DESEDEPrivProtocol),
UdpTransportTarget(('10.96.158.251', 161)),
ContextData(),
ObjectType(ObjectIdentity(mib)))
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(varBind[1])
if __name__ == "__main__":
get('.1.3.6.1.2.1.1.5.0')
When I execute this I get
Traceback (most recent call last):
File "pytest.py", line 24, in
get('.1.3.6.1.2.1.1.5.0')
File "pytest.py", line 11, in get
ObjectType(ObjectIdentity(mib)))
File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 113, in getCmd
File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/carrier/asyncore/dispatch.py", line 50, in runDispatcher
pysnmp.error.PySnmpError: poll error: Traceback (most recent call last):
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher
use_poll=True, map=self.__sockMap, count=1)
; File "/usr/lib/python2.7/asyncore.py", line 220, in loop
poll_fun(timeout, map)
; File "/usr/lib/python2.7/asyncore.py", line 201, in poll2
readwrite(obj, flags)
; File "/usr/lib/python2.7/asyncore.py", line 123, in readwrite
obj.handle_error()
; File "/usr/lib/python2.7/asyncore.py", line 108, in readwrite
obj.handle_read_event()
; File "/usr/lib/python2.7/asyncore.py", line 449, in handle_read_event
self.handle_read()
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/carrier/asyncore/dgram/base.py", line 163, in handle_read
self._cbFun(self, transportAddress, incomingMessage)
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/carrier/base.py", line 70, in _cbFun
self, transportDomain, transportAddress, incomingMessage
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/entity/engine.py", line 152, in __receiveMessageCbFun
self, transportDomain, transportAddress, wholeMsg
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/rfc3412.py", line 344, in receiveMessage
statusInformation
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/rfc3412.py", line 533, in __expireRequest
cachedParams['cbCtx'])
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/entity/rfc3413/cmdgen.py", line 104, in processResponsePdu
(origSendRequestHandle, cbFun, cbCtx))
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/rfc3412.py", line 153, in sendPdu
pduVersion, PDU, expectResponse, sendPduHandle
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/mpmod/rfc3412.py", line 240, in prepareOutgoingMessage
securityEngineId, securityName, securityLevel, scopedPDU
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/secmod/rfc3414/service.py", line 525, in generateRequestMsg
None)
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/secmod/rfc3414/service.py", line 395, in __generateRequestOrResponseMsg
(snmpEngineBoots, snmpEngineTime, None), dataToEncrypt
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/secmod/eso/priv/des3.py", line 117, in encryptData
encryptKey, snmpEngineBoots
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/secmod/eso/priv/des3.py", line 77, in __getEncryptionKey
preIV = privKey[24:32]
; File "/usr/local/lib/python2.7/dist-packages/pyasn1/type/univ.py", line 996, in __getitem__
return self.clone(self._value[i])
; File "/usr/local/lib/python2.7/dist-packages/pysnmp-4.4.3-py2.7.egg/pysnmp/proto/rfc1902.py", line 202, in clone
return univ.OctetString.clone(self, *args, **kwargs).setFixedLength(self.getFixedLength())
; File "/usr/local/lib/python2.7/dist-packages/pyasn1/type/base.py", line 349, in clone
return self.__class__(value, **initilaizers)
; File "/usr/local/lib/python2.7/dist-packages/pyasn1/type/univ.py", line 819, in __init__
base.AbstractSimpleAsn1Item.__init__(self, value, **kwargs)
; File "/usr/local/lib/python2.7/dist-packages/pyasn1/type/base.py", line 246, in __init__
raise exType('%s at %s' % (exValue, self.__class__.__name__))
;ValueConstraintError: , > failed at: ValueConstraintError(" failed at: ValueConstraintError('',)",) at OctetString
Seems to be a bug in pysnmp 4.4.3 when using 3DES with short-hash HMAC such as MD5.
You can either use some other AUTH algorithm which produces at least 32-octet long hash or pull fixed pysnmp (4.4.4) from its master branch.
Your script seems to work with the simulator once you change username/passphrase.
I have a list which looks like this,
data_raw=[[], [7944, -11896, 3376, 1627, -850, -3991], [8688, -12192, 1936,1404, -616, -3536], [6540, -11800, 1608, 3021, 780, -1061], [6804, -11864, 3828, 4310, 552, -2343], [7208, -12544, 3768, 2542, 286, 1264], [7048, -14532, 6824, 2528, 1577, 2583], [6112, -17376, 10180, 132, -1716, 1001], [7576, -21140, 6796, -1725, 1657, 1980], [2928, -31716, 15400, -5945, 824, -3558], [8940, -24016, 11540, -12047,-5574, -16019], [12020, -17516, 3744, -14637, 1521, -14791], [8916, -16160, 5860, -14122, -3793, -13597], [10144, -8124, 1076, -12027, -1194, -8809], [8088, -7264, 928, -18441, -2058, -80], [7684, -4896, -5224, -9800, 2427, 2054], [2040, -7776, -3520, -9306, 4442, 1276], [6240, -7340, -7216, -1757, -3630, -2734], [5720, -3940, -4632, -901, 1469, -1682], [5244, -4676, -5648, 2720, 3526, -436], [4016, -5336, -2976, 4280, 4543, -1562], [4028, -5156, -5560, 7391, 5000, -1317], [748, -9800, -2144, 10353, 6616, -3390], [10268, -7220, 1844, 11657, 8566, -4740], [11300, -10752, 4508, 11666, 10771, -1356], [16792, -10180, 24476, 13474, 2828, -5205], [19208, -10908, 6636, 9747, 10501, 1676], [7540, -20480, 13248, 8715, 12607, 7017], [15780, -20832, 11600, 5686, 4737, -3654], [18004, -20072, 17716, 1082, 2218, -3181], [16516, -18528, 14568, -3931, -5457, -4260], [15596, -12596,9084, -7735, -8646, -4221], [13296, -8948, 6316, -9215, -8260, -3225], [10860, -8124, 6116, -7264, -7653, -678], [7968, -7828, 5384, -8604, -7043, 1076], [8008, -5316, 1816, -6457, -7414, -50], [9304, -3568, 1092, -4895, -4654, 3123], [9560, -3932, -352, -904, -6369, 1981], [14692, -3168, 836, 2406, -8099, 3121], [13088, -6292, 44, 5503, -11759, 6405], [11892, -8316, -836, 6159, -8673, 10130], [8252, -13220, -1064, 8279, -7906, 12090], [3572, -18392, -1536, 5995, -2719, 10667], [2864, -19576, 960, 6207, -4501, 6554], [1024, -20140, -1964, 7834, -10817, 5197]]
When i use this code:
data = np.array(data_raw).astype(float)
I got an error:
>> Traceback (most recent call last):
data = np.array(data_raw).astype(float)
ValueError: setting an array element with a sequence.
Does anyone know why this error occurred?
The first element of your 2D list is an empty list, causing this issues. If you remove it or just use
data = np.array(data_raw[1:]).astype(float)
it will work as intended.
Is it possible in keras to use tensorflow tensor manipulations commands for altering the shape of data, of split by an axis or somethign like that..
I am currently trying something like this:
def row_convolution(input):
filter_size = 8
temp_list = []
input = tf.placeholder(tf.float32,shape=(None,33,1,8,45,3))
for section in tf.unstack(input,axis=1):
section = tf.reshape(section,[1,8,45,3])
temp_list.append((Conv2D(filters = 1, kernel_size = (8,45), activation='relu' , name = 'conv')(section)))
print "len: " + str(len(temp_list))
return tf.convert_to_tensor(np.array(temp_list))
Which is is the function for a lamdba layer, but the last command give me an error message stating:
Traceback (most recent call last):
File "keras_cnn_phoneme_original_fit_generator.py", line 182, in <module>
fws()
File "keras_cnn_phoneme_original_fit_generator.py", line 167, in fws
model.add(Lambda(row_convolution,input_shape = (33,1,8,45,3)))
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 422, in add
layer(x)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 554, in __call__
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 659, in call
return self.function(inputs, **arguments)
File "keras_cnn_phoneme_original_fit_generator.py", line 147, in row_convolution
return tf.convert_to_tensor(np.array(temp_list))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 637, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 702, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 110, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 99, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 451, in make_tensor_proto
append_fn(tensor_proto, proto_values)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 109, in SlowAppendObjectArrayToTensorProto
tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <tf.Tensor 'lambda_1/conv/Relu:0' shape=(1, 1, 1, 1) dtype=float32>
The code:
class StockFactory(UniqueObjectsFactory):
FACTORY_FOR = Stock
FACTORY_DJANGO_GET_OR_CREATE = ('name', 'market')
market = factory.SubFactory(MarketFactory)
symbol = FuzzyAttribute(lambda: ''.join(random.choice(string.ascii_uppercase) for _ in xrange(4)))
name = FuzzyCompanyName()
# last_trade_price = fuzzy.FuzzyDecimal(0.0, 10000.0)
class PositionsFactory(FreezeableFactory):
FACTORY_FOR = Position
FACTORY_DJANGO_GET_OR_CREATE = ('stock','AnotherObject')
id = FuzzyInteger(100000)
stock = factory.SubFactory(Stock)
AnotherObject = factory.SubFactory(AnotherObject) #If I comment stock out it would fail here
created_date = FuzzyDateTime(start_dt=datetime(2013, 1, 1, tzinfo=compat.UTC))
The error:
File "/home/alon/Projects/stox-server/execution/tests/functional/test_positions.py", line 21, in setUp
PositionsFactory.create( portfolio=self.portfolio)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 522, in create
attrs = cls.attributes(create=True, extra=kwargs)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 365, in attributes
force_sequence=force_sequence,
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 283, in build
return stub.__fill__()
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 83, in __fill__
res[attr] = getattr(self, attr)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
val = val.evaluate(self, self.__containers)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
containers=containers,
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 75, in evaluate
return self.function(obj)
File "/home/alon/Projects/stox-server/execution/tests/common/factories.py", line 173, in <lambda>
symbol = factory.LazyAttribute(lambda pos: pos.stock.symbol)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
val = val.evaluate(self, self.__containers)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
containers=containers,
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 299, in evaluate
return self.generate(sequence, obj, create, defaults)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 386, in generate
return subfactory.simple_generate(create, **params)
AttributeError: type object 'Stock' has no attribute 'simple_generate'
Any clues? Ideas? I work with factory-boy quite a lot and most of the time it's an excellent tool. but after hours of debugging I just cant find the problem
So stupid of me:
Those line are wrong:
stock = factory.SubFactory(Stock)
AnotherObject = factory.SubFactory(AnotherObject)
Should have been:
stock = factory.SubFactory(StockFactory)
AnotherObject = factory.SubFactory(AnotherObjectFactory)
Hope it helps anyone else who bumps into this issue