Code examples and tutorials for MyTokenWallet SDK
import { MyTokenWallet } from '@mytokenwallet/sdk';
const wallet = new MyTokenWallet({
network: 'mainnet',
apiEndpoint: 'https://api.mytokenwallet.com'
});
const publicKey = await wallet.getPublicKey({
protocolID: [1, "MyApp"],
keyID: "registration",
counterparty: "self",
forSelf: true
});
console.log('Public Key:', publicKey);
const cert = await wallet.createCertificate({
type: 'https://example.com/user-profile',
subject: '0123456789abcdef...',
serialNumber: '1',
fields: {
name: 'John Doe',
email: 'john@example.com'
}
});
console.log('Certificate:', cert);
const tx = await wallet.createTransaction({
outputs: [{
satoshis: 1000,
script: '76a914...88ac'
}],
description: 'Payment for service'
});
console.log('TXID:', tx.txid);
const signature = await wallet.signMessage({
message: 'Hello, BSV!',
protocolID: [1, "MyApp"],
keyID: "signing-key"
});
console.log('Signature:', signature);
const isValid = await wallet.verifyCertificate({
certificate: cert,
verifier: '0123456789abcdef...'
});
console.log('Valid:', isValid);
const decrypted = await wallet.decrypt({
ciphertext: encryptedData,
protocolID: [2, "SecureMessaging"],
keyID: "message-key",
counterparty: senderPublicKey
});
console.log('Message:', decrypted);
const action = await wallet.createAction({
description: 'Send payment',
inputs: [...],
outputs: [...],
lockTime: 0
});
console.log('Action:', action);
Implement passwordless authentication using BRC-43 protocol
// Generate authentication key
const authKey = await wallet.getPublicKey({
protocolID: [1, "MyApp"],
keyID: "auth",
counterparty: "self",
forSelf: true
});
// Sign challenge
const signature = await wallet.signMessage({
message: challengeNonce,
protocolID: [1, "MyApp"],
keyID: "auth"
});
Accept BSV payments with transaction monitoring
// Create payment request
const payment = await wallet.createTransaction({
outputs: [{
satoshis: amountInSatoshis,
script: recipientScript
}],
description: `Payment for Order #${orderId}`
});
// Monitor confirmation
await wallet.waitForConfirmation(payment.txid);
Sign documents and verify authenticity
// Sign document hash
const docHash = sha256(documentContent);
const signature = await wallet.signMessage({
message: docHash,
protocolID: [2, "DocSigning"],
keyID: "document-key"
});
// Store signature on-chain
await wallet.storeSignature(signature);
End-to-end encrypted messaging
// Encrypt message for recipient
const encrypted = await wallet.encrypt({
plaintext: messageData,
protocolID: [2, "SecureChat"],
keyID: "chat-key",
counterparty: recipientPublicKey
});
// Send encrypted data
await sendMessage(encrypted);