Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add utils.tx.signTxn using senderPublicKey
  • Loading branch information
wk3368 committed Nov 9, 2021
1 parent 0673299 commit e4d8f5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/encoding/index.spec.ts
Expand Up @@ -138,7 +138,7 @@ test("encoding SignedUserTransaction hex, 0x1::DaoVoteScripts::cast_vote", async

const provider = new JsonRpcProvider(nodeUrl);

// const senderSequenceNumber = await provider.getSequenceNumber(senderAddressHex)
const senderSequenceNumber = await provider.getSequenceNumber(senderAddressHex)

// TODO: generate maxGasAmount from contract.dry_run -> gas_used
const maxGasAmount = 10000000
Expand All @@ -147,10 +147,10 @@ test("encoding SignedUserTransaction hex, 0x1::DaoVoteScripts::cast_vote", async

// because the time system in dev network is relatively static,
// we should use nodeInfo.now_secondsinstead of using new Date().getTime()
// const nowSeconds = await provider.getNowSeconds()
const nowSeconds = await provider.getNowSeconds()
// expired after 12 hours since Unix Epoch
// const expiredSecs = 43200
// const expirationTimestampSecs = nowSeconds + expiredSecs
const expiredSecs = 43200
const expirationTimestampSecs = nowSeconds + expiredSecs

const sendAmount = 0.01;
const config = { creator: "0xb2aa52f94db4516c5beecef363af850a", id: 1, type_args_1: "0x1::OnChainConfigDao::OnChainConfigUpdate<0x1::TransactionPublishOption::TransactionPublishOption, d::e::f>" };
Expand Down
37 changes: 13 additions & 24 deletions src/utils/tx.ts
Expand Up @@ -112,15 +112,23 @@ export async function getSignatureHex(
}

async function generateSignedUserTransaction(
senderPrivateKey: HexString,
senderPrivateKey: string,
signatureHex: string,
rawUserTransaction: starcoin_types.RawUserTransaction
): Promise<starcoin_types.SignedUserTransaction> {
// Step 3-1: generate authenticator
const senderPublicKeyMissingPrefix = await ed.getPublicKey(stripHexPrefix(senderPrivateKey))
const senderPublicKeyMissingPrefix = <string><unknown>await ed.getPublicKey(stripHexPrefix(senderPrivateKey))
const signedUserTransaction = signTxn(senderPublicKeyMissingPrefix, signatureHex, rawUserTransaction)
return Promise.resolve(signedUserTransaction)
}

const public_key = new starcoin_types.Ed25519PublicKey(arrayify(senderPublicKeyMissingPrefix, { allowMissingPrefix: true }))
const signature = new starcoin_types.Ed25519Signature(arrayify(signatureHex))
export function signTxn(
senderPublicKey: string,
signatureHex: string,
rawUserTransaction: starcoin_types.RawUserTransaction
): starcoin_types.SignedUserTransaction {
// Step 3-1: generate authenticator
const public_key = new starcoin_types.Ed25519PublicKey(arrayify(addHexPrefix(senderPublicKey)))
const signature = new starcoin_types.Ed25519Signature(arrayify(addHexPrefix(signatureHex)))
const transactionAuthenticatorVariantEd25519 = new starcoin_types.TransactionAuthenticatorVariantEd25519(public_key, signature)

// Step 3-2: generate SignedUserTransaction
Expand Down Expand Up @@ -164,25 +172,6 @@ export async function signRawUserTransaction(
return hex
}


// export async function signTxn(
// accountPrivateKey: MultiEd25519KeyShard,
// rawUserTransaction: starcoin_types.RawUserTransaction
// ): Promise<string> {

// // TransactionAuthenticator::multi_ed25519(key.public_key(), key.sign(message).into())
// const multiEd25519SignatureShard = await accountPrivateKey.sign(rawUserTransaction)

// const transactionAuthenticatorVariantMultiEd25519 = new starcoin_types.TransactionAuthenticatorVariantMultiEd25519(accountPrivateKey.publicKey(), multiEd25519SignatureShard.signature)

// // Step 3-2: generate SignedUserTransaction
// const signedUserTransaction = new starcoin_types.SignedUserTransaction(rawUserTransaction, transactionAuthenticatorVariantMultiEd25519)

// // Step 4: get SignedUserTransaction Hex
// const hex = getSignedUserTransactionHex(signedUserTransaction)
// return hex
// }

function encodeStructTypeTag(
str: string
): TypeTag {
Expand Down

0 comments on commit e4d8f5d

Please sign in to comment.