Skip to content

Instantly share code, notes, and snippets.

@dashameter
Created December 4, 2021 08:17
Show Gist options
  • Save dashameter/e31d7e82efc2024177401d34580b0fb8 to your computer and use it in GitHub Desktop.
Save dashameter/e31d7e82efc2024177401d34580b0fb8 to your computer and use it in GitHub Desktop.
const Dash = require("dash");
const dashpaycrypto = require("dashpay-crypto");
// const Base58 = require('../lib/encoding/base58');
// changing mnemonic requires adjusting or removing skipSynchronizationBeforeHeight
// const senderMnemonic = 'catalog world quit margin supreme pony vacuum park inch soul daughter manage'; // B-DashpayJS
// const senderMnemonic = 'upper renew that grow pelican pave subway relief describe enforce suit hedgehog blossom dose swallow'; // iOS test vector
// const senderMnemonic =
// "differ need van hire walk globe busy drill name neutral below increase"; // C-DashpayJS THIS MNEMONIC HAS ISSUES REGISTERING A USERNAME IN ANDROID WALLET
const senderMnemonic =
"embody aunt amazing unaware submit degree chalk amazing bubble bargain potato increase"; // C-DashpayJS
const receiverUsername = "A-DashpayJS";
const clientOpts = {
network: "testnet",
// dapiAddresses: [
// '34.220.41.134',
// '18.236.216.191',
// '54.191.227.118',
// ],
wallet: {
mnemonic: senderMnemonic,
unsafeOptions: {
skipSynchronizationBeforeHeight: 485512,
},
},
apps: {
// dashpay: {
// contractId: '2DAncD4YTjfhSQZYrsQ659xbM7M5dNEkyfBEAg9SsS3W',
// },
// dpns: { contractId: process.env.NUXT_DPNS_CONTRACTID },
},
};
const client = new Dash.Client(clientOpts);
const main = async () => {
client.account = await client.getWalletAccount();
const senderIdentityId = client.account.identities.getIdentityIds()[0];
const senderIdentity = await client.platform.identities.get(senderIdentityId);
const senderHdPrivateKey = client.account.identities.getIdentityHDKeyByIndex(
0,
0
);
const senderPrivateKey = senderHdPrivateKey.privateKey.toString();
const receiverDPNS = await client.platform.names.resolve(
`${receiverUsername}.dash`
);
const receiverIdentity = await client.platform.identities.get(
receiverDPNS.ownerId
);
const receiverPublicKey = receiverIdentity.toJSON().publicKeys[0].data;
// ECDH Shared Key / Diffie-Hellman Key Exchange
// https://github.com/dashpay/dips/blob/feat/dashpay/dip-0015.md#ecdh-shared-key-senderkeyindex-and-recipientkeyindex
const sharedSecret = dashpaycrypto.ecdhSharedKey(
senderPrivateKey,
receiverPublicKey
);
// DashPay Incoming Funds Derivation Path
// https://github.com/dashpay/dips/blob/feat/dashpay/dip-0015.md#dashpay-incoming-funds-derivation-path
const publicKeyDIP15 = dashpaycrypto.deriveExtendedPublicKeyDIP15(
senderHdPrivateKey,
senderIdentityId,
receiverIdentity.id.toString()
);
const contactRequest = {
toUserId: receiverIdentity.id,
senderKeyIndex: 0,
accountReference: dashpaycrypto.createAccountReference(
senderPrivateKey,
publicKeyDIP15
),
recipientKeyIndex: 0,
encryptedPublicKey: Buffer.from(
dashpaycrypto.encryptPublicKey(publicKeyDIP15, sharedSecret),
"hex"
),
encryptedAccountLabel: Buffer.from(
dashpaycrypto.encryptAccountLabel("Default Account", sharedSecret),
"base64"
),
};
console.log(contactRequest);
const contactRequestDocument = await client.platform.documents.create(
"dashpay.contactRequest",
senderIdentity,
contactRequest
);
console.log(
"contactRequestDocument.toJSON() :>> ",
contactRequestDocument.toJSON()
);
const documentBatch = {
create: [contactRequest], // Document(s) to create
replace: [], // Document(s) to update
delete: [], // Document(s) to delete
};
// Sign and submit the document(s)
return client.platform.documents.broadcast(documentBatch, senderIdentity);
};
main()
.then((d) => console.log(d, "Success!"))
.catch((e) => console.error("Something went wrong:\n", e))
.finally(() => client.disconnect());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment